Caie A2 Level: Computer Science
Caie A2 Level: Computer Science
ORG
CAIE A2 LEVEL
COMPUTER
SCIENCE
SUMMARIZED NOTES ON THE PRACTICAL SYLLABUS
Prepared for Awab Aqib for personal use only.
CAIE A2 LEVEL COMPUTER SCIENCE
Python Code
1. Sorting and Searching def binary_search(arr, target):
Part of the Computational Thinking and Problem-Solving while low <= high:
Chapter mid = (low + high) // 2
mid_element = arr[mid]
1.1. Linear Search
if mid_element == target:
How does it work? return f"Item {target} found at index {mid}."
elif mid_element < target:
The user is asked to enter an item they want to find in an low = mid + 1
array. else:
All elements of an array are searched one by one until the high = mid - 1
item the user entered is found.
When an item is found, the algorithm outputs an return f"Item {target} not found in the array."
appropriate message saying that the item is found and
which index/location the item is at.
1.3. Bubble Sort
If the particular item is not found, the algorithm outputs
an appropriate message saying that the item is not found.
How Does It Work?
The linear search algorithm has a Big O notion of O(n).
Bubble sort compares adjacent elements in the list/array.
Python Code 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.
The process is repeated until the entire array is sorted.
WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is authorised
for personal use only by Awab Aqib at Lahore Grammar School on 03/09/24.
CAIE A2 LEVEL COMPUTER SCIENCE
# Move elements of arr[0..i-1] that are greater than key to one position
TopOfStack ahead of their
= TopOfStack + 1 current position
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j] MyStack[TopOfStack] = NewItem
j -= 1
ENDIF
arr[j + 1] = key
ENDPROCEDURE
In the worst case, it has a time complexity of O(n^2), where n
is the number of elements in the array. Use of Stacks:
Insertion sort is efficient for small datasets or partially sorted
datasets. Interrupt Handling
The contents of the register and the PC are saved and
put on the stack when the interrupt is detected
2. Abstract Data Types (ADTs) The return addresses are saved onto the stack as well
Retrieve the return addresses and restore the register
Part of the Computational Thinking and Problem-Solving contents from the stack once the interrupt has been
Chapter serviced
Evaluating mathematical expressions held in Reverse
2.1. Stacks Polish Notation
Procedure Calling
Stack – an ADT where items can be popped or pushed Every time a new call is made, the return address
from the top of the stack only must be stored
LIFO – Last In First Out data structure Return addresses are recalled in the order ‘the last
one stored will be the first to be recalled.’
Popping (pseudocode) If there are too many nested calls, then stack overflow
2.2. Queues
PROCEDURE PopFromStack
Queue: an ADT where new elements are added at the end
IF TopOfStack = -1 of the queue, and elements leave from the start of the
queue
THEN FIFO: First In, First Out Data structure
Creating a Circular Queue (pseudocode):
OUTPUT “Stack is already empty”
PROCEDURE Initialise
ELSE
Front = 1
OUTPUT MyStack[ TopOfStack ] “is popped”
Rear = 6
TopOfStack ← TopOfStack – 1
NumberInQueue := 0
ENDIF
END PROCEDURE
ENDPROCEDURE
To add an Element to the Queue (pseudocode):
WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is authorised
for personal use only by Awab Aqib at Lahore Grammar School on 03/09/24.
CAIE A2 LEVEL COMPUTER SCIENCE
THEN Write (“Queue overflow”) Items may only be removed from the front of the list and
added to the end of the list
ELSE
2.3. Linked Lists
IF Rear == 6
It can be represented as two 1-D arrays - a string array
THEN Rear = 1 for data values and an integer array for pointer values
Creating a Linked list →Setting values of pointers in the
ELSE Rear = Rear + 1 free list and empty data linked list
ENDIF
FOR Index ← 1 TO 49
Q[Rear] = NewItem
NameList[Index].Pointer ← Index + 1
NumberInQueue =NumberInQueue +1
ENDFOR
ENDIF
NameList[50].Pointer ← 0
ENDPROCEDURE
HeadPointer ← 0
PROCEDURE DeQueue
IF NumberInQueue == 0
ELSE
NewItem = Q[Front]
NumberInQueue = NumberInQueue – 1
IF Front ==6
THEN Front = 1
ELSE
Front = Front + 1
ENDIF
WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is authorised
for personal use only by Awab Aqib at Lahore Grammar School on 03/09/24.
CAIE A2 LEVEL COMPUTER SCIENCE
WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is authorised
for personal use only by Awab Aqib at Lahore Grammar School on 03/09/24.
CAIE A2 LEVEL COMPUTER SCIENCE
3. Recursion
Part of the Computational Thinking and Problem-Solving
Chapter
WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is authorised
for personal use only by Awab Aqib at Lahore Grammar School on 03/09/24.
CAIE A2 LEVEL COMPUTER SCIENCE
Part of the Further Programming Chapter 4.3. Defining Classes and Methods
4.1. Key Terms & Definitions • To define a class in Python, use the class keyword followed
by the class name.
Objects: Instances of classes representing real-world • Inside the class, you can define attributes (variables) and
entities. methods (functions) that belong to the class.
Properties/Attributes: The data items/attributes and the Here's a simple example of defining a class called Person
data types // characteristics defined in a class. with attributes and methods as shown below:
Methods: the procedures/ functions / programmed
instructions in a class that act on the
properties/attributes.
Classes: Blueprint for creating objects with shared
attributes and methods.
Inheritance: It is a mechanism for creating a new class
based on an existing one, inheriting its attributes and
methods. Through inheritance, attributes contained in one
class (parent class) are made available to / reused by
another class (child class).
Polymorphism: Ability to use different classes through a
common interface. It allows the same method to take on
4.4. Get and Set Methods
different behaviours depending on which class is
These methods are used to access/change attributes set to
instantiated. These methods can be redefined for derived
be private in a class. These methods are decelerated inside
classes.
the class.
Containment (Aggregation): Combining multiple objects to
create a more complex object.
Encapsulation: Hiding the internal details of a class from
the outside.
Getters and Setters: Methods for accessing and modifying
object attributes. Get methods/Getters are used to access
attributes, while set methods/setters are used to modify
object attributes.
Instances: Individual objects created from a class.
4.2. Constructor
Constructors are functions that are used for initializing the 4.5. Inheritance
attributes/Properties of a class.
• Inheritance is a fundamental concept in OOP that allows you
A constructor in Object-Oriented Programming (OOP) is a to create a new class (a derived or child class) based on an
special method within a class that is automatically called existing class (a base or parent class).
when an object of that class is created. • The child class inherits the attributes and methods of the
Its primary purpose is to initialize the object's attributes or parent class and can also add new attributes and methods or
perform setup actions. override the ones inherited.
In Python, the constructor is typically named init and takes In the context of the Person class, Inheritance would involve
the self parameter, which refers to the created object. creating a child class, such as a Student or Employee, that
Inside the constructor, you initialize the object's attributes. inherits attributes and methods from the Person class. For
For example: example, a Student class could inherit the name and age
attributes and the get_name method from the Person class.
Polymorphism
WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is authorised
for personal use only by Awab Aqib at Lahore Grammar School on 03/09/24.
CAIE A2 LEVEL COMPUTER SCIENCE
• Polymorphism is the ability of different classes to be treated leading underscore). This indicates that __name it should not
as instances of a common base class. It allows objects of be accessed directly from outside the class. Instead, you
different classes to be used interchangeably if they provide a controlled interface through the get_name and
implement the same methods or interface. set_name methods, ensuring that name changes are
• Polymorphism promotes flexibility and extensibility in your validated and controlled.
code. Below is a brief code example illustrating how inheritance,
In the context of the Person class, Polymorphism could be polymorphism, and encapsulation could be applied to the
applied when you have different types of persons, such as Person class:
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
WWW.ZNOTES.ORG Copyright © 2024 ZNotes Education & Foundation. All Rights Reserved. This document is authorised
for personal use only by Awab Aqib at Lahore Grammar School on 03/09/24.
CAIE A2 LEVEL
Computer Science
© ZNotes Education Ltd. & ZNotes Foundation 2024. All rights reserved.
This version was created by Awab Aqib on 03/09/24 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).