Featured image of post Final Alevel CS Review Guidance

Final Alevel CS Review Guidance

A final CS note

Data Representation

User-defined data types

Non-composite types

  • enumerated
    • TYPE Courses = (English,Chinese,Maths)
  • pointer
    • TYPE SelectParts = ^Parts // It reference the memory location in which the part is stored

Define & Use composite data types

  • set, record and class/object
  • Pseudocode example of class:
1
2
3
4
5
TYPE School
    DECLARE StudentNumber : INTEGEAR
    DECLARE Courses : (English,Chinese,Maths)
    DECLARE StudentList : ARRAY[0:200] OF STRING
ENDTYPE

File organisation and access

Organisation

  • Serial

  • Sequential

  • Random

    • Only that the access can be to any record in the file without sequential reading of the file.
    • A separate index file is created which has two fields per record.
    • The first field has the key field value and the second field has a value for the position of this key field value in the main file.

Access

  • Sequential
  • Direct

Floating-point numbers, representation and manipulation

  • Overflow
  • Underflow
  • Rounding errors


Communication and internet technologies

Protocols

Importance

TCP/IP

Bittorrent

  • file sharing
  • peer-to-peer

Other protocols

  • FTP
  • IMAP, SMTP, POP3
  • HTTP
  • ……

Circuit switching, packet switching

Packet

Circuit

  • A method of data transfer in which the message is sent over a dedicated communication channel.

Hardware and Virtual Machines

Processors, Parallel Processing and Virtual Machines

Reduced Instruction Set Computers (RISC)

Complex Instruction Set Computers (CISC)

Parallel processing

  • SISD
  • SIMD
  • MISD
  • MIMD


Logic Circuits

Half-adder

  • 2 input
  • 2 output
    • carry bit
    • sum bit

Full-adder

  • 3 input
  • 2 output
    • carry bit
    • sum bit

Flip-flop

SR
  • It can be constructed with two NAND gates or two NOR gates.
  • Used as a storage device for 1 bit in the RAM, since it’s values can be altered
  • Issue: When the both the input signals are 1 (invalid state) the flip-flop sets the value of Q and Q’ to 0.

JK
  • The J acts as a set input and the K as a clear input.
  • There is a clock input to synchronise the inputs.
  • When both the input signals are one, Q toggles.

Usage
  • Flip-flops are used to build
    • Data storage elements
    • Digital circuits

Boolean Algebra


Karnaugh maps


System Software

Purposes of an Operating System (OS)

  • OS provides utility software, user interface (GUI,CLI), resources management, multi-tasking, a platform to run code……


Translation Software


BNF notation

  • ::=
  • |

RPN expression

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Origin expression:
0!+123+4*(5*6!+7!/8)/9

RPN expression:
0 ! 123 + 4 5 6 ! * 7 ! 8 / + * 9 / +

Proccess:
大致计算过程:
0入栈
!为运算符,计算栈顶0!,结果入栈
123入栈
+为运算符,将栈顶两元素相加,结果入栈
4入栈
5入栈
6入栈
!为运算符,将栈顶6进行运算,结果入栈
*为运算符,将栈顶6!和5相乘,结果入栈
7入栈
!为运算符,计算7!,结果入栈
8入栈
/为运算符,将栈顶两元素相除,结果入栈
+为运算符,将栈顶两元素相加,结果入栈
*为运算符,将栈顶两元素相乘,结果入栈
......

Security

Encryption, Encryption Protocols and Digital certificates

  • public key, private key, plain text, cipher text, encryption
  • symmetric key cryptography and asymmetric key cryptography

Symmetric key encryption


Asymmetric key encryption

Private keys & public key


Digital certificate


Digital signature


Quantum cryptography


SSL & TLS


How to establish a secure connection


Artificial Intelligence (AI)

Please check the note released before🔗


Computational thinking and problem-solving

Algorithms

Please check the note released before🔗

Recursion


Description

  • have a base case
  • have a general case
  • reach the base case aft er a finite (limited) number of calls to itself.

Benefits

  • More elegant
  • Use less program code than iterative solutions

Drawbacks

  • Large amounts of memory usage and processor time

How to implement recursion (Stack)

  • Each time a subroutine is called, a stack frame is pushed onto the stack.
  • A stack frame consists of the return address and the values of the local variables.
  • When a subroutine completes, the corresponding stack frame is popped off the stack.

Further Programming

Programming Paradigms

  • A programming style/classification // characteristics/features that programming language has/uses

Low-level Programming

  • Programs use the instruction set of a processor
Addressing mode
  • immediate
  • direct
  • indirect
  • indexed
  • relative

Imperative Programming (Sequence of commands)

Object Oriented Programming


Declarative Programming (Focus on the result)

  • The programmer doesn’t tell the computer what to do. To get information, the programmer poses a query (sets a goal). It’s up to the logic programming system to work out how to get the answer.
  • Declarative programming languages include SQL and Prolog.

File Processing and Exception Handling

Record

Pseudocode
1
2
3
4
5
6
7
8
9
TYPE CarRecord
    DECLARE VehicleID : STRING // unique identifier and record key
    DECLARE Registration : STRING
    DECLARE DateOfRegistration : DATE
    DECLARE EngineSize : INTEGER
    DECLARE PurchasePrice : CURRENCY
ENDTYPE

DECLARE Car : ARRAY[1:100] OF CarRecord // store the details of 100 cars
Python3
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class CarRecord:              # declaring a class without other methods
    def __init__(self):       # constructor
        self.VehicleID = ""
        self.Registration = ""
        self.DateOfRegistration = None
        self.EngineSize = 0
        self.PurchasePrice = 0.00

ThisCar = CarRecord()         # instantiates a car record
ThisCar.EngineSize = 2500     # assign a value to a field
Car = [CarRecord() for i in range(100)] # make a list of 100 car records
Car[1].EngineSize = 2500 # assign value to a field of the 2nd car in list

File processing

Psedocode
Structured English Pseudocode
Create a file and open it for writing OPENFILE <filename> FOR WRITE
Open a file in append mode OPENFILE <filename> FOR APPEND
Open a file for reading OPENFILE <filename> FOR READ
Open a file for random access OPENFILE <filename> FOR RANDOM
Close a file CLOSEFILE <filename>
Write a record to a file PUTRECORD <filename>, <identifier>
Read a record from a file GETRECORD <filename>, <identifier>
Move to a specific disk address within the file SEEK <filename>, <address>
Test for end of file EOF(<filename>)

Python3

1
file = open("filename",mode=?)   //create a file object
Recommanded mode parameter
  • 'w' for write
  • 'r' for read
  • 'a' for append
  • '+' let the file can be read and write at the same time
    • If the mode is 'w', you cannot use file.read([size]),file.readline([size]) and file.readlines([sizeint])
    • If the mode is 'w+', there is no worry of reading the file
  • 'b' for open/write/append a binary file
    • for example: 'wb', 'wb+'

Detail mode parameters
模式描述
t文本模式 (默认)。
x写模式,新建一个文件,如果该文件已存在则会报错。
b二进制模式。
+打开一个文件进行更新(可读可写)。
r以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。
rb以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。一般用于非文本文件如图片等。
r+打开一个文件用于读写。文件指针将会放在文件的开头。
rb+以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。一般用于非文本文件如图片等。
w打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。
wb以二进制格式打开一个文件只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。一般用于非文本文件如图片等。
w+打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。
wb+以二进制格式打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除。如果该文件不存在,创建新文件。一般用于非文本文件如图片等。
a打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
ab以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。也就是说,新的内容将会被写入到已有内容之后。如果该文件不存在,创建新文件进行写入。
a+打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。
ab+以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。如果该文件不存在,创建新文件用于读写。

Sequence file processing
  • To store records in binary file, we can use module pickle.
1
2
3
4
# 3 most important codes
import pickle
pickle.dump(item,file)
pickle.load(file_content)
  1. You need to import the module.
  2. Read/write a file object via the binary mode (mode='wb'/'rb'/etc.)
Code example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import pickle # this library is required to create binary files
Car = [CarRecord() for i in range(100)]

CarFile = open('CarFile.DAT', 'wb') # open file for binary write
for i in range(100): # loop for each array element
    pickle.dump(Car[i], CarFile) # write a whole record to the binary file

CarFile.close() # close file

CarFile = open('CarFile.DAT', 'rb') # open file for binary read

Car = [] # start with empty list
while True: # check for end of file
    Car.append(pickle.load(CarFile)) # append record from file to end of list

CarFile.close()
Some file fuctions
序号方法及描述
1

file.close()

关闭文件。关闭后文件不能再进行读写操作。

6

file.read([size])

从文件读取指定的字节数,如果未给定或为负则读取所有。

7

file.readline([size])

读取整行,包括 "\n" 字符。

8

file.readlines([sizeint])

读取所有行并返回列表,若给定sizeint>0,返回总和大约为sizeint字节的行, 实际读取值可能比 sizeint 较大, 因为需要填充缓冲区。

9

file.seek(offset[, whence])

移动文件读取指针到指定位置

10

file.tell()

返回文件当前位置。

11

file.truncate([size])

从文件的首行首字符开始截断,截断文件为 size 个字符,无 size 表示从当前位置截断;截断之后后面的所有字符被删除,其中 windows 系统下的换行代表2个字符大小。

12

file.write(str)

将字符串写入文件,返回的是写入的字符长度。

13

file.writelines(sequence)

向文件写入一个序列字符串列表,如果需要换行则要自己加入每行的换行符。


Exception


Code language
Pseudocode
1
2
3
4
5
TRY
    <statementsA>
EXCEPT
    <statementsB>
ENDTRY
1
2
3
4
5
6
7
8
# With FINALLY
TRY
    <statementsA>
EXCEPT
    <statementsB>
FINALLY
    <statementsC>
ENDTRY
  • TRY
  • EXCEPT
  • FINALLY - The statements (<statementsC>) in this block will be executed regardless of whether there was an exception or not.
Python3
1
2
3
4
5
6
7
8
9
## Basic try and exception examples
file = open("test.txt",mode='r')


try:
    file.readlines()
except EOFError:
    print("reach the end of file")
    file.close()
1
2
3
4
5
6
try:
    file = open("test.txt",mode='w')
    file.write("line 1")
    file.close()
except IOError:
    print("cannot find the file")
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
try:  
    runoob()  
except AssertionError as error:  
    print(error)  
else:  
    try:  
        with open('file.log') as file:  
            read_data = file.read()  
    except FileNotFoundError as fnf_error:  
        print(fnf_error)  
finally:  
    print('这句话,无论异常是否发生都会执行')
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import sys  

try:  
    f = open('myfile.txt')  
    s = f.readline()  
    i = int(s.strip())  
except OSError as err:  
    print("OS error: {0}".format(err))  
except ValueError:  
    print("Could not convert data to an integer.")  
except:  
    print("Unexpected error:", sys.exc_info()[0])
More info about exception in Python3
🦼 Wuu~~~
Built with Hugo
Theme Stack designed by Jimmy