Additional Programming Revision (1)
Additional Programming Revision (1)
Date: ________________________
Time: 88 minutes
Marks: 71 marks
Comments:
if numThree == -1:
print(f"Area {answer}")
else:
print(f"Volume {answer}")
(a) Complete the trace table using the program in the figure above.
5 6 –1
10 4 0
3 5 10
(3)
(b) Describe one way that the program in the figure above could be made more robust.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(1)
(Total 4 marks)
Figure 2 shows part of a Python program that checks the grid reference entered by a player.
The grid reference is valid if:
• there are exactly two characters
• the first character entered is A, B or C
• the second character entered is 1, 2 or 3.
Figure 2
check = False
while check == False:
square = ""
while len(square) != 2:
square = input("Enter grid reference (eg C2): ")
square = square.upper()
The Python function upper() converts letters into uppercase, e.g. b1 would be converted to B1
You should use indentation as appropriate, meaningful variable name(s) and Python syntax in
your answer.
The answer grid contains vertical lines to help you indent your code.
(Total 6 marks)
square = ""
while len(square) != 2:
square = square.upper()
Examples:
• if the value of reg is R0 or R9 then regValid should be True
• if the value of reg is r6 or Rh then regValid should be False
You may wish to use the subroutine isDigit(ch) in your answer. The subroutine isDigit
returns True if the character parameter ch is a string representation of a digit and False
otherwise.
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
(Total 3 marks)
Figure 1 shows an example of how the tiles might be arranged on the board at the start of the
game with the blank space in the position (0, 1).
Figure 2 shows the correct final positions for the tiles when the puzzle is solved.
Figure 1 Figure 2
Subroutine Purpose
Figure 3 Figure 4
Figure 5
if getTile(1, 0) == 0:
move(2, 0)
if getTile(2, 0) == 0:
move(2, 1)
displayBoard()
Figure 6
Complete the board to show the new positions of the tiles after the program in Figure 5 is run.
(Total 2 marks)
Figure 1 shows an example of how the tiles might be arranged on the board at the start of the
game with the blank space in the position (0, 1).
Figure 2 shows the correct final positions for the tiles when the puzzle is solved.
Figure 1 Figure 2
Table 1
Subroutine Purpose
Figure 3 Figure 4
Table 2
Subroutine Purpose
Figure 5 Figure 6
For example:
• for the board in Figure 5, the program would display No
• for the board in Figure 6, the program would display Yes
You should use indentation as appropriate, meaningful variable name(s) and Python syntax in
your answer.
The answer grid below contains vertical lines to help you indent your code accurately.
(Total 4 marks)
Figure 1 shows an example of how the tiles might be arranged on the board at the start of the
game with the blank space in the position (0, 1).
Figure 2 shows the correct final positions for the tiles when the puzzle is solved.
Figure 1 Figure 2
Table 1
Subroutine Purpose
Figure 3 Figure 4
Table 2
Subroutine Purpose
Table 3 shows a description of the move subroutine previously described in more detail in Table
1.
Table 3
Subroutine Purpose
You should use indentation as appropriate, meaningful variable name(s) and Python syntax in
your answer.
The answer grid below contains vertical lines to help you indent your code accurately.
(Total 6 marks)
SUBROUTINE calculate(n)
a ← n
b ← 0
REPEAT
a ← a DIV 2
b ← b + 1
UNTIL a ≤ 1
OUTPUT b
ENDSUBROUTINE
(a) Complete the trace table for the subroutine call calculate(50)
You may not need to use all the rows in the table.
n a b OUTPUT
50
(4)
(b) State the value that will be output for the subroutine call calculate(1)
___________________________________________________________________
___________________________________________________________________
(1)
State a better identifier for this variable that makes the algorithm easier to read and
understand.
___________________________________________________________________
___________________________________________________________________
(1)
Figure 1
SUBROUTINE calculate(n)
a ← n
b ← 0
REPEAT
a ← a DIV 2
b ← b + 1
UNTIL a ≤ 1
OUTPUT b
ENDSUBROUTINE
Figure 2
SUBROUTINE calculate(n)
a ← n
b ← 0
WHILE a > 1
a ← a DIV 2
b ← b + 1
ENDWHILE
OUTPUT b
ENDSUBROUTINE
One difference in the way the subroutines in Figure 1 and Figure 2 work is:
• the REPEAT…UNTIL iteration structure in Figure 1 loops until the condition is true
• the WHILE…ENDWHILE iteration structure in Figure 2 loops until the condition is false.
1. ________________________________________________________________
___________________________________________________________________
___________________________________________________________________
2. ________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(Total 8 marks)
SUBROUTINE decrease(n)
result ← n DIV 2
RETURN result
ENDSUBROUTINE
SUBROUTINE increase(n)
result ← (3 * n) + 1
RETURN result
ENDSUBROUTINE
SUBROUTINE isEven(n)
IF (n MOD 2) = 0 THEN
RETURN True
ELSE
RETURN False
ENDIF
ENDSUBROUTINE
The first output has already been written in the trace table. You may not need to use all
rows of the table.
Output
(4)
(b) Describe how the developer has used the structured approach to programming.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(Total 6 marks)
• if the final score is 21, output a message to say the player has won
• if the final score is greater than 21, output a message to say the player has lost
• if the final score is less than 21, the program generates a random number between 15 and
21 inclusive:
○ if this random number is greater than the player’s final score, output a message to
say the player has lost
○ otherwise, output a message to say the player has won.
The figure below shows the output of a program that plays this dice game.
Roll 1: 1
Roll 2: 4
Current score: 5
Would you like to roll again? yes
Roll 1: 1
Roll 2: 6
Current score: 12
Would you like to roll again? yes
Roll 1: 1
Roll 2: 2
Current score: 15
Would you like to roll again? yes
Roll 1: 6
Roll 2: 1
Current score: 22
You lost!
The first line has been written for you in the answer grid.
The dice rolls are carried out by the program generating random numbers between 1 and 6. You
will need to use the Python function random.randrange(a, b) which generates a random
integer in the range a to b starting at a but finishing one before b.
You should use indentation as appropriate, meaningful variable name(s) and Python syntax in
your answer.
The answer grid below contains vertical lines to help you indent your code.
(Total 11 marks)
Complete the trace table for the algorithm in the figure above.
You may not need to use all the rows in the table.
weeks
i daysTotal weeksTotal
[0] [1] [2]
0 0 0
(Total 6 marks)
(1)
(b) The figure below shows an incomplete algorithm, represented using pseudo-code.
Complete the figure below by filling in the gaps using the items in the table below.
• You may need to use some of the items in the table more than once.
• You will not need to use all the items in the table.
1 2 author
B1 B2 Book
bookName i Real
RECORD _____________
bookName : String
_____________ : String
price : ______________
ENDRECORD
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(3)
(Total 7 marks)
The figure below shows a binary search algorithm that has been programmed in Python.
12.
animals = ["cat", "dog", "hippo", "llama", "ox",
"rat", "tiger", "wolf"]
animalToFind = input("What animal would you like to
find? ")
validAnimal = False
start = 0
finish = len(animals) - 1
while validAnimal == False and start <= finish:
mid = (start + finish) // 2
if animals[mid] == animalToFind:
validAnimal = True
elif animalToFind > animals[mid]:
start = mid + 1
else:
finish = mid - 1
print(validAnimal)
You may not need to use all the rows in the table.
wolf False 0 7 3
(Total 4 marks)
(Total 4 marks)
• Change data types used in the question to float / single / double / decimal / real
for inputs;
1
[4]
Mark A for using the variable check within their own code;
Mark B for using selection or equivalent to check the grid references;
I. Case
I. Gaps/spaces throughout the code, except where to do so would
explicitly alter the logic of the code in a way that makes it incorrect.
check = False
while check == False:
square = ""
while len(square) != 2:
square = input("Enter grid reference: ")
square = square.upper()
(only 2 marks awarded as the answer contains an error in the Boolean condition)
[3]
A. 0 instead of blank space or any other sensible indicator for the blank space.
A. unaffected cell contents not shown as long as it is clear which is the blank space.
A. answers written on the diagram below if board is left blank.
[2]
Mark A for the use of a selection structure with multiple conditions // use of multiple
selection structures // an iteration structure containing one selection structure;
Program Logic
Mark B for correctly checking three consecutive values in getTile
(even if the wrong row/column);
Mark C for fully correct indices used in getTile for the first row;
Mark D for a structure that would output either Yes or No correctly in all circumstances, but
never both; A. if conditions are not fully correct
I. Case
I. Messages or no messages with input statements
I. Gaps/spaces throughout the code, except where to do so would explicitly alter the logic
of the code in a way that makes it incorrect
Note to examiners: in a nested if statement, all pathways must be present to award Mark
D (including the part shaded yellow above).
Note to examiners: in a nested if statement, all pathways must be present to award Mark
D (including the part shaded yellow above).
Mark A for the use of an indefinite iteration structure that exists within their language;
Mark B for the use of a selection structure or equivalent to check for a blank space;
Program Logic
Mark C for using user input and storing the result in two variables correctly for the row and
column;
Mark D for code that uses both the solved subroutine and the checkSpace subroutine in
logically correct locations;
Mark E for calling the move subroutine in a pathway following an = True condition (or
equivalent) with the row and column from the user input as parameters;
Mark F for outputting Invalid move when the tile does not get moved and asking the user
to input row and column again in logically correct locations; R. if user is asked to re-input
after the problem is solved.
I. Case
I. Messages or no messages with input statements
I. Gaps/spaces throughout the code, except where to do so would explicitly alter the logic
of the code in a way that makes it incorrect
1;
1 mark for giving a new identifier that describes this purpose, eg count //
total // times // numberOfTimes // counter
1
• If the value of n is 1 (or less) then the REPEAT…UNTIL structure will cause
the value of a/b to change, but the WHILE…ENDWHILE structure will not;
Max 2 from:
Program Design
Note that AO3 (design) marks are for selecting appropriate techniques to
use to solve the problem, so should be credited whether the syntax of
programming language statements is correct or not and regardless of
whether the solution works.
Program Logic
I. Case
I. Gaps/spaces throughout the code, except where to do so would
explicitly alter the logic of the code in a way that makes it incorrect.
I. Messages or no messages with input statements
import random
score = 0
rollAgain = "yes"
(C, Part of G,
while rollAgain == "yes":
Part of H)
dice1 = random.randrange(1, 7) (Part of A,E)
dice2 = random.randrange(1, 7) (Part of A,E)
score = score + dice1 + dice2 (F)
print(f"Roll 1: {dice1}") (D)
print(f"Roll 2: {dice2}")
print(f"Current score: {score}")
if score < 21: (Part of G)
rollAgain = input() (Part of G)
else:
rollAgain = "no"
if score > 21: (Part of H)
print("You lost! ") (Part of I)
elif score == 21: (Part of K)
print("You won! ") (Part of I)
else: (Part of K)
if random.randrange(15,22) > score: (Part of I)
print("You lost!") (J)
else: (Part of K)
print("You won! ") (Part of K)
A. random.randint(1, 6)
A. random.randint(15, 21)
[11]
1 mark for the correct total of weeks[0], weeks[1] and weeks[2] in the final column and
no other value;
I. preceding zeroes
A. follow through value as long as the total is correct for the three final values the student
has written in the weeks columns.
1 RECORD Book
2 bookName : String
3 author : String
4 price : Real
5 ENDRECORD
I. Case
3
1 mark for correctly using a selection structure with multiple conditions // use of
multiple selection structures to compare B1 and B2 in some way (even if Boolean
conditions incorrect);
I. Case
A. Pseudo-code statements written using different syntax as long as the logic is still
correct.
3
[7]
[4]