- May 20, 2023
Mastering Cambridge 9618 Paper 4 Practical: Past Paper Solutions Part 1
- Chenming Yong
- 0 comments
YouTube Video
What You Will Need
2022 October/November - 9618/42 Question Paper
2022 October/November - 9618/42 Mark Scheme
2022 October/November - 9618/42 evidence.doc
2022 October/November - 9618/42 Characters.txt
Free download using this link
Q1
Visualisation
For python, when we define the variables outside function definition, it will be a global variable.
Since python variables are dynamically typed which means the variable type will be assigned
Based on the value being assigned, we do not have to define whether it's an integer or 2D array.
Jobs # global array, 100 by 2 elements
NumberOfJobs # global integerAlthough personally, it would be better if we write the answer this way. At least, we know that Jobs is a list which will become a 2D array later on and we know that NumberOfJobs is an integer data type.
Jobs = []
NumberOfJobs = 0The key idea here is that we will be updating both Jobs array and NumberOfJobs inside the function which means we have to use the keyword global within the function.
For NumberOfJobs is just assignment, which is pretty easy 😊
For Jobs array, we will just insert row by row with both columns -1 until we finish inserting 100 rows!
Which loop to use? What is the range? How to insert 1 rows with 2 columns into an array?
def Initialise():
global Jobs
global NumberOfJobs
for x in range(0,100):
Jobs.append([-1,-1])
NumberOfJobs = 0Two input parameters 👉🏼 job number, priority
How do we know which is the next free array element in Jobs array? 🤔
Well, we can just use NumberOfJobs as the index to track since it starts from 0.
What is the possible case that we can't store the elements into the Jobs array?
When the array is FULL! 🌝 Which means when the value of NumberOfJobs is 100!
🚨 Take note that to store 100 elements in array, we are using index 0 to index 99
Print "Not added" statement when array is full (NumberOfJobs is 100)
Print "Added" statement when array is not full and assign value into Jobs row by row
def AddJob(JobNumber, Priority):
global NumberOfJobs
global Jobs
if NumberOfJobs == 100:
print("Not added")
else:
Jobs[NumberOfJobs] = [JobNumber, Priority]
print("Added")
NumberOfJobs = NumberOfJobs + 1First step is call the procedure Initialise(), pretty straightforward 😌
Second step is call the procedure AddJob with input parameters shown 👆🏼
Both inputs are integer
Initialise()
AddJob(12,10)
AddJob(526,9)
AddJob(33,8)
AddJob(12,9)
AddJob(78,1)The array/the table has 2 columns (Job Number, Priority), we need to sort based on Priority
Sort it using Insertion Sort ✅
If you know Insertion Sort by heart, this should be quite doable 🙌
Sample InsertionSort()
SampleArray = [10, 9, 1, 4, 7]
print("Before sorting: ",SampleArray )
def InsertionSort():
LengthOfArray = len(SampleArray) # Get the length of the array
# Iterate over the array starting from the second element
for I in range(1, LengthOfArray):
Key = SampleArray[I] # Current element to be inserted
J = I - 1 # Start comparing with the previous element
# Shift elements greater than key to the right
while J >= 0 and SampleArray[J] > Key:
SampleArray[J+1] = SampleArray[J]
J = J - 1
SampleArray[J+1] = Key # Insert the key at the correct position
InsertionSort()
print("After sorting: ",SampleArray )Solution
def InsertionSort():
global Jobs
global NumberOfJobs # Get the length of the array
# Iterate over the array starting from the second element
for I in range(1, NumberOfJobs):
Key1 = Jobs[I][0] # Current JobNumber to be inserted
Key2 = Jobs[I][1] # Current Priority to be inserted
J = I - 1 # Start comparing with the previous element
# Shift elements greater than key to the right
while J >= 0 and Jobs[J][1] > Key2:
Jobs[J+1][0] = Jobs[J][0]
Jobs[J+1][1] = Jobs[J][1]
J = J - 1
Jobs[J+1][0] = Key1 # Insert the JobNumber at the correct position
Jobs[J+1][1] = Key2 # Insert the Priority at the correct positionLoop through the array row by based based on total number of rows
Print out JobNumber in Column 1 and Priority in Column 2
Most probably need to convert the output to string since input is an integer!
def PrintArray():
global Jobs
global NumberOfJobs
for X in range(0, NumberOfJobs):
print(str(Jobs[X][0]), " priority ", str(Jobs[X][1]))Just call InsertionSort() and PrintArray() procedure, free marks really 🌝
InsertionSort()
PrintArray()Call everything from 1(a) to 1g(i) and print out the output
Jobs = []
NumberOfJobs = 0
def Initialise():
global Jobs
global NumberOfJobs
for x in range(0,100):
Jobs.append([-1,-1])
NumberOfJobs = 0
def AddJob(JobNumber, Priority):
global NumberOfJobs
global Jobs
if NumberOfJobs == 100:
print("Not added")
else:
Jobs[NumberOfJobs] = [JobNumber, Priority]
print("Added")
NumberOfJobs = NumberOfJobs + 1
Initialise()
AddJob(12,10)
AddJob(526,9)
AddJob(33,8)
AddJob(12,9)
AddJob(78,1)
def InsertionSort():
global Jobs
global NumberOfJobs # Get the length of the array
# Iterate over the array starting from the second element
for I in range(1, NumberOfJobs):
Key1 = Jobs[I][0] # Current JobNumber to be inserted
Key2 = Jobs[I][1] # Current Priority to be inserted
J = I - 1 # Start comparing with the previous element
# Shift elements greater than key to the right
while J >= 0 and Jobs[J][1] > Key2:
Jobs[J+1][0] = Jobs[J][0]
Jobs[J+1][1] = Jobs[J][1]
J = J - 1
Jobs[J+1][0] = Key1 # Insert the JobNumber at the correct position
Jobs[J+1][1] = Key2 # Insert the Priority at the correct position
def PrintArray():
global Jobs
global NumberOfJobs
for X in range(0, NumberOfJobs):
print(str(Jobs[X][0]), " priority ", str(Jobs[X][1]))
InsertionSort()
PrintArray()Hope you enjoy this walkthrough, comment below what you think! 😊