The mesh network puzzle consists of creating a partial mesh network that consists of:
Three workstations (client computers)
Three servers: a file server, an e-mail server and a web server.
Network Requirements
Each client computer will need to be connected to all three servers using a partial mesh topology. The direct connections between a workstation and a server will be done using Ethernet cables.
The constraint for this puzzle is that you are not allowed to cross over Ethernet cables!!!
Here is our first attempt at solving this puzzle! in this attempt, you can see that we cannot connect the third workstation to the file server without crossing over an ethernet cable!
Your Turn
Grab a piece of paper and draw three workstations and three servers. Try to connect them following the requirements for this network.
Alternatively, you can draw your network topology on this online whiteboard:
In this blog post we will investigate how to implement a bouncing algorithm used either in a platform game when the main sprite collides with a platform or in a breakout game when the ball bounce against a brick.
(x,y) coordinates and velocity vector
In a frame based game, each sprite can be positioned on the screen based on its (x,y) coordinates. Each sprite can also be allocated a velocity vector. The velocity vector consists of two values (Vx, Vy) which represents the variation in pixels of the (x,y) coordinates of the sprite between two frames.
Collision Detection
A collision occurs when two sprites are overlapping. Different algorithms can be used to detect such collisions. When creating a game with a library such as the Pygame library, you can re-use built-in collision detection algorithms without the need to implement these yourself.
A collision will impact the velocity vectors of the sprites involved in the collision. In a platform game or breakout game, we can assume that the platform or the brick will remain static. However, the collision will impact the velocity vector of the main character or the bouncing ball. Note that in a pool game the collision between two balls would impact the velocity vectors of both sprites involved in the collision!
The change in direction will depends on which edge of the platform/brick is being hit first and on the initial direction of the sprite when it collides with the platform.
Let’s investigate one approach that could be used in both our games:
Implementing a Bouncing Algorithm
This approach consists of considering 8 possible different collisions and their impact on the velocity vector of the bouncing sprite.
So, to implement an effective bouncing algorithm of a sprite against this platform, we must first identify the position of the sprite, relatively to the platform to distinguish the 8 possible areas. In the case of a rectangular platform, we can use the following comparisons between the (x,y) coordinates of the centre of our sprite with the top, right, bottom, left coordinates of our platform.
The following pseudocode demonstrates how the bouncing algorithm can be implemented. Though it only covers the first three scenarios, you should be able to complete this algorithm further to cater for all 8 scenarios.
Landing on a platform
Note that the above pseudocode is used to make our sprite bounce against the platform which would be suitable for the breakout game and in most cases for a platform game. However, with a platform game. When the sprite lands on a platform (case 2 in the above diagram/pseudocode) we may not want the sprite to bounce back but instead we want the sprite to remain on top of the platform. In this case the pseudocode for this scenario would be as follows:
Did you know that in the UK (and also in most European countries), the eggs you buy in a shop must be stamped with a code that can help you find out more about how and where your eggs were produced. This code is helpful to distinguish organic and free range eggs from eggs from more industrial production (e.g. barn or caged hen eggs).
Here is how the code works:
Python Challenge
For this challenge you will need to write a Python script that:
Takes one input from the end-user: the code as it appear on a stamped egg. (Just the code, not the Best Before Date)
Use this code to output the farming method this egg originated from: (Organic, Free range, Barn or Cage)
Output the country of Origin: e.g.:
UK: United Kingdom
NL: Netherlands
FR: France
BE: Belgium
DE: Germany
ES: Spain
Output the farm/producer ID
Python Code
Complete the code below…
Test Plan
Test #
Input Value
Expected Output
Actual Output
#1
1UK54321
Free Range Egg Country of Origin: United Kingdom Farm Id: 54321
#1
0NL6789
Organic Egg Country of Origin: Nethelands Farm Id: 6789
#1
3ES02468
Barn Egg Country of Origin: Spain Farm Id: 02468
Extension Task #1: Defensive Design
Update your code to automatically reject any invalid code:
A valid code contains at least 7 alphanumerical characters,
A valid code should start with a number digit between 0 and 3.
Extension Task #2: Country Codes
For our code to be complete, we need to recognise all the possible two-letter country codes. We have stored the full list of country codes in the following CSV file.
In this blog post, we will investigate the use of a Stack data structure to store the different flavours of the different scoops of an ice cream!
A stack is a FILO data structure: First In Last Out and seems to be a suitable data structure for our ice cream, where the first scoop of ice-cream being added on the cone, will be the last one to be eaten!
With a Stack data structure you can either:
Push new data at the end of the stack.
Pop the last value of the stack.
In order to implement our stack will use a 1D array of 5 values, assuming that you cannot make an ice cream with more than 5 scoops! We will use a pointer to store the index of the next empty cell of our array where data can be added.
Using OOP programming, we will create an IceCreamclass to implement our ice cream as a stack. We will encapsulate both the array data structure and the stack pointer as private properties of the IceCream class and we will use two public methods to push() and pop() scoops of different flavours to/from our IceCream!
Let’s look at the code of our class:
#A class to implement a Stack that can hold up to 5 values
class IceCream():
#Constructor...
def __init__(self):
self.__array = [None,None,None,None,None] #__Private Property
self.__pointer = 0 #__Private Property
self.__maxCapacity = len(self.__array) #__Private Property
#A method to push a new scoop/flavour to the ice cream
def push(self, flavour):
#Let's first check that we have not reached the maximum capacity (5 scoops max)
if self.__pointer < self.__maxCapacity:
self.__array[self.__pointer] = flavour
self.__pointer = self.__pointer + 1
print(flavour + " added!")
else:
print("Cannot add new favour! Ice cream is full!")
#A method to pop the last scoop/flavour of the ice cream
def pop(self):
#Let's first check that the ice cream is not empty (has at least 1 scoop)
if self.__pointer > 0:
self.__pointer = self.__pointer - 1
flavour = self.__array[self.__pointer]
self.__array[self.__pointer] = ""
return flavour
else:
print("Ice cream is empty!")
return False
Now that we have an IceCream class, we can instantiate any ice cream object and construct each ice cream by pushing (and popping/eating) different flavours!
myIceCream = IceCream()
myIceCream.push("Strawberry")
myIceCream.push("Chocolate")
myIceCream.push("Pistachio")
flavour = myIceCream.pop()
print("This scoop of " + flavour + " was delicious!")
myIceCream.push("Blueberry")
Full Python Code
Let’s test this code… You can customise this code to create any ice cream based on your favourite flavours!!!
In this blog post, we are going to learn about the format of different addresses used on computer networks to uniquely identify hardware devices. We will investigate the use and format of:
IPv4 Addresses,
IPv6 Addresses,
MAC Addresses.
IPv4 Addresses
An IP address is a unique identifier of a device on an IP network.
An IP address (IPv4) consists of 4 Bytes: 4 numbers between 0 and 255 separated by dots.
IPv4:
A dynamic IP Address can change every time your restart your device/reconnect to the network.
Some devices can be configured on an IP network to have a static IP address: it will remain she same even if you restart your device.
As an IPv4 address consists of 4 Bytes (=32 bits), there are 232 possible IP addresses which is just under 4.3 billions IP addresses. Though this seems like a large number, we are reaching a stage where we are running out of unique IP addresses. Remember, every device (router, computer, smartphone, smartwatch, smart TV, smart speaker, etc.) connected to the Internet will have its own IP address and there are nearly 8 billions human beings on planet Earth. This is the reason why the Internet is progressively upgrading to the next generation of IP addresses: IPv6…
IPv6 Addresses
To overcome the issue of the Internet running out of unique IPv4 addresses, a new format was introduced. IPv6 addresses consist of 16 Bytes (=128 bits). They are expressed in hexadecimal using 8 blocks of 2 Bytes (4 hexadecimal digits) separated by colons e.g.:
IPv6:
With 128 bits per IP address, we can generate 2128 unique IP addresses. That’s 340,282,366,920,938,463,463,374,607,431,768,211,456 IP addresses, more than enough to give each grain of sand on planet Earth a unique IP address!!!
MAC Addresses
A MAC address consists of 6 Bytes of Data, expressed in hexadecimal, separated using colons. e.g.
MAC Address:
A MAC Address is static and unique for a device. It cannot change. It is hardcoded into the device by the manufacturer when the device is built. (A bit like the chassis number of a car!)
Networking equipment such as routers and switches use IP addresses and/or MAC addresses to direct traffic between computers/devices on the network.
URL: Uniform Resource Locator
A live website is hosted/stored on a webserver and consists of different files such as HTML pages, png graphics, jpg pictures, gif animations and other multimedia files (mp3 audio clips, mp4 movie clips, pdf documents etc.). These files are stored on the web server within a root folder and other subfolders. A URL is used to request a specific file (resource) stored on a web server.
A URL is used to request/locate a specific resource stored on a web server. It consists of:
The protocol used to access the web page: either HTTP or HTTPs,
The Domain Name to identify the web server,
Optional: Folder/Sub-folder structure: If the resource/file you are trying to access is not on the root folder of the website but in a sub-folder,
Optional: File name: the file name and extension of the file you are requesting. If the filename is not provided, the server will return the index.html file if it exists.
Error 404: Page Not Found
When a URL points towards a file that cannot be located or simply does not exist on the webserver, a 404 error message is returned to the web browser!
Domain Name Server
In order to connect to a website, your web-browser needs to know the IP address of the webserver where the website is hosted. However, IP address are tricky to remember (for human beings). This is why domain names were invented! Domains names such as 101computing.net or amazon.com are easy to remember. When the user types a domain name in the address bar of the browser, the browser automatically contacts a DNS server on the Internet. The DNS server will then lookup for the matching IP address for this domain name and return it to the web-browser. The web-browser will then be able to contact the webserver to request a specific page:
In a way, we could say that a Domain Name Server is the equivalent of of the phone book that we used to consult when we had the name of someone we wanted to call but did not know their actual phone number. We would lookup for their name in the phone book to retrieve their phone number. Similarly, a Domain Name Server is used to lookup a domain name to find its IP address!
Python Task #1
Write a python program to generate and output:
A random IPv4 address,
A random IPv6 address,
A random MAC address.
Python Task #2
Write a python program that asks the user to enter a URL. The program will then extract and output the following components of the URL:
Protocol,
Domain Name,
Folder Structure,
File name.
Solution...
The solution for this challenge is available to full members! Find out how to become a member: ➤ Members' Area
In the following set of challenges, we will use a trace table to demonstrate the impact of the FDE cycle on the main registers:
Program Counter (PC)
Memory Address Register (MAR)
Memory Data Register (MDR)
Current Instruction Register (CIR)
Accumulator (ACC)
Note that the following program are all based on the LMC language and are hence based on direct addressing.
To complete this task, you need to understand what happens inside the CPU at each stage of the FDE cycles. For instance:
A the start of the Fetch stage of the FDE cycle, the PC contains the address of the next instruction to be Fetched. So the value of the PC is copied to the MAR so that the instruction can be retrieved from memory. The instruction stored at this MAR address will be stored in the MDR and immediately copied to the CIR.
The PC is then automatically incremented by 1.
What happens during the Execute stage of the FDE cycle will depend on the instruction itself.
It may be that more data may be loaded from memory using the MAR and MDR (e.g. LDA, ADD, SUB instructions).
It may be that the PC will be overridden by a BRP, BRZ or BRA instruction.
The Accumulator may also be overwritten by an input value (INP), the result of executing a calculation (ADD, SUB) or of loading data from memory (LDA).
Challenge #1Challenge #2Challenge #3
Challenge #1:
When tracing the following algorithm we will use the following input values:
First input: 5
Second input: 4
INP
STA 99
INP
ADD 99
OUT
Trace Table
We have started to complete the trace table for the first two FDE cycles of this program. Your task is to complete this trace table for the whole program.
FDE Stage
PC
MAR
MDR
CIR
ACC
Output?
– RESET –
0
0
0
0
0
Fetch
1
0
INP
INP
0
Execute
1
0
INP
INP
5
Fetch
2
1
STA 99
STA 99
5
Execute
2
99
5
STA 99
5
…
…
…
…
…
…
…
Challenge #2:
When tracing the following algorithm we will use the following input values:
First input: 5
Second input: 4
INP
STA 99
INP
STA 98
SUB 99
BRP 08
LDA 99
BRA 09
LDA 98
OUT
Trace Table
We have started to complete the trace table for the first two FDE cycles of this program. Your task is to complete this trace table for the whole program.
FDE Stage
PC
MAR
MDR
CIR
ACC
Output?
– RESET –
0
0
0
0
0
Fetch
1
0
INP
INP
0
Execute
1
0
INP
INP
5
Fetch
2
1
STA 99
STA 99
5
Execute
2
99
5
STA 99
5
…
…
…
…
…
…
…
Challenge #3:
When tracing the following algorithm we will use the following input value:
First input: 3
INP
STA 99
LDA 99
SUB one
STA 99
OUT
BRP 3
HLT
one DAT 1
Trace Table
We have started to complete the trace table for the first two FDE cycles of this program. Your task is to complete this trace table for the whole program.
Zara has created the following program in her computer science lesson. The aim of this program is to find out if the user is old enough to vote or not, the voting age being 18 years old. If the user is too young to vote, the program will calculate and output in how many years the user will be allowed to vote!
Here is her Python code:
#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!")
Zara made two errors in her code. A syntax error and a logic error. Identify these two errors and explain how these could be fixed.
Syntax Error:
Logic Error:
Question 2[2 marks]
Explain the difference between a logic error and a syntax error.
Question 3[3 marks]
In order to test her program, Zara is designing a test plan. She wants to use specific input values to complete three types of tests:
Normal/Valid Test
Boundary/Borderline/Extreme Test
Erroneous Test
Identify three input values that Zara could use in her test plan.
Valid/Normal Test Data:
Boundary Test Data:
Erroneous Test Data:
Question 4[2 marks]
Identify two purposes of testing.
Question 5[2 marks]
Identify two ways, Zara could use to make her code easier to understand and maintain.
Question 6[5 marks]
Zara would like to create a function using her code. The function should be called checkVotingAge(). It should
take one parameter (the age of the user as an integer value),
output the messages as it currently does
return a Boolean value as to whether the user is allowed to vote or not.
Rewrite Zara’s code into a function.
Solution...
The solution for this challenge is available to full members! Find out how to become a member: ➤ Members' Area
Somjay has created the following program in his computer science lesson. The aim of this program is to generate and display five random “lucky” numbers every time this program is executed.
Here is his Python code:
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)
Somjay has received feedback from his teacher who asked him to rewrite his code using iteration. Rewrite this code to take into consideration the feedback Somjay received from his teacher.
Question 2[3 marks]
Somjay is investigating a method to save all the numbers from a list into a text file, one number per line. He has typed the following code to do so:
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()
His teacher believes this code is not as efficient as it could be, especially if the list of numbers was to increase. He believes this code could easily be refined to minimise the number of times the text file is opened and closed when the code is run. Rewrite this code to improve its efficiency based on this feedback.
Question 3[4 marks]
Somjay is now focusing on a piece of code that should ask the user to enter a star rating (a value between 0 and 5) and display an error message if the star rating is invalid.
Here is Somjay’s code:
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!")
Somjay would like to refine this code so that the computer keeps asking the user to enter a rating until a valid rating is being entered. He believes this could be done using a while loop.