0% found this document useful (0 votes)
16 views10 pages

CAIE-A2 Level-Computer Science - Practical

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

CAIE-A2 Level-Computer Science - Practical

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ZNOTES.

ORG

UPDATED TO 2023-2025 SYLLABUS

CAIE A2 LEVEL
COMPUTER SCIENCE
SUMMARIZED NOTES ON THE THEORY SYLLABUS
Prepared for Viduranga for personal use only.
CAIE A2 LEVEL COMPUTER SCIENCE
The binary search algorithm has a Big O notion of O(log n).

1. Sorting and Searching The log is of base 2.

Python Code
Algorithms
def binary_search(arr, target):
Part of the Computational Thinking and Problem-Solving
Chapter low, high = 0, len(arr) - 1

1.1. Linear Search while low <= high:


mid = (low + high) // 2
mid_element = arr[mid]
How does it work?
The user is asked to enter an item they want to find in an if mid_element == target:
array. return f"Item {target} found at index {mid}.
All elements of an array are searched one by one until elif mid_element < target:
the item the user entered is found. low = mid + 1
When an item is found, the algorithm outputs an else:
appropriate message saying that the item is found and high = mid - 1
which index/location the item is at.
If the particular item is not found, the algorithm outputs return f"Item {target} not found in the array."
an appropriate message saying that the item is not
found. 1.3. Bubble Sort
The linear search algorithm has a Big O notion of O(n).
How Does It Work?
Python Code
Bubble sort compares adjacent elements in the list/array.
They are swapped if the elements are in the wrong order
(according to the desired sorting).
The algorithm iterates through the array multiple times
in passes.
On each pass, the largest unsorted element "bubbles up"
to its correct position at the end of the array.
1.2. Binary Search The process is repeated until the entire array is sorted.
The necessary condition for a binary search is that the Python Code
list/array being searched must be ordered/sorted.
def bubble_sort(arr):
How Does It Work? n = len(arr)
# Traverse through all array elements
The middle item/index of the list is found for i in range(n):
Item at the middle of the list is compared to item user # Last i elements are already sorted, so we don'
inputs for j in range(0, n-i-1):
If the item in the middle of the list is the same as the # Swap if the element found is greater than
item that the user inputs, a message saying “item found” if arr[j] > arr[j+1]:
will be output. arr[j], arr[j+1] = arr[j+1], arr[j]
If the item is greater than what the user inputted, all the
items at the index lower than the middle index are In the worst case, it has a time complexity of O(n^2), where n
discarded. is the number of elements in the array.
If the item is lower than what the user inputted, all the
items at the index greater than the middle index are
discarded.
1.4. Insertion Sort
The above steps are repeated until the item searched for
How Does It Work?
is found
If one item is left in the list and it is not the item
searched for, a message saying “item not found” is
outputted.

WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is
authorised for personal use only by Viduranga at Royal Institute on 09/12/24.
CAIE A2 LEVEL COMPUTER SCIENCE

The algorithm starts with the assumption that the first


element in the array is already sorted. PROCEDURE PopFromStack
It then compares the next element with the sorted
portion of the array. IF TopOfStack = -1
If the next element is smaller, it shifts the larger
elements to the right until it finds the correct position for THEN
the next element and inserts it there.
The sorted portion of the array grows with each iteration OUTPUT “Stack is already empty”
until the entire array is sorted.
The process is repeated until all elements are in their ELSE
correct positions.
OUTPUT MyStack[ TopOfStack ] “is popped”
Python Code
TopOfStack ← TopOfStack – 1
def insertion_sort(arr):
for i in range(1, len(arr)): ENDIF
key = arr[i]
j = i - 1 ENDPROCEDURE

# Move elements of arr[0..i-1] that are grea Pushing (pseudocode)


while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1 PROCEDURE PushToStack

arr[j + 1] = key IF TopOfStack = MaxStackSize

In the worst case, it has a time complexity of O(n^2), where n THEN


is the number of elements in the array.
Insertion sort is efficient for small datasets or partially OUTPUT “Stack is full”
sorted datasets.
ELSE

2. Abstract Data Types (ADTs) TopOfStack = TopOfStack + 1

Part of the Computational Thinking and Problem-Solving MyStack[TopOfStack] = NewItem


Chapter
ENDIF
2.1. Stacks
ENDPROCEDURE
Stack – an ADT where items can be popped or pushed
from the top of the stack only Use of Stacks:
LIFO – Last In First Out data structure

Popping (pseudocode)

WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is
authorised for personal use only by Viduranga at Royal Institute on 09/12/24.
CAIE A2 LEVEL COMPUTER SCIENCE

Interrupt Handling
The contents of the register and the PC are saved and 2.3. Linked Lists
put on the stack when the interrupt is detected
The return addresses are saved onto the stack as well
Retrieve the return addresses and restore the
register contents from the stack once the interrupt
has been serviced
Evaluating mathematical expressions held in Reverse
Polish Notation
Procedure Calling
Every time a new call is made, the return address
must be stored
Return addresses are recalled in the order ‘the last
one stored will be the first to be recalled.’
If there are too many nested calls, then stack
overflow

2.2. Queues

WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is
authorised for personal use only by Viduranga at Royal Institute on 09/12/24.
CAIE A2 LEVEL COMPUTER SCIENCE

Binary Tree
START at Root Node

REPEAT

IF WantedItem = ThisItem

THEN Found = TRUE

ELSE

IF WantedItem > ThisItem


2.4. Binary Tree
THEN Follow Right Pointer

ELSE Follow Left Pointer

UNTIL Found or Null Pointer Encountered

3. Recursion
Part of the Computational Thinking and Problem-Solving
Chapter

3.1. Essential Features of a Recursion


Must have a base case/stopping condition
Must have a general case which calls itself (recursively) //
Defined in terms of itself
The general case should be changing its state and move
toward the base case
Unwinding occurs once the base case is reached.

3.2. Advantages and Disadvantages of a


Recursion

WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is
authorised for personal use only by Viduranga at Royal Institute on 09/12/24.
CAIE A2 LEVEL COMPUTER SCIENCE
Advantages Disadvantages
Less efficient in terms of Objects: Instances of classes representing real-world
Can produce simpler, more entities.
computer time and storage
natural solutions to a problem Properties/Attributes: The data items/attributes and
space
A lot more storage space is the data types // characteristics defined in a class.
used to store return addresses Methods: the procedures/ functions / programmed
and states. instructions in a class that act on the
This could lead to infinite properties/attributes.
recursion. Classes: Blueprint for creating objects with shared
attributes and methods.
3.3. How A Compiler Translates Inheritance: It is a mechanism for creating a new class
based on an existing one, inheriting its attributes and
Recursive Programming Code methods. Through inheritance, attributes contained in
one class (parent class) are made available to / reused by
Before the procedure call is executed, the current state of another class (child class).
the registers/local variables is saved onto a stack. Polymorphism: Ability to use different classes through a
When returning from a procedure call, the registers/local common interface. It allows the same method to take on
different behaviours depending on which class is
variables are re-instated on the stack
instantiated. These methods can be redefined for
When the stopping condition/base case is met, the
derived classes.
algorithm unwinds the last set of values that are taken
off the stack (in reverse order) Containment (Aggregation): Combining multiple
objects to create a more complex object.
Encapsulation: Hiding the internal details of a class from
4. Object-Oriented the outside.
Getters and Setters: Methods for accessing and
Programming modifying object attributes. Get methods/Getters are
used to access attributes, while set methods/setters are
Part of the Further Programming Chapter used to modify object attributes.
Instances: Individual objects created from a class.
4.1. Key Terms & Definitions
4.2. Constructor
Constructors are functions that are used for initializing the
attributes/Properties of a class.

A constructor in Object-Oriented Programming (OOP) is a


special method within a class that is automatically called
when an object of that class is created.
Its primary purpose is to initialize the object's attributes
or perform setup actions.
In Python, the constructor is typically named init and
takes the self parameter, which refers to the created
object.
Inside the constructor, you initialize the object's
attributes.

For example:

WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is
authorised for personal use only by Viduranga at Royal Institute on 09/12/24.
CAIE A2 LEVEL COMPUTER SCIENCE
• The child class inherits the attributes and methods of the
parent class and can also add new attributes and methods
or override the ones inherited.
In the context of the Person class, Inheritance would involve
creating a child class, such as a Student or Employee, that
inherits attributes and methods from the Person class. For
When you create an instance of the class, the constructor
example, a Student class could inherit the name and age
is invoked automatically to set the object's initial state.
attributes and the get_name method from the Person class.
This ensures that objects are created in a valid and
consistent state. Polymorphism
4.3. Defining Classes and Methods • Polymorphism is the ability of different classes to be
treated as instances of a common base class. It allows
• To define a class in Python, use the class keyword followed objects of different classes to be used interchangeably if
by the class name. they implement the same methods or interface.
• Inside the class, you can define attributes (variables) and • Polymorphism promotes flexibility and extensibility in your
methods (functions) that belong to the class. code.
Here's a simple example of defining a class called Person In the context of the Person class, Polymorphism could be
with attributes and methods as shown below: applied when you have different types of persons, such as
students, employees, and teachers, each having a get_name
method. You can call get_name on instances of these
different classes without knowing their specific type, as long
as they all have a get_name method.

Encapsulation
• Encapsulation is the practice of hiding the internal details
of a class and providing a controlled interface to access and
modify the class's attributes.
• This helps maintain the integrity and consistency of the
4.4. Get and Set Methods object's state by controlling how data is accessed and
modified.
These methods are used to access/change attributes set to In the context of the Person class, Encapsulation is applied
be private in a class. These methods are decelerated inside by making the name attribute private (by convention, using a
the class. leading underscore). This indicates that __name it should not
be accessed directly from outside the class. Instead, you
provide a controlled interface through the get_name and
set_name methods, ensuring that name changes are
validated and controlled.
Below is a brief code example illustrating how inheritance,
polymorphism, and encapsulation could be applied to the
Person class:

4.5. Inheritance
• Inheritance is a fundamental concept in OOP that allows
you to create a new class (a derived or child class) based on
an existing class (a base or parent class).

WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is
authorised for personal use only by Viduranga at Royal Institute on 09/12/24.
CAIE A2 LEVEL COMPUTER SCIENCE

• In the example above, the Student class inherits from the


Person class, demonstrating the concept of inheritance.
• Both classes can be treated interchangeably when calling
the get_name method, showing polymorphism.
• Encapsulation is maintained by controlling access to the
name attribute through getter and setter methods.

WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is
authorised for personal use only by Viduranga at Royal Institute on 09/12/24.
CAIE A2 Level
Computer Science

© ZNotes Education Ltd. & ZNotes Foundation 2024. All rights reserved.
This version was created by Viduranga on Mon Dec 09 2024 for strictly personal use only.
These notes have been created by Ashmit Bhola for the 2024-2025 syllabus.
The document contains images and excerpts of text from educational resources available on the internet and printed books.
If you are the owner of such media, test or visual, utilized in this document and do not accept its usage then we urge you to contact us
and we would immediately replace said media. No part of this document may be copied or re-uploaded to another website.
Under no conditions may this document be distributed under the name of false author(s) or sold for financial gain.
"ZNotes" and the ZNotes logo are trademarks of ZNotes Education Limited (registration UK00003478331).

You might also like