
Getting ready for your A Level Computer Science exam? Test our knowledge by answering all the following questions:
A Level Computer Science RevisionOpen in New Window
Getting ready for your A Level Computer Science exam? Test our knowledge by answering all the following questions:
A Level Computer Science RevisionOpen in New Window
#Voting Age Checker a = int(input("How old are you?")) if a<=18: print(You can vote!") else: y = 18 - a print("You will be allowed to vote in " + str(y) + " years!")
print("Your five lucky numbers are...") number = random.randint(1,100) print(number) number = random.randint(1,100) print(number) number = random.randint(1,100) print(number) number = random.randint(1,100) print(number) number = random.randint(1,100) print(number)
list = [4, 6, 12, 98, 57, 32, 31, 6] for i in range (0,len(list)): file = open("stats.txt","a") # "a" -> append mode file.write(list[i]) file.close()
rating = int(input("Enter a rating between 1 and 5")) if rating < 0: print("This rating is to low...") elif rating > 5: print("This rating is too high!") else: print("This rating is valid!")
#Discount Price Calculator def calculateDiscount(price,percentageDiscount): discount = price * percentageDiscount / 100 discount = round(discount,2) return discount #Main Program item = input("Enter the name of the item to buy? or X to quit!") while item!="X": itemPrice=float(input("Enter a price for your item: £")) percentageDiscount = int(input("Enter a percentage discount for your item: %")) discount = calculateDiscount(itemPrice, percentageDiscount) newPrice = itemPrice - discount print("Discounter price: £" + str(newPrice)) item = input("Enter the name of another item to buy? or X to quit!")
In this challenge we will compare two methods used to calculate the mode value of a list of numbers. In Maths, The mode is the value that appears most often in a set of data. For instance, considering the following list of numbers:
Python Code:
This method relies on the use of a hash table (a.k.a dictionary data strucutre) to store the frequency of each distinct value of the list.
Python Code:
Both of the above algorithm will return the mode of a list. However, on occasion, there could be more than one mode value: if there are multiple numbers that occur with equal frequency, and more times than the others in the set. For instance:
Your task is to adapt both of the above algorithms to make sure they identify all the mode values from a given list of numbers.
A hard drive is a magnetic storage device consisting of several disks (a.k.a. platters) where data is stored.
Each disk is divided into many concentric circular tracks. Each track contains several sectors where data is stored. When the Operating System stores a file on a hard drive, this file is split into several clusters. Each cluster will be stored across a few sectors. The typical size of a cluster on a Windows computer is 4KB.
When loading a file into memory, it is necessary to access all the sectors used by this file. This involves physical movement of the Hard Drive arm used to position the Read/Write head on the right track. Once the head is positioned on the right track, the sectors of this track can easily be reached as the disk is constantly rotating. Moving the arm from one track to another takes time which increases the overall seek time: the time taken to locate a specific piece of data stored on a hard disk.
Disk scheduling is done by the operating system to schedule Read/Write requests (a.k.a I/O requests) arriving for the disk.
When a file is loaded into memory, several sectors (and tracks) will need to be accessed. The operating system can use different disk scheduling algorithms to do so. In this post we will compare four main algorithms:
Let’s consider a disk consisting of 100 concentric tracks (0-99) and let’s assume the Operating System needs to access sectors on the following tracks: [43,52,24,65,70,48,16,61]. And let’s assume the head is, to start with at track 20.
With a First Come First Served scheduling algorithm, the tracks will be accessed in the order they were requested. So the read/write head will be moving from its current position to track 43, then track 52 then track 24 and so on…
Looking at the above pattern we can estimate the seek time of this approach:
So in this case, we can calculate the number of tracks crossed as follows:
With a SCAN Disk Scheduling algorithm, the disk arm will not access the tracks in the order they were requested. Instead it will move in one direction and service the requests coming in its path. After reaching the end of disk (e.g. track 99), it will reverse its direction and again services the request arriving in its path. Due to the movement of the arm going all the way up and down the number of tracks, the algorithm is also known as elevator algorithm. The aim of this approach is to reduce the average seek time when accessing a list of sectors.
In this case, using the same example as above: Starting Position: Track 20, list of tracks to access [43,52,24,65,70,48,16,61] we can calculate the number of tracks crossed as follows:
On average this algorithm will reduce the overall seek time (compared to a FCFS Disk Scheduling algorithm.
The LOOK disk scheduling algorithm is an improved version of the SCAN disk scheduling algorithm. In this algorithm, instead of reaching the last track (e.g. track 99) before changing direction, this algorithms changes direction when it reaches highest (or lowest) track from the list of track to access.
In this case, using the same example as above: Starting Position: Track 20, list of tracks to access [43,52,24,65,70,48,16,61] we can calculate the number of tracks crossed as follows:
On average this algorithm will reduce the overall seek time (compared to both a FCFS Disk Scheduling algorithm and a SCAN Disk Scheduling algorithm).
With the Shortest Seek Time First algorithm, we once again do not necessary process the requests in order they arrived. Instead we all always process the request which is the closest to the current position (track number) of the head.
In this case, using the same example as above: Starting Position: Track 20, list of tracks to access [43,52,24,65,70,48,16,61] we can calculate the number of tracks crossed as follows:
On average this algorithm will reduce the overall seek time (compared to all the other 3 algorithms we investigated in this blog post).
Estimate the Seek Time for all 4 scheduling algorithms for the given track lists:
Starting Position | Track List | FCFS Disk Scheduling Seek Time |
SCAN Disk Scheduling Seek Time |
LOOK Disk Scheduling Seek Time |
SSTF Disk Scheduling Seek Time |
|
#1 | 50 | [24,60,30,8,52] | ||||
#2 | 80 | [70,60,10,20,40,30] | ||||
#3 | 40 | [32,48,64,12,16,8,90] |
In this challenge we are creating an information board that displays key messages on a LED screen that consists of 4 rows of 20 characters.
We have created the HTML, and CSS code to render the board on the screen with a welcome message that, for now is hardcoded in the HTML code.
We have also created a Javascript function called clearBoard() to clear the content of the board and have started a new function called inputMessage(). This function will allow the user to input a message to display on the board. However this function is incomplete and your task will be to add the relevant code to convert the message typed by the end-user into a Javascript 2D array called board. This board will then be passed as a parameter to the displayBoard() function that displays the content of this 2D array on the information board.
Note that when displaying a message on the board, if a word cannot fit on a line, then it is added at the start of the next line of the board. (Words are not truncated or hyphenated!)
To complete this challenge, you will need to click on the “Edit on CodePen” button below, and edit the Javascript code from line 36.
See the Pen
Information Board 101 by 101 Computing (@101Computing)
on CodePen.
This challenge is based on a question from the H046 A Level Computer Science June 2017 – Exam paper from OCR
The first step in the compilation process is lexical analysis. During this stage, the source code is read and converted into a sequence of tokens. A token is a basic unit of meaning, like keywords (if
, while
), variables, constants, operators (+
, -
), and punctuation.
During lexical analysis, the compiler also removes comments, annotations (like //
or /*...*/
), and whitespace (such as spaces, tabs, and newlines). These are ignored because they are not needed for understanding the logic of the program, but they help in writing human-readable code.
This step is performed by the lexer or scanner, which breaks down the source code into these tokens.
The next stage is syntax analysis (also known as parsing). The compiler takes the tokens generated in the previous stage and checks whether they are arranged in a valid way according to the grammar (syntax) of the programming language.
A parser is used to create a parse tree or abstract syntax tree (AST). This tree represents the structure of the program, showing how different parts of the code relate to each other according to the language rules. For example, the parser will check if statements are correctly formed, like ensuring that an if
statement has the proper condition and body.
The purpose is to ensure the code follows the language’s syntax rules and that the sequence of tokens makes sense logically in the structure of the program.
During the syntax analysis, if a syntax error is detected, the compilation process stops here and an error message is generated.
Once the program passes the syntax analysis and is considered structurally correct, the compiler moves to code generation. In this step, the compiler translates the high-level source code (like if (x > 5)
) into machine code or intermediate code (such as bytecode for languages like Java).
The compiler uses the abstract syntax tree (AST) from the syntax analysis stage to generate the target code. This code can be directly executed by the computer’s processor or can be further processed into an executable program.
The purpose of code generation is to create an efficient machine-readable output (binary code) that the computer can execute.
Code optimisation is an optional but very important step where the generated code is improved for efficiency. The optimiser attempts to reduce the program’s runtime, memory usage, or both, without changing its functionality.
The optimisation process might involve eliminating redundant calculations, simplifying loops, or using more efficient data structures or algorithms. In some cases, it can rearrange the order of instructions to make the program run faster or take up less space.
The goal is to make the code run faster or more efficiently, improving its performance while keeping the logic of the program intact.
Each of these stages is crucial in turning human-readable high-level code into a functional and efficient machine-executable program.