More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post
page
Python IDE Dashboard

Discount Price Calculator – Q&A

Question 1[10 marks]
Oksana has created the following program in her computer science lesson. The aim of this program is to calculate the price after discount of a list of products bought by a customer.

Here is her Python code:
#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!")


To help Oksana use the right terminology when annotating and explaining what her code does, you will need to answer the following 10 questions:

  1. What is the identifier of the subroutine defined in this code?
  2. Is this subroutine a procedure or a function? Why?
  3. How many parameters does this subroutine take?
  4. What programming construct is this subroutine based on: sequencing, selection or iteration?
  5. Which line of code will be executed first by the computer when you run this code?
  6. What programming construct is the main program based on: sequencing, selection or iteration?
  7. Can you identify three variables used in the main program?
  8. What is the data type of item?
  9. What is the data type of discount?
  10. On which line of code is string concatenation used?
1) 
2) 
3) 
4) 
5) 
6) 
7) 
8) 
9) 
10) 
Question 2[4 marks]
Without rewriting the above code, explain how Oksana could improve this code further to calculate and output the total discounted cost of all the items being bought by a customer?
Save / Share Answers as a link
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Mode Algorithm using a Hash Table

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:

list = [14, 8, 25, 14, 32, 8, 8, 32, 7, 8]​

The mode is 8 as the value 8 appears 4 times. You will notice that it is easier to spot the mode of a list of numbers when the numbers are sorted. e.g.:
list = [7, 8, 8, 8, 8, 14, 14, 25, 32, 32]​

Method 1: Iterative Approach, using a sorted list

mode-iterative-apporach
Python Code:

Method 2: Using a hash table to store the frequency of each value

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.

mode-algorithm-using-hash-table
Python Code:

Your Task

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:

list = [7, 8, 8, 8, 8, 14, 14, 25, 30, 30, 30, 30, 32, 32]​

The above list is bimodal as it has two modes: 8 and 30.

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.

Tagged with:

Job Scheduling Algorithms – Q&A

Question 1[20 marks]
One of the main purpose of the Operating System is to control the hardware and more specifically the CPU. The CPU performs all the jobs/processes requested by the different applications. A scheduler is a program of the Operating System that manages the amount of time that is allocated to each job that the CPU needs to process. To do so the scheduler keeps a list of all the jobs that need to be processed. This is called the job queue. The scheduler also uses a scheduling algorithm to determine in which order these jobs will be processed and the amount of processing time to allocate to each job.

The main job scheduling algorithms are:
  • FCFS: First Come First Served
  • Round Robin
  • Shortest Job First
  • Shortest Remaining Time First
  • Multilevel Feedback Queues
This question is based on the following job queue:
job-queue
For each of the following scheduling algorithms:
  1. Determine the order in which each job will be processed by the CPU (e.g. ABCD).
  2. Estimate the completion time for each job in the queue.
Current time: 01:00 (mm:ss) – Round Robin Time Slice: 1s
FCFS: ……………
    Job A: Completion time: 
    Job B: Completion time: 
    Job C: Completion time: 
    Job D: Completion time: 

Shortest Job First: ……………
    Job A: Completion time: 
    Job B: Completion time: 
    Job C: Completion time: 
    Job D: Completion time: 

Shortest Remaining Time First: ……………
    Job A: Completion time: 
    Job B: Completion time: 
    Job C: Completion time: 
    Job D: Completion time: 

Round Robin: ……………
    Job A: Completion time: 
    Job B: Completion time: 
    Job C: Completion time: 
    Job D: Completion time: 

Question 2[4 marks]
Some job scheduling algorithms can potentially lead to starvation? Out of the four algorithms listed in question 1, what algorithms can potentially lead to starvation? Explain what would cause this?
Save / Share Answers as a link
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Disk Scheduling Algorithms

A hard drive is a magnetic storage device consisting of several disks (a.k.a. platters) where data is stored.

hard-disk-platters-tracks-sectors

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

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:

  • The FCFS Disk Scheduling Algorithm (First Come First Serve)
  • The SCAN Disk Scheduling Algorithm
  • The LOOK Disk Scheduling Algorithm
  • The SSTF Disk Scheduling Algorithm (Shortest Seek Time First)

FCFS Disk Scheduling Algorithm

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:

Seek Time = (Number of tracks crossed) x (Time to cross one track)

fcfs-disk-scheduling

So in this case, we can calculate the number of tracks crossed as follows:

Number of tracks crossed = |20-43| + |43-52| + |52-24| + |24-65| + |65-70| + |70-48| + |48-16| + |16-61| = 205

Seek Time = 205 x (Time to cross one track)

SCAN Disk Scheduling Algorithm

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.
SCAN-Disk-Scheduling-Algorithm
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:

Number of tracks crossed = |20-99| + |99-16| = 162

Seek Time = 162 x (Time to cross one track)

On average this algorithm will reduce the overall seek time (compared to a FCFS Disk Scheduling algorithm.

LOOK 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.
Look-Disk-Scheduling-Algorithm
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:

Number of tracks crossed = |20-70| + |70-16| = 104

Seek Time = 104 x (Time to cross one track)

On average this algorithm will reduce the overall seek time (compared to both a FCFS Disk Scheduling algorithm and a SCAN Disk Scheduling algorithm).

SSTF 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.
SSTF-Disk-Scheduling-Algorithm
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:

Number of tracks crossed = |20-24| + |24-16| + |16-70| = 66

Seek Time = 66 x (Time to cross one track)

On average this algorithm will reduce the overall seek time (compared to all the other 3 algorithms we investigated in this blog post).

You Task

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]

Information Board: Javascript Challenge

information-board
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

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area


Tagged with:

The 4 Stages of the Compilation Process

The compilation process involves converting high-level source code into machine code that a computer can execute. It’s a multi-step process, and each stage plays an important role in ensuring that the code runs as expected. Here are the four main stages of the compilation process:

1. Lexical Analysis

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.

2. Syntax Analysis

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.

3. Code Generation

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.

4. Code Optimisation

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.

To Sum It Up…

  • Lexical Analysis: Breaks down the source code into tokens and removes comments, annotations, and whitespace.
  • Syntax Analysis: Checks the tokens against the syntax rules of the language, generating a parse tree or abstract syntax tree (AST).
  • Code Generation: Translates the valid code into machine-readable or intermediate code.
  • Code Optimisation: Improves the efficiency of the generated code, making it run faster or consume less memory.

Each of these stages is crucial in turning human-readable high-level code into a functional and efficient machine-executable program.

Task 1: Drag and Drop

Can you identify and describe the 4 stages of the compilation process?

Complete the fill in the gaps activity below to label the 4 stages of the compilation process with for each stage their input and output:

Compilation ProcessOpen in New Window

Task 2: Q&A

Answer the following question:

Question 1[12 marks]
Can you identify and describe the 4 stages of the compilation process:
Stage 1: 

Stage 2: 

Stage 3: 

Stage 4: 

Save / Share Answers as a link

Network Design – Drag & Drop

Can you identify some of the main hardware components that you will find in a small Client-Server Local Area Network?

Before competing this activity, you may want to read more about network design concepts.

Task 1: Network Design Drag and Drop

Complete the fill in the gaps activity below to label this network diagram:

Network DesignOpen in New Window

Task 2: Q&A

Answer the following questions, in the context of the above Local Area Network.

Question 1[6 marks]
Can you describe the purposes of the following network components used in the above Local Area Network:
Router: 

Switch: 

Firewall: 

WAP: 

File Server: 

E-Mail Server: 

Question 2[2 marks]
The above network is a Client Server network as it includes servers which provide a dedicated services to all the computers/workstations connected to the network. Can you identify two other types of servers, other than File Server and E-Mail Server, that can be used on a Client-Server network?
Save / Share Answers as a link
Tagged with:

Traffic Lights Controller using Logic Gates

In this challenge, we will design, create and test a logic gates circuit to control a traffic lights made of 3 LEDs (Red Amber Green). This challenge is designed to be completed with a logic board such as the Logic & Arithmetic board designed by the University of Southampton and the UK Electronic Skills Foundation. You will be able to complete the circuit with just the Logic & Arithmetic board. However if you have access to breadboards and LEDs you will be able to create your own Red Amber Green traffic lights for a more realistic output.

Alternatively, if you do not have access to a physical kit, you can still complete these challenges using our online logic gates circuit simulator

Traffic Lights Sequence

A UK Traffic Lights follows a sequence of four different states:
traffic-light-sequence

In order to design a logic gates circuit for our traffic lights system we will need a circuit with two inputs (J and K) and 3 outputs (X, Y and Z). Our two inputs will give 4 different states to our circuit: 00, 01, 10 and 11. The following diagram shows the expected outputs for 4 possible states (input patterns):
traffic-lights-sequence-2-inputs-3-outputs

Truth Table?

Your first task is to use the above diagram to complete the Truth Table for our Traffic Light Controller system:

Input J Input K Output X (Red) Output Y (Amber) Output Z (Green)
0 0
0 1
1 0
1 1

Logic Gates Diagrams

Using the information from the completed Truth table we can then work out the logic gates circuits needed to control each output based on the two inputs J and K.
You can try to create this diagrams by yourself using logic.ly or our online Logic Gates Diagram designer tool.

Here are the individual diagrams:
Output X: Red Light:
Red-Light-Logic-gates-Diagram
Output Y: Red Light:
Amber-Light-Logic-Gates-Diagram
Output Z: Red Light:
Green-Light-Logic-Gates-Diagram

All three diagrams can then be combined into a single diagram:
Traffic-Lights-Controller-Logic-Gates-Diagram

You can now recreate this circuit with your logic board:
traffic-light-controller-logic-board-xyz

Using RGB LEDs

The following diagram shows you how to connect your logic board to 3 LEDs on a breadboard.
traffic-light-controller-logic-board-leds

Be careful when connecting your LEDs to detect where the anode and the cathode are on each LED:
LED

Using Lego bricks…

You can also create your own traffic lights using Lego bricks and control them with your logic board!
lego-traffic-light-front
lego-traffic-light-back
lego-led


Traffic Light Sequence using Logic Gates

By combining the above circuit with a 2-bit counter circuit, we can create a fully animated traffic light sequence. Click on the diagram below to test this circuit:

Tagged with: ,

Comparison Operators using Logic Gates

This set of challenges is designed to be completed with a logic board such as the Logic & Arithmetic board designed by the University of Southampton and the UK Electronic Skills Foundation.

Alternatively, if you do not have access to a physical kit, you can still complete these challenges using our online logic gates circuit simulator.

J==K? Comparing if two inputs are equal

The following diagram is used to compare if two inputs are the same. The output (X) should be on if both inputs J and K are set to O or both inputs are set to 1. This circuit is used to implement the comparison operator == (“is equal to”).

logic-board-equal-to-operator

Truth Table:

Input J Input K Output X
0 0
0 1
1 0
1 1

J<K? Less than operator

The following diagram is used to compare if input J is strictly lower than input K. The output (X) should be on if so (e.g. when J = 0 and K = 1). This circuit is used to implement the comparison operator < (“is less than”).

logic-board-less-than-operator

Truth Table:

Input J Input K Output X
0 0
0 1
1 0
1 1

J>K? Greater than operator

The following diagram is used to compare if input J is strictly greater than input K. The output (X) should be on if so (e.g. when J = 1 and K = 0). This circuit is used to implement the comparison operator > (“is greater than”).

logic-board-greater-than-operator

Truth Table:

Input J Input K Output X
0 0
0 1
1 0
1 1

1-bit magnitude comparator

By combining all three logic gates circuits into one, we can create a 1-bit magnitude comparator that takes two inputs (J and K) and produces three outputs:

  • X = (J < K)
  • Y = (J == K)
  • Z = (J > K)

Here is the logic gates diagram for this comparator. You can recreate this circuit and test it on your logic board.
logic-board-binary-comparator
comparisons-operators-equal-less-greater

Truth Table:

Input J Input K Output X
J < K ?
Output Y
J == K ?
Output Z
J > K ?
0 0
0 1
1 0
1 1

Extension Task: !=, <= and >= Comparison Operators

comparison-operators
How could you combine and tweak the above diagrams to create logic gates circuits to implement the following comparison operators:

  • “is not equal to” (e.g. J != K)
  • “is lower than or equal to” (e.g. J <= K)
  • “is greater than or equal to” (e.g. J >= K).

Design your circuits, then recreate them and test them on the logic board to see if they “behave” as you would expect!

Find out more…

You can find out more about binary comparators on the following pages:

Tagged with: ,

Logic Gates Circuits

This set of challenges is designed to be completed with a logic board such as the Logic & Arithmetic board designed by the University of Southampton and the UK Electronic Skills Foundation.

Alternatively, if you do not have access to a physical kit, you can still complete these challenges using our online logic gates circuit simulator

Step 1: The Main Logic Gates

For each of the main logic gates, recreate the circuit as it appears on the diagram. Then use your circuit to compete the truth table for the logic gate.

NOT GateAND GateOR GateXOR GateNAND GateNOR Gate

logic-board-not-gate
Truth Table:

Input J Output X
0
1


logic-board-and-gate
Truth Table:

Input J Input K Output X
0 0
0 1
1 0
1 1


logic-board-or-gate
Truth Table:

Input J Input K Output X
0 0
0 1
1 0
1 1


logic-board-xor-gate
Truth Table:

Input J Input K Output X
0 0
0 1
1 0
1 1


logic-board-nand-gate
Truth Table:

Input J Input K Output X
0 0
0 1
1 0
1 1


logic-board-nor-gate
Truth Table:

Input J Input K Output X
0 0
0 1
1 0
1 1

Step 2: Logic Gates Circuits

By combining multiple logic gates together you can create a circuit that will “behave” a certain way. To record how the circuit “behaves” for any given set of inputs we use a Truth Table. For instance you can complete the Truth table for the following circuit:

Logic Gates Circuit
logic-board-circuit-1
Truth Table:

Input J Input K Input L Output X
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

Step 3: Logic Problems

Your task is to create and test a logic gate circuit for each of the given problems:

Car LightBurglar AlarmSmartphone Ringtone
car-3-doors
A small car has 3 doors: 2 front doors and a hatchback. The internal light of the car is on when any of the car doors is open.

Create and test a logic gates circuit to control the internal light (X) of the car based on whether a door (J, K or L) is open (1) or closed (0).

Input J Input K Input L Output X
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

burglar-alarmA burglar alarm has three inputs:

  • Input J: Whether the burglar alarm is activated (1) or not (0).
  • Input K: A motion sensor can detect movement (1) or not (0).
  • Input L: A door sensor can detect if the front door is open (1) or is closed (0).

The alarm siren (X) should be on if the alarm is activated and the motion sensor detects a movement or the door sensor detects that the door is open.

Create and test a logic gates circuit to control the siren of this burglar alarm based on the three inputs J,K and L.

Input J Input K Input L Output X
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

incoming-call
A smartphone ringtone is played only if:

  • The phone is receiving an incoming call. (input J = 1)
  • The phone sound is on. (input K = 1)
  • The phone is not on airplane mode. (input L = 0)

Create and test a logic gates circuit to control whether the ringtone of a smartphone is on (X=1) or not (X=0) based on the three inputs J, K, L.

Input J Input K Input L Output X
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

Extension Task: The Mystery Gate!

Mystery Gate

logic-gates-simplify-circuit
The above logic gates circuit can be replaced by one single logic gate! Recreate this circuit on your logic board to test it and complete its truth table. Then find out what logic gate has the same truth table as this complex circuit.
Truth Table:

Input J Input K Output X
0 0
0 1
1 0
1 1

Logic Gate:

Tagged with: ,