0% found this document useful (0 votes)
31 views55 pages

A-Level - Paper 3 - Study Guide

This study guide covers key topics for A-Level Computer Science, including data representation, communication technologies, hardware, system software, security, and artificial intelligence. It details user-defined data types, file organization, floating-point number representation, and various communication protocols like TCP/IP and HTTP. Additionally, it includes programming concepts such as searching, sorting, and recursion.

Uploaded by

Rawail Naveed
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)
31 views55 pages

A-Level - Paper 3 - Study Guide

This study guide covers key topics for A-Level Computer Science, including data representation, communication technologies, hardware, system software, security, and artificial intelligence. It details user-defined data types, file organization, floating-point number representation, and various communication protocols like TCP/IP and HTTP. Additionally, it includes programming concepts such as searching, sorting, and recursion.

Uploaded by

Rawail Naveed
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/ 55

A-Level Computer Science

Paper 3: Study Guide

Table of Contents
[Click below to immediately navigate to desired topic]

● Topic 13: Data Representation


○ 13.1: User-defined Data Types
○ 13.2: File Organisation and Access
○ 13.3: Floating-point Numbers, Representation, and Manipulation
● Topic 14: Communication and Internet Technologies
○ 14.1: Protocols
○ 14.2: Circuit Switching, Packet Switching
● Topic 15: Hardware and Virtual Machines
○ 15.1: Processors, Parallel Processing, and Virtual Machines
○ 15.2: Boolean Algebra and Logic Circuits
● Topic 16: System Software
○ 16.1: Purposes of an Operating System
○ 16.2: Translation Software
● Topic 17: Security
○ 17.1: Encryption, Encryption Protocols, and Digital Certificates
● Topic 18: Artificial Intelligence
○ 18.1: Artificial Intelligence
● Programming
○ Programming Languages
○ Searching
○ Sorting
○ Recursion
○ Big-O Notation
○ Text File Handling
○ Abstract Data Structures
■ Queues
■ Stacks
■ Linked Lists
Topic 13: Data Representation

13.1: User-defined Data Types


- Non-Composite Data Types
- Defined without referencing another data types in type definition
- Includes primitives built into programming languages (integers, characters, etc.)
and user-defined data types
- Used to refer to a single value at any given time, which cannot be broken down
into smaller components
- Primitive, Enumerated, and Pointer data types
- Primitive
- Example:
DECLARE Age: Integer
Age ← 23
- Enumerated
- Does use a specific data type
- Consists of a list of possible values that can be used
- Adding 1 allows us to iterate across possible values
- Typical problems include defining enumerated data types rather than any
complex operations
- Example:
TYPE TColors = (Red, Blue, Green)
DECLARE theColor : TColors
theColor ← Blue
theNextColor ← theColor + 1
- Pointer
- Used to reference a memory location
- Stores an address in memory of data rather than the data itself
- Requires the data type of the data at the memory location being
referenced
- Example:
DECLARE Age: Integer
TYPE IntegerPointer = ^Integer

DECLARE agePointer : IntegerPointer


// Note: Memory address of Age assigned to agePointer
agePointer ← ^Age

DECLARE newAge
// Note: Data at address of age is accessed
// Useful if value of Age is changing
newAge ← agePointer^
- Composite Data Types
- Can refer to other data types in type definition
- Includes Records, Sets, and Classes/Objects
- Records
- Example:
TYPE Student
DECLARE Name: STRING
DECLARE Id: STRING
DECLARE BirthDate: String
DECLARE GPA: REAL
ENDTYPE

DECLARE Student1 : Student


// Note: Create an array of Student records
DECLARE SArray : ARRAY[1:30] OF Student

Student1.Name ← “John Ali”


Student1.Id ← “X387F”
Student1.BirthDate ← 30/05/2012
Student1.GPA ← 3.21
- Sets
- Unordered list of a values of a certain data type
- Values cannot be repeated - only consists of unique values
- Operations such as intersection, union, etc. can be performed on them
- Example:
TYPE Grades = SET OF Integer
DEFINE gradeRecord (99, 81, 83, 84) : Grades
- Classes
- Terminology
- Instance - an occurrence of an object; a specific object based on
the class; an instantiation of a class
- Properties - Attributes of an object, representing its state or data,
accessed through getters/setters
- Methods - Functions of an object that define its behavior and
operate on its properties
- Inheritance - the capability of defining a new class of objects that
has all the attributes and methods from a parent class.
- Polymorphism - allows the same method to take on different
behaviors depending on which class is instantiated; methods can
be redefined for derived classes.
- Encapsulation - putting properties and methods inside a class //
ensures sensitive data is hidden from users by hiding values of a
structured data object inside a class.
- Getter – method that is used to return the value of a property.
- Setter – method that is used to update the value of a property.

Example

// Note: Create a class called “Car”


CLASS Car
// Note: Declare class variables/attributes with access modifiers (i.e. PUBLIC, PRIVATE,
PROTECTED, etc.)
PRIVATE Make: STRING
PUBLIC SerialNumber: INTEGER
// Note: Create the constructor
PUBLIC PROCEDURE (CarMake:STRING, CarSerialNumber:
INTEGER)
// Note: Initialize class variables
Make ← CarMake
CarSerialNumber ← RAND(9999)
ENDPROCEDURE
// Note: Declare a class method (doesn’t contain method body)
PUBLIC GetMake() RETURNS STRING
ENDCLASS

13.2: File Organisation and Access


- File Organization
- Serial File Organization
- Records are stored in a file in the order that it was added
- Records are found by searching through the file one-by-one (linear search)
- File can be directly edited
- Sequential File Organization
- Records are stored in a file in order, usually based on a key field
- New version of file must be created to update a file (add or remove a
record)
- Records are found by searching through the file one-by-one (linear search)
- Random File Organization
- Record is stored in any available positions in a file
- Location of record found by inputting the record’s key field into a hashing
algorithm
- Hashing algorithm used to both place and access records
- File can be directly edited
- File Access
- sequential access - linear search; go through records from beginning until record
is found or EOF (end-of-file)
- direct access - a method to find a specific record without accessing any other
record (i.e. hash algorithms)
- Hashing Algorithms and Collision
- collision - when a hash algorithm produces a storage location that is already
being used
- Collision can be dealt with using open hashing and closed hashing when either
storing or accessing records
- Closed Hashing
- Storage - Instead of storing a single value at an index generated by the
hashing algorithm and key, a linked list is stored at this index, containing
the keys matching value.
- Retrieval - The key is put into the hashing algorithm to obtain and access
an index. The list at this index is traversed until the key and accompanying
data are found.
- Open Hashing
- Utilizes an extra overflow area
- Storage - When collision occurs, record is stored as a key and value in the
overflow area
- Retrieval - If hash algorithm produces an index and the data at the index
does not match the key, the overflow area is searched until a match is
found.

13.3: Floating-point Numbers, Representation, and Manipulation


- Binary Floating-Point Number Representation
- Can represent a much wider range of values than simple binary numbers
- Can represent fractions and negative numbers
- The standard for floating point computation in computer programs
- Can be more efficient for complex calculations, such as those in the spheres of
science, engineer, and 3D graphics
- Normalization
- Negative Numbers
- Mantissa starts with 10 (1.0)
- Add to the exponent
- Positive Numbers
- Mantissa starts with 01 (0.1)
- Subtract from the exponent
- To store the maximum range of numbers in the minimum number of bytes / bits
- Precision / accuracy of the number for the given number of bits
- Enables very large / small numbers to be stored with accuracy.
- Avoids the possibility of many numbers having multiple representations.
- Normalizing Positive Floating-Point Numbers
- Example:
Mantissa: 0000000111
Exponent: 100111

Step 1: Change this to “scientific notation” format.


0.000000111 * 2100111

Step 2: Change the exponent to denary.


0.000000111 * 239

Step 3: Change the exponent such that the mantissa is in the format of a positive
floating number (0.1xxxxxxx)
0.11100000 * 233

Step 4: Convert the new exponent back to binary.


0.000000111 * 2100001

Normalized Mantissa: 011100000


Normalized Exponent: 100001

- Normalizing Negative Floating-Point Numbers


- Example:
Mantissa: 11101100
Exponent: 00001010

Step 1: Change this to “scientific notation format.”


1.1101100 * 200001010

Step 2: Change the exponent to binary.


1.1101100 * 210

Step 3: Change the exponent such that the mantissa is in the format of a positive
floating number (xx1.0xxxxxxx)
111.01100 * 28

Step 4: Remove any extra digits beyond the first digit on the left side of the
decimal point.
1.01100000 * 28

Step 5: Convert the new exponent back to binary.


1.01100000 * 200001000
Normalized Mantissa: 101100000
Normalized Exponent: 00001000

- Converting Denary to Positive Floating-Point Numbers


- Example:
Mantissa: 12 bits
Exponent: 4 bits
Denary Number to Convert: 65.25

Step 1: Convert the decimal into an improper fraction.

Step 2: Multiply the improper fraction by ½ until the denominator of the fraction is
greater than the numerator.

Step 3: The number of times we have to multiply the improper fraction by ½ such
that the denominator is greater than our numerator becomes our exponent. Once
we find this exponent, we must also convert it to binary.

Step 4: We must find the fewest multiples of ½ that can be added together to
give us our result from Step 2.
Step 5: We are working with a positive decimal number, so the first value in our
mantissa will be 0, corresponding to 20. This is the only value to the left of the
decimal print. We will fill in all the other values in the mantissa to the right of this
first value based on the result from Step 4.

Mantissa: 010000010100
Exponent: 0111

- Converting Denary to Negative Floating-Point Numbers


- Example:
Mantissa: 10 bits
Exponent: 6 bits
Denary Number to Convert: -7.25
Step 1: Convert the denary number into an improper fraction.
Step 2: Multiple the result from Step 1 by ½ until the denominator is greater than the
absolute value of the numerator.

Step 3: Count the number of times we had to multiply our improper fraction from Step 1
to achieve our result in Step 2, and then convert this value to binary.

Step 4: Because we are converting a negative floating point number, our mantissa will
start with 0 and the rest of the mantissa will be the difference between -1 and the result
of Step 2.

Step 5: We must find the fewest multiples of ½ that can be added together to give us our
result from Step 2.
Step 6: We use these multiples of ½ to fill in the rest of the mantissa.

Mantissa: 1000110000
Exponent: 000011

- Converting Floating-Point Numbers to Denary


Mantissa: 1011000111
Exponent: 000111

Step 1: Write the mantissa and exponent in “scientific notation” format.


1.011000111 * 2000111

Step 2: Convert the exponent to binary.


1.011000111 * 27

Step 3: Move the decimal point to the right the same number of times as the exponent.
The exponent then becomes 0, which is equal to 1.
Step 4: Calculate the binary value of the resulting mantissa to get your answer.

Answer: -78.25

- Converting Unsigned Binary Numbers to Floating Point Numbers


- Example:
Mantissa: 12 bits
Exponent: 4 bits
Unsigned Binary Value: 1011100.011001

Step 1: Due to the fact that the binary value is unsigned, assume that the
mantissa will be positive and set up the mantissa accordingly.

Step 2: Move the decimal point in the binary value enough places to the left such
that the result follows the format 0.1xxxxxxxxxxx.

Result: 0.1011100011001 * 27
Step 3: The number of times you move the decimal point will be the value of your
exponent, which you can convert to binary.
Step 4: Given the number of bits allocated to the mantissa, remove any excess
bits on the right side of the mantissa.

Mantissa: 010111000110
Exponent: 0111

- Overflow and Underflow


- overflow - calculation produces a number larger than the maximum value that
can be represented in the given floating-point format
- Loss of precision
- Excess values cut off (truncated)
- underflow - occurs when a calculation produces a number closer to zero than the
smallest positive value that can be represented in the given floating-point format
Topic 14: Communication and Internet Technologies

14.1: Protocols
- Protocols
- common standard for communication between some group of devices
- allow devices made by different manufactures to communicate with each other
- All data between transmitted between protocol-compatible device must follow
same set of rules
- Protocols as a Stack
- Structured in layers, with each layer responsible for a part of the communication
process
- Data packets transferred one-by-one from top layer to bottom layer before being
set sent off
- Each layer, which corresponds to specific rules and software to validate these
rules, operates independently
- TCP/IP Protocol
- Enables computers to communicate over the internet and other networks
- Application Layer
- User Interface - where the user submits data to be transmitted via web
browser, etc.
- Security - data is encrypted/secured for transmission
- Translation - data is translated/formatted for transmission over network
- Transport Layer
- Segmentation - divides data into data packets
- Sequencing - decides order in which packets will be transmitted, adds this
information to packet header
- Received data from Application layer, sends packets to Network later
- Internet (Network) Layer
- Decides optimum route through which packets will travel
- Addresses packets with their source and destination IP address
- Transmits packets down to Network Access (Physical/Data Link) layer
- Network Access (Physical/Data Link Layer)
- Turns data into raw signal to be transmitted over network
- Controls rate at which data is transmitted
- Responsible for physical connection (wifi, ethernet, etc.)
- Other Protocols
- HTTP - used for sending and receiving webpages
- FTP - used for sending and receiving files over networks
- SMTP - used for sending emails from a mail server using push protocol
- POP3 - used for receiving and downloading emails from a mail server using pull
protocol (users must make request to see new emails)
- IMAP - used for receiving and downloading emails from a mail server using
push protocol (emails are automatically delivered to user)
- BitTorrent - peer-to-peer (P2P) file-sharing protocol used for distributing large
files across the internet
- swarm downloading - files are broken down into different pieces, which
can be stored with different users, but downloaded and assembled back
together by a single user

14.2: Circuit Switching, Packet Switching


- Packet Switching
- A method by which data is transmitted across networks, including the Internet
1. A file to be transmitted is divided into chunks called data packets.
2. The packet is given a header with information about its path and a payload, which
contains the actual data to be transmitted.
3. Each packet is sent into the network independently and may take a different path
to its destination computer (indicated by an IP address)
4. Packets arrive out of order, but are reassembled according to a sequence number
included in the header.
5. If any packets are missing, a request for retransmission is sent back to the
destination IP address.
- Routers and Packet Switching
- Analyzes packet header
- Uses knowledge of status of network and destination to calculate current
optimum route and send packet to next appropriate router
- Packet Switching: Advantages and Disadvantages
- Advantages
- Security - packets hashed and sent by different routes
- Resilience - if a router is down or network is damaged, packets can
simply change routes
- Completeness - missing packets can easily be detected (via sequence
number) and a resend request made to IP address of origin
- Disadvantages
- Network problems may lead to errors in data packets
- Complexity
- Circuit Switching
- A method by which data is transmitted over networks using an exclusive,
dedicated path for a transmission between two devices
- Used with landline telephones - usage rapidly declining
1. Dedicated path established between sender and receiver
2. Resources, including necessary bandwidth are allocated to circuit, which is used
in its entirety
3. Once path is established, data can be transferred back and forth between sender
and receiver
4. Session is terminated and route is once again free for communication between
other senders and receivers
- Circuit Switching: Advantages and Disadvantages
- Advantages
- Consistency - Data frames arrive in order and therefore, do not need to be
reassembled
- Speed - No delay in data transmission
- Simplicity
- Disadvantages
- Less secure - same route used for all data frames that make up a
message
- Inefficient - resources are allocated and route is reserved regardless of
whether data is activelybeing transmitted
- Packet Switching vs. Circuit Switching
Topic 15: Hardware and Virtual Machines

15.1: Processors, Parallel Processing, and Virtual Machines


- Processor Architectures (CISC and RISC)
- CISC - Complex Instruction Set Computer
- RISC - Reduced Instruction Set Computer
- Processor design affect how instructions must be framed and are executed by
processors
- Different architectures have different types and sizes of instructions sets (the set
of all commands that can be executed by the CPU)
- Architecture can lead to certain operations being executed much more quickly
than others.
- CISC vs. RISC

- RISC + Pipelining
- Allows several instructions to be simultaneously processed
- Processing cycle divided into several stages (Fetch, Decode, Execute, Memory
Access, Results written to register)
- While one instruction is in one stage, the next instruction is in the previous stage
- At each clock cycle, multiple instructions are going through different stages
- Reduces CPU downtime, more instructions processed in a given period of time,
instructions executed more quickly
- RISC + Registers
- Use general purpose registers - can hold multiple data types
- Reduces need to fetch data from memory, which decreases execution time
- Can be used as temporary storage for intermediate results during execution,
which makes pipelining easy
- (Massively) Parallel Computing
- When a large number of computers/processors are connected to each other to
collaboratively complete a large task
- Pass messages to each other to coordinate operations
- Connected by some type of network infrastructure
- On a massive scale, hundreds or thousands of computers can work together
- 4 Basic Computer Architectures
- Refers to how multiple processors are organized to collectively process
instructions
- SISD (Single Instruction, Single Data) - Single instruction used to process single
data input by single processor, one at a time [Most common]
- SIMD (Single Instruction, Multiple Data) - Same operation simultaneously
performed by multiple processors on different data inputs from the same data
set [Graphical applications]
- MISD (Multiple Instruction, Single Data) - Multiple different operations on the
same data input on multiple processors
- MIMD (Multiple Instruction, Multiple Data) - Multiple different operations on data
from multiple different data inputs simultaneously [Multicore systems]
- Virtual Machines
- Emulates a computer system on another host computer system, usually with a
different operating system
- For example, using a virtual machine, you could run a fully functional Linux
system, on a Windows PC
- Components like hard drive, graphic card, memory are all virtual
- Software like Parallels, Boot Camp, VMWare, make this possible
- Advantages & Disadvantages of Virtual Machines

15.2: Boolean Algebra and Logic Circuits


- Boolean Algebra
- Basic Rules:
Example 1
- Truth Table Example:
- We can generate products based on the inputs in a truth table that result
in an output of 1.
- We add these products together to give us a sum of products. This is the
answer to the question below.

- Karnaugh Maps (K-Maps)


- Used to simplify Boolean expressions
- Rules
- We make groups only of 1s
- We make groups only of of 2n size
- Groups can be a column, row, or rectangle only, no diagonals
- Groups can overlap, but should as little as possible
- The fewer groups, the better - groups should be as big as possible
- K-Map Example:
- Logic Circuits
- Half Adder and Full Adder
- Half Adder - XOR and AND, used to add two 1-bit binary numbers

- Full Adder - Used to add three 1-bit binary numbers


- We need to be able to generate truth tables for these circuits and identify them
from truth tables.
Topic 16: System Software

16.1: Purposes of an Operating System


- Role of UI (User Interface)
- UI used to hide complexities of computer hardware from the user
- How is this done?
- Usage of Graphical User Interfaces (GUIs) rather than Command-Line
Interfaces (CLIs)
- Use of device drivers (make setting up and using hardware like mouses
and printers easy)
- Tasks carried out in the background
- Make data transfer and retrieval from RAM, secondary storage devices,
and external storage devices as seamless as possible
- Processes
- process - a program or task currently running on a program’s CPU (Central
Processing Unit)
- a process is launched when a new program is launched
- processes do not interfere with each other
- processors have their own space in RAM
- multitasking - when multiple processes are running at the same time
- Process Control Block (PCB)
- a data structure used by the OS to manage and maintain information about
processes
- Role
- Switching between processes
- Running multiple processes (multitasking)
- General process-related operations.
- Process States
- Refers to current status of a process
- 3 possible states: running, ready, blocked
- running - program currently running on CPU
- ready - CPU waiting to run next program
- blocked - CPU is running a process, but is waiting for an individual event to
complete before it can proceed (i.e. waiting for input from the user); process is
temporarily suspended
- Scheduling
- scheduling - the process by which the operating system and determines the order
in which tasks or processes are executed on the CPU
- Scheduling involves modifying individual process data in the PCB (process
control block).
- Resolves conflicts between two processes that need to use the same resource
- Order is determined algorithmically.
- Purpose of Scheduling
- Enables multitasking
- Makes sure CPU is busy at all times
- reduced wait times
- more efficiency
- Allows certain jobs to be prioritized
- Scheduling Routine Algorithms
- Round Robin - every process is executed for the same amount of time; if
not finished it is blocked and placed in a queue, which is continually being
served
- Benefits: Each process will be served, same priority for all
processes
- Shortest Job First - Processes executed in ascending order of CPU time
required; Shortest tasks executed first
- Benefits: More processes can be executed in a shorter amount of
time
- First Come First Served - Each process enters a queue and is executed
one by one
- Benefits: No complex logic, less processor overhead
- Shortest Remaining Time - execution time of new process compared with
remaining time of currently running process - if shorter, process executed
first
- Benefits: Higher throughput, more processes can be executed in a
given amount of time
- Interrupts & Interrupt Handlers
- interrupts - a combination of software and hardware components that can be
triggered to prioritize a process for immediate execution
- Examples
- Printer paper jam
- Keyboard press by user
- Software error/malfunction
- Infinite loop
- interrupt handling - piece of code in the CPU that knows how to handle interrupts
- Current state of CPU saved, interrupt process executed, operations resume as
normal
- Kernel
- OS component
- Intermediary between hardware and software layers of computer
- Responsible for managing interrupt handling
- Steps in kernel response to interrupts
1) Detects and acknowledges interrupt
2) Saves context (state) of currently running process in CPU (includes data
in registers, program counter, etc.)
3) Determines priority of interrupt (compared to other interrupts)
4) Identifies and transfers control to Interrupt Service Routine (ISR), a block
of code responsible for handling a specific type of interrupt.
5) Interrupt (s) are executed.
6) Kernel restores CPU context and instructs CPU to control normal
operations.
- Virtual Memory (Logic Memory)
- When RAM is overloaded, data is temporarily sent to secondary storage.
- This data is stored in the virtual memory, a section of secondary storage.
- Data is transferred back to RAM from virtual memory, when there is free space.
- Only data in use by currently running programs needs to remain in the RAM.
- Accessing data in virtual memory takes more time than accessing data in RAM.
- Paging + Virtual Memory
- OS manages the virtual memory feature by transferring pages of data from RAM
to secondary storage when RAM is overloaded.
- Allows processes to use a larger virtual-address space than physical memory
available, enabling processes to utilize data stored in virtual memory.
- Memory Management: Paging
- Memory is split up into blocks (partitions) of a fixed-size
- Blocks can correspond to different programs
- Physical memory (RAM) uses paging and is split into frames.
- Logical memory (virtual memory) is split into pages which correspond to a
specific frame, as they are the same size.
- Program allocated number of pages slightly more than what is needed
- Page table used to keep track of pages in virtual memory and recall the correct
pages.
- Memory Management: Segmentation
- Memory divided into variable-sized segments.
- Works similar to paging.
- Can be used in virtual memory, but this is less efficient.
- Uses segment tables for mapping segments to processes, similar to paging
tables.
- Compiler (which compiles source code in programs) responsible for allocating
data into segments and calculating necessary segment size
- Paging vs. Segmentation

- Data Thrashing
- a situation that occurs in computer systems when the operating system spends a
significant amount of time and resources repeatedly swapping data between
physical memory (RAM) and virtual memory
- Causes
- Insufficient RAM
- Overcommitting memory - total amount of virtual memory exceeds
physical memory
- Excessive multitasking
- Poor memory management - inefficient swapping algorithms
- Effects
- Data loss/corruption
- High disk activity - can reduce life of hard drive
- Reduced multitasking capability

16.2: Translation Software


- Interpreters
- Input source code (Python, Javascript, PHP, etc.)
- Executes source code line-by-line
- Execution = conversion to machine code and execution by CPU
- If there are any errors during execution, execution stops and an errors message is
output
- Interpretation done every time a program is run
- In loops, the repeated section is interpreted for each iteration
- Compilers
- Takes source code (C++, Java, Swift, etc.) as input
- Converts source code to object code
- Object code is in binary and is then executed by the CPU
- Object code may also be in intermediate form, which is later converted to binary
before execution.
- Interpreters vs. Compilers

- Stages of Compilation
1) Lexical Analysis - converting a sequence of characters into a sequence of tokens
2) Syntax Analysis - using parsing algorithms to interpret the meaning of a
sequence of tokens
- Checks if code follows the rules of the programming language
3) Optimisation - changing code to minimizing a program’s execution time and
memory usage
4) Code Generation - converting an intermediate representation of source code into
an executable form
- Lexical Analysis
- converting a sequence of characters into a sequence of tokens
1) All characters not required for the program are removed (white space, comments,
etc.)
2) Source code needs to be converted to tokens (tokenization).
- Each keyword or symbol can be matched to a string or number (token)
using a keyword table
- We end up with a sequence of these values.
- Any variables, constants or other identifiers (function, class names) are
added to the keyword table during compilation to aid tokenization
- Backus-Naur Form
- A set of symbols and rules that are used to describe the syntax of a programming
language
- Syntax rules can also be described using syntax diagrams
Syntax Diagram Example

Syntax Rules and BNF Notation

A variable first consists of a letter, and then an unsigned integer, in that order.

<variable> ::= <letter><unsigned_integer>

An unsigned integer is either a single digit, or two digits put together (concatenated).

<unsigned_integer> ::= <digit>|<digit><digit>

A digit is either 1, 2, or 3.
<digit> ::= 1 | 2 | 3

An operator is either the +, -, or * symbol.

<operator> ::= + | - | *

An assignment statement consists of a variable, followed by an equal sign, a variable, an


operator, and then another variable. Both the variable and operator must fit the criteria
mentioned above.

<assignment_statement> ::= <variable> =


<variable><operator><variable>

Syntax Rules Chart

Symbol Meaning Example

::= “Defined as” <digit> ::= 0|1|2|3

| “or” <digit> ::= 0|1|2|3

Syntax Diagram Example 1

Syntax Rules

1. One or more letters


2. 0, 1 or more than one digit

BNF Notation

identifier ::= <letter>|<letter><digit>


Syntax Diagram Example 2

Syntax Rules

1. 1 characters
2. A digit, multiple digits, a capital letter followed by one or more digits and/or capital
letters, or a digit followed by one or more digits/capital letters

BNF Notation

password ::= <character><code>

code ::= <digit>|<capital_letter><code>|<digit><code>

- Reverse Polish Notation (RPN)


- Method of representing arithmetic or logical operations without the use of
parentheses or other symbols to indicate the order of operations
- Uses postfix notation: operator comes after variable or value it acts on
- Used by compiler to evaluate mathematical expressions
- Example 1 (Arithmetic Operation to RPN) :

(a - b)* (a + c) / 7

Step 1: Each individual operation is converted into both the relevant variables,
followed by the relevant operation. We start with parentheses and work our way
outwards.

ab- * ac+ /7

Step 2:

ab- ac+ * /7
Step 3:

ab- ac+ * 7/

- Example 2 (RPN to Arithmetic Operation) :

ab / 4 * a b + -

Step 1

(a/b) 4 * (a + b) -

Step 2

((a/b) * 4) - (a + b)

- Example 3 (RPN to Arithmetic Operation):

ab + cd / /

(a + b) / (c / d)

- RPN: Benefits
- Can be used to evaluate expression from left to right rather than skipping around
- Doesn’t require parenthesis to be taken into account
- Doesn’t require order of operations to be taken into account
Topic 17: Security

17.1: Encryption, Encryption Protocol, and Digital Certificates


- Key Cryptography
- Ensures message is authentic
- Ensures message came from trusted source
- Ensures message wasn’t altered during transmission
- Makes sure only the intended recipient can understand message
- Involves keys - strings of alphanumeric text used to decrypt and encrypt
messages
- Symmetric Cryptography
- Requires text to be encrypted (plaintext) , a secret key, and an encryption
algorithm
- secret key - random alphanumeric string
1) Input the secret key, and plain text into the encryption algorithm to get your
encrypted text, which should appear as a random set of numbers and letters
(ciphertext).
2) Send the ciphertext to the recipient, who should already have the same secret key
and decryption algorithm.
3) The secret key and cipher text are input into the decryption algorithm to yield the
original plaintext
- Main problem is securely sharing key between sender and recipient (key
distribution problem)
- Asymmetric Cryptography
- Both sender and receiver generate private and public keys using an algorithm.
The public key can only be generated and verified using the private key.
- public key - alphanumeric text, shared with others
- private key - alphanumeric text, kept to oneself
- The intended recipient sends their public key to the sender, which the sender
uses to encrypt their message into ciphertext.
- The sender sends the ciphertext to the recipient, who can use their private key to
decrypt the message, which was encrypted with their own public key.
- If the sender wants to receive a message from the recipient, they need to
complete the same process, first sending their own public key.
- Symmetric vs. Asymmetric Cryptography

- Quantum Cryptography
- Information is represented in qubits, which can be 1, 0, or both 1 and 0 at the
same time
- Each qubit is represented by a photon (unit of light energy) pointing in a specific
direction based on its value
- Photons travel between a sender and recipient via fiber optic cable.
- Also requires a non-quantum “classical” mode of electronic communication
- Data is transmitted as a stream of photons, all in a particular order and
orientation.
- Any attempt to access this data while being transmitted alters the message.
- It is theoretically impossible to intercept a transmission protected by quantum
cryptography.
- Quantum Cryptography: Advantages & Disadvantages

- Protocols (SSL/TLS)
- SSL - Secure Sockets Layer
- TLS - Transport Layer Security
- Process used by web browser to encrypt communication between browser and a
server
- Confirms identity of client and server
- Prevents hackers from intercepting traffic between client and server
- TLS is newer and more secure than SSL
- SSL/TLS Steps
1) Client (on a web browser) tries to access a website which uses SSL/TLS
(a handshake)
2) Websites’ server receives connection and responds with a digital
certificate, which contains a public key and is validated by a certificate
authority.
3) The client’s browser validates the authenticity of the digital certificate.
4) The browser generates a key for the current session using the public key,
which is sent back to the server.
5) The server uses its private key (which is used to generate the public key)
to verify the session key.
6) Any further communication between the client’s browser and server is
encrypted (hashed) through the use of this session key.
- Digital Signatures
1) The sender uses a mathematical function called a hash function to generate a
hash value (also known as a message digest).
2) The result is an alphanumeric value of a fixed-length, which is then encrypted
using a public key.
3) This encrypted hash value is the digital signature and is then attached to
unhashed version of the message to be sent
4) The message is sent to the recipient.
5) The recipient decrypts the signature using their private key and puts the message
they received through a hash function.
6) The recipient compares the contents of the decrypted signature and the hashed
message and if they are equal, the transmission is authentic.
- Digital Certificates
- Verifies the identity of a website
- Contains website’s public key, identifying information, Certificate Authority
information + CA private key, validity period, etc.
- Browser can verify digital certificate using CA public key
- Obtaining a Digital Certificate
1) A website makes an inquiry to a Certificate Authority (CA).
2) The CA investigates the website and any associated entities.
3) The CA verifies the identity and security of the website and issues a public key.
4) The website includes this public key in a certificate sent to a client attempting to
connect using SSL.
5) The client (browser) verifies the certificate using the CAs digital signature in the
certificate and communicates with the website by encrypting data with this
public key.
Topic 18: Artificial Intelligence

18.1: Artificial Intelligence


- AI (Artificial Intelligence) - a system that attempts to solve problems by simulating how
the human brain works
- Supervised Learning
- Unsupervised Learning
- Reinforcement Learning
- Reinforcement learning is similar to supervised learning, except that the model
receives its rewards in real-time.
- The algorithm is given a goal as well as a range of actions for the program to
achieve that goal.
- Every action is given a score, given on how close it comes to achieving the goal
of the algorithm. This is dictated by a mathematical function and pushes the
algorithm to try different approaches until the maximum score is achieved.
- Neural Networks
- Parts of a Neural Network
- Input Layers - the input layer accepts data either for training purposes or
to make a prediction
- Hidden Layers - the hidden layers are responsible for actually deciding
what the output is for a given input; this is also where the “training” occurs
- Output Layers - outputs the final prediction
- Deep Learning
- Deep learning is a type of machine learning that makes use of neural networks,
but involves utilizing many more hidden layers than the average neural network.
- More hidden layers means that data can be analyzed with more granularity and
more complex problems can be solved.
- Meant to simulate the human brain
- Machine learning in general can make use of statistical algorithms like linear
regressions, k-Nearest Neighbor, etc. while deep learning specifically focuses on
large neural networks.
- Dijkstra Algorithm
- Involves calculating the distance to the closest points (vertices), then the second
closest vertices and so forth.
- Once we are done, we can find the shortest path to the target vertex.
Example

- A* Algorithm
- Like Djikstra algorithm, but with heuristic (h) value - predicted distance to goal
- heuristic value (h), and movement value (g), and the sum of these two (f)
- Find the first node with the lowest f
- From the first node, find the next node that produces the lowest f.
- Keep doing this until you get to your objective.
Example
Basically the idea here is that the cost to move from the home node to any given node is the
cost (g). The heuristic is the expected distance from any given node to the ultimate destination,
which is the School node in the above example.

In the problem above, basically you had to calculate first the sum of the cost and heuristic for
routes that minimize the total and are possible routes for approaching the destination node (i.e
School node). Eventually, this will lead you to routes directly to the destination node without a
heuristic), of which you are expected to find the route with lowest cost from the home node.

This is basically an iterative process by which you get closer and closer to the destination and
show cost, heuristic and total of these in the process.
Programming

Programming Languages
- Imperative Programming Languages
- use variables that are modified through assignment statements and rely
on iteration
- provide a sequence of commands for the computer to execute in a
specific order, with each line of code altering the program's state.
- Example: Python, Java, etc.
- Declarative Programming Languages
- specify what needs to be done rather than how to do it, using facts, rules,
and queries to achieve goals.
- can be either logical, stating programs as sets of logical relations, or
functional, constructed by applying functions to arguments in a
mathematical style.
- Used to store information and then recall it
- Examples: SQL, Prolog
- On Paper 3, examples of declarative programming are written using a
variant of Prolog, as shown below
- Code Example:
// Information
student(John)
student(Ali)
student(Valeria)
student(Alice)
student(Vijay)
// Relationships
subject(John, Mathematics)
subject(John, Computer Science)
subject(Ali, Mathematics)
subject(Valeria, Computer Science)
subject(Alice, Biology)
subject(Vijay, Biology)
Query:
// Queries subject relationships
// We follow the same order in which data is presented in the subject
relationships - students and then subject
// Expected output is a students/students, hence Student is written instead
of the name of a specific student and the first letter is capitalized
subject(Student, Mathematics)
Output:
John, Ali
Query:
subject(John, Subject)
Output:
Mathematics, Computer Science

Searching
- Linear Search
- Progress from beginning to end of array and check whether each element is
equal to the target value

Example

DECLARE MyList : ARRAY[0:9] OF INTEGER


DECLARE MaxIndex : INTEGER
DECLARE Index : INTEGER
DECLARE Found : BOOLEAN
DECLARE ValueToFind : INTEGER

INPUT ValueToFind
Found ⟵ FALSE
Index ⟵ 0
MaxIndex ⟵ LENGTH(MyList) - 1

REPEAT
// Note: Check every single element in an array until you find the target value
IF MyList[Index] = ValueToFind THEN
Found ⟵ TRUE
ENDIF
Index ⟵ Index + 1
UNTIL Found OR Index > MaxIndex

IF Found THEN
OUTPUT "Value found at position ", Index
ELSE
OUTPUT "Value not found"
ENDIF

- Binary Search
- Can only be used with an array that is already sorted (by ascending or
descending order)
- Process (assuming an array is used)
1) Compare the target value with the value of the middle element of the
array.
2) If the values match, the value being searched for was found.
3) If the target value is less than the middle elements of the array, then the
action repeats for the value (sub-array) to the left of the middle element.
4) If the target value is greater than the middle element, it repeats on the
right of the middle element.
5) If the remaining array to be searched is empty, the target value was not
found.

Binary search with target value of 6

Example

// Note: Index of first element of array to be searched


Lower ← 0
// Note: Index of last element of array to be searched
Upper ← 99
// Note: Index of current element of array to be compared against target value
Mid ← 0
Exit ← FALSE
OUTPUT "Enter the name to be found"
INPUT Target
REPEAT
IF Upper < Lower THEN
OUTPUT Target, " does not exist"
Exit ← TRUE
ENDIF
// Note: Access the element directly in the middle of the Upper and Lower indices
Mid ← Lower + (Upper - Lower + 1) DIV 2
// Note: If currently accessed element is lower than the target, move the Lower index
upwards, above the middle value.
IF Names[Mid] < Target THEN
Lower ← Mid + 1
ENDIF
// Note: If currently accessed element is greater than the target, move the Upper index
downwards, below the middle value.
IF Names[Mid] > Target THEN
Upper ← Mid - 1
ENDIF
// Note: If currently accessed element is equal to the target, we’re done.
IF Names[Mid] = Target THEN
OUTPUT Target, " was found at location ", Mid
Exit ← TRUE
ENDIF
UNTIL Exit // UNTIL Exit = TRUE

Sorting
- Factors for Sorting Algorithm Performance
- The initial order of the data
- The number of data items to be sorted
- The efficiency of the sorting algorithm
- Bubble Sort
- Process
- Goes through the array and compares each pair of elements.
- If ascending, swaps elements where the first element is greater than the
second. Otherwise, if descending, swaps elements where the first is less
than the second.
- Repeats the process, iterating through the entire array as many times as
is necessary until the array is sorted.
- Array will be traversed a maximum of n - 1 times, where n is the length of
the array
Example
Assume the existence of an array called Score with 250 integers that represent the scores of
250 students
NumberOfStudents ← 249
Flag ← TRUE
WHILE Flag = TRUE
Flag ← FALSE
FOR Student ← 1 TO NumberOfStudents - 1
IF Score[Student] < Score[Student + 1] THEN
Temp1 ← Score[Student]
Score[Student] ← Score[Student + 1]
Score[Student + 1] ← Temp1
Flag ← TRUE
ENDIF
NEXT Student
ENDWHILE

- Insertion Sort
- Steps
1. Start from the second element and consider it as the key.
2. Compare the key with elements in the sorted portion of the array.
3. Move elements that are greater than the key one position to the right.
4. Insert the key into the correct position.
5. Repeat until the entire array is sorted.

Example
Assume the existence of an array called Score with 250 integers that represent the scores of
250 students
NumberOfStudents ← 249
FOR Student ← 2 to NumberOfStudents
Temp ← Score[Student]
Counter ← Student
WHILE Counter > 1 AND Score[Counter - 1] < Temp
Score[Counter] ← Score[Counter - 1]
Counter ← Counter - 1
ENDWHILE
Score[Counter] ← Temp
NEXT Student
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 changes its state and moves towards the base case; unwinding
occurs once the base case is reached

Example

FUNCTION factorial(n : INTEGER) RETURNS INTEGER


IF n = 0 THEN
RETURN 1
ELSE
RETURN n * factorial(n - 1)
ENDIF
ENDFUNCTION

factorial(4)→

return 4*factorial(3) = return 24

return 3*factorial(2) = return 6

return 2*factorial(1) = return 2

return 1*factorial(0) = return 1

factorial(0) = 1

So, factorial(1) = 1*1 = 1, factorial(2) = 2*1 = 2, factorial(3) = 3*2 = 6, factorial(4) = 24

Big-O Notation
- Used to describe the amount of time required for an algorithm to complete some task,
based on size
- Used to describe the efficiency of the searching process on the A-level exam
- In Big-O notation, “n” is the number of terms to be searched

Examples
- Linear Search - O(n) - time to search increases linearly (think x = y) relative the the
number of elements (n) in an array that must be searched for a specific value
- Binary Search - O(log(n)) - time to search increases logarithmically relative to the
number (n) of elements in an array that must be searched
- time to search increases less rapidly based on size than for linear search, making
it a faster solution in some, but not all cases
Text File Handling

Example 1

DECLARE Account : STRING


OPENFILE "DestinationFile.txt" FOR READ
OPENFILE "TargetFile.txt" FOR WRITE
// Note: EOF means “end of file” and allows us, with a while loop, to execute certain functions
until we reach the end of a text file.
// Note: READFILE and WRITEFILE functions only allow us to read and write multiple lines,
sequentially in a text file inside a while loop.
// Note: If not, we will just read the next individual line when we call READFILE or write to the next
individual line when we call WRITEFILE
WHILE NOT EOF("DestinationFile.txt")
// Note: Read text file individually line-by-line
READFILE "DestinationFile.txt", Account
IF Account = "" THEN
// Note: Write to the text file individually line-by-line
WRITEFILE "TargetFile.txt", "Account not present"
ELSE
WRITEFILE "TargetFile.txt", Account
ENDIF
ENDWHILE

CLOSEFILE "DestinationFile.txt"
CLOSEFILE "TargetFile.txt"

Example 2

DECLARE TextLine : STRING


OPENFILE "amith.txt" FOR READ
OPENFILE "destination.txt FOR WRITE
DECLARE Position : INTEGER

DECLARE NumberOfLines: INTEGER

WHILE NOT EOF("amith.txt")


READFILE "amith.txt", TextLine
OUTPUT TextLine
// Note: Keep track of the number of lines in the text file
NumberOfLines <- NumberOfLines + 1
ENDWHILE

CLOSEFILE "amith.txt"
OPENFILE "amith.txt" FOR READ
OPENFILE "destination.txt FOR WRITE

WHILE NOT EOF("amith.txt")


READFILE "amith.txt", TextLine
IF TextLine <> "" THEN
WRITEFILE "amith.txt", TextLine
ENDIF
ENDWHILE

Abstract Data Structures

Queues
- Dynamic Data Structure
- FIFO (First in First Out) - the first element to be “enqueued” (inserted) is the last to be
“dequeued” (removed) like a real-life queue
Example

Used to construct a queue from an array

CONSTANT MaxLength = 50
DECLARE FrontPointer : INTEGER
DECLARE RearPointer : INTEGER
DECLARE Length : INTEGER
DECLARE Queue : ARRAY[0:MaxLength - 1] OF STRING

// Note: Initialisation of queue


PROCEDURE Initialise
FrontPointer ← -1
RearPointer ← -1
Length ← 0
ENDPROCEDURE

// Note: Adding a new item string) to the queue


PROCEDURE Enqueue(NewItem : STRING)
IF Length < MaxLength THEN
RearPointer ← RearPointer + 1
// Note: Set RearPointer to leftmost element if is greater than the maximum
possible index
IF RearPointer > MaxLength - 1 THEN
RearPointer ← 0
ENDIF
// Note: We add the new item to the back of the queue
Queue[RearPointer] ← NewItem
Length ← Length + 1
ENDIF
ENDPROCEDURE

// Note: Remove an element from the front queue


FUNCTION Dequeue() RETURNS STRING
TargetItem ← “”
IF Length > 0 THEN
// Note: Array is static data structure, so when we remove an element, we simply
increase the index of the FrontPointer, which is 0 at first
TargetItem = Queue[FrontPointer]
FrontPointer ← FrontPointer + 1
Length ← Length - 1
ENDIF
RETURN TargetItem
ENDFUNCTION
Stacks
- Dynamic Data Structure
- LIFO (Last in First Out) - the last element to be “pushed” (inserted) into the stack is the
first to be removed (“popped”)
- Used to keep track of function calls
Example

Capacity: Maximum size of stack


TopOfStack: Pointer to top element of stack

CONSTANT Capacity = 25
// Note: Index of bottom element in stack
DECLARE BasePointer : INTEGER
DECLARE TopPointer : INTEGER
// Note: Array used to represent stack
DECLARE Stack : ARRAY[1:25] OF REAL

// Note: Popping an item from the stack


FUNCTION Pop() RETURNS REAL
DECLARE Item : REAL
Item ← 0
IF TopPointer >= BasePointer THEN
// Note: Get the element on the top of the stack and change the top to the next
lowest element in the stack by index. Ex: The top goes from index 5 to index 6
Item ← Stack[TopPointer]
TopPointer ← TopPointer - 1
ELSE
OUTPUT "The stack is empty - error"
ENDIF
RETURN Item
ENDFUNCTION

// Note: Check if stack is full - the number of elements in the representative array is equal to the
size of the array
FUNCTION StackFull() RETURNS BOOLEAN
IF TopOfStack = Capacity THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION

// Note: Add an integer to the top of the stack


FUNCTION Push(NewInteger : INTEGER) RETURNS STRING
IF StackFull() THEN
RETURN "The stack is full"
ELSE
// Note: Make the top of the stack the next highest free index in the array and store
the new integer here
TopOfStack ← TopOfStack + 1
Stack[TopOfStack] ← NewInteger
RETURN "Item added"
ENDIF
ENDFUNCTION
- Stacks vs. Queues

Comparison Point Stacks Queues

Order First in last out (FILO) / Last First in first out (FIFO)
in first out (LIFO)

Removal Order Data is removed in the Data is removed in the


reverse order to which it is reverse order to which it is
received received

Size Can vary in size / are of Can vary in size / are of


indeterminate length indeterminate length

Data Operation Data is popped and pushed Data is enqueued and


(onto/from a stack) at the dequeued (to/from a queue)
same end at different/opposite ends

Ends Has only one accessible end Has two accessible ends

Pointers Has only one moveable Has two moveable pointers


pointer

Linked Lists
- Consists of nodes that are linked to each other by “pointers”
- Head is first node in linked lists and tail is last node
- The tail’s pointer leads nowhere
- Each node contains of some data and a pointer, which is really the memory address of
the next node in the linked list and can be used to access that node

Example
A linked list can be represented on the A-Level exam by 2 arrays, like in the example below.

Index LinkedList NextPointer

1 James 7

2 Alice 10

3 Shi 1 (Head)

4 Muhammad 8

5 Ignacio 6

6 Sumit 0 (Tail)

7 Diego 2

8 Nina 5

9 Fatima 4

10 Sofia 9

// Note: Index where the linked list begins


DECLARE HeadPointer: Integer
DECLARE Found: Integer
DECLARE Pointer: Integer
// Note: Indices in the LinkedList that each “node” in LinkedList points to
// Note: NextPointer[2] holds the index that the “node” at LinkedList[2] points to
DECLARE NextPointer : ARRAY[1:10] OF INTEGER
DECLARE LinkedList: ARRAY[1:10] OF INTEGER

HeadPointer ← 3
Pointer ← HeadPointer
Found ← 0
// Note: Allows you to find a specific value in a linked list
OUTPUT "Enter a student name: "
INPUT StudentName
WHILE Pointer <> 0
IF LinkedList[Pointer] = StudentName THEN
Found ← Pointer
Pointer ← 0
ELSE
// Note: Use the indices in the NextPointer array to traverse the LinkedList and
move on the next node in the linked list
Pointer ← NextPointer[Pointer]
ENDIF
ENDWHILE

IF Pointer = 0 THEN
OUTPUT LinkedList[Found], " is found"
ELSE
OUTPUT "The student whose name you wanted is not in the list."
ENDIF

You might also like