Paper 2 Algorithmic Representation Answers
These answers correspond to Paper 2 Algorithmic Representation Drills.
Verification note: every Python code block in this answer file has been executed locally.
Answer 1: Pseudocode to Python
Model answer:
def grade(score):
if score >= 70:
result = "A"
elif score >= 50:
result = "Pass"
else:
result = "Fail"
return result
print(grade(82))
print(grade(55))
print(grade(40))Expected output:
A
Pass
FailMark points:
- defines
grade(score); - uses
iffor the first condition; - uses
eliffor the second condition; - uses
elsefor the remaining case; - returns the selected result;
- produces all three expected outputs.
Common weak answer:
- using three separate
ifstatements that can overwrite the earlier result.
Answer 2: Sentinel Loop
Model answer:
def sum_until_stop(values):
total = 0
for value in values:
if value == -1:
break
total = total + value
return total
print(sum_until_stop([4, 7, -1, 100]))Expected output:
11Mark points:
- initializes an accumulator;
- loops through the list in order;
- checks for the sentinel
-1; - stops when the sentinel is reached;
- adds only non-sentinel values before the sentinel;
- ignores values after the sentinel and returns
11.
Common weak answer:
- summing all values and subtracting
-1. Values after the sentinel must not be processed.
Answer 3: Decision Table Function
Model answer:
def access_action(logged_in, paid_fee, not_suspended):
if logged_in and paid_fee and not_suspended:
return "ALLOW"
return "DENY"
print(access_action(True, True, True))
print(access_action(True, True, False))
print(access_action(False, True, True))Expected output:
ALLOW
DENY
DENYMark points:
- defines the function with three Boolean parameters;
- combines the conditions with
and; - returns
"ALLOW"only when all are true; - returns
"DENY"otherwise; - handles the
not_suspended = Falsecase; - handles the
logged_in = Falsecase; - uses exact output strings;
- matches all expected outputs.
Common weak answer:
- using
or, which would allow access when only one condition is true.
Answer 4: Trace by Print
Model answer:
def trace_total():
count = 1
total = 0
while count <= 3:
total = total + count
print(count, total)
count = count + 1
trace_total()Expected output:
1 1
2 3
3 6Mark points:
- initializes
countto1; - initializes
totalto0; - uses the correct loop condition;
- prints the state after updating
total.
Common weak answer:
- printing before the update, which produces a different trace.
Answer 5: Modular Program
Model answer:
def get_marks():
return [60, 75, 45]
def calculate_average(marks):
return sum(marks) / len(marks)
def display_average(average):
return "Average: " + str(average)
marks = get_marks()
average = calculate_average(marks)
message = display_average(average)
print(message)Expected output:
Average: 60.0Mark points:
- defines a separate input/data function;
- returns the specified list of marks;
- defines a processing function;
- correctly calculates the average;
- defines an output-formatting function;
- returns the exact display string;
- calls the functions in a sensible sequence;
- prints the expected output.
Common weak answer:
- putting all logic into one block without the required modules.
Answer 6: Flowchart Implementation
Model answer:
def count_even(numbers):
count = 0
for number in numbers:
if number % 2 == 0:
count = count + 1
return count
print(count_even([2, 5, 8, 9, 10]))Expected output:
3Mark points:
- initializes
countto0; - loops through each number;
- tests divisibility by
2; - increments the count for even numbers;
- does not increment for odd numbers;
- returns or outputs the final count;
- matches the expected output.
Common weak answer:
- counting odd numbers by using
number % 2 == 1.
Answer 7: Validation Function
Model answer:
def valid_mark(mark):
return type(mark) == int and mark >= 0 and mark <= 100
print(valid_mark(-1))
print(valid_mark(0))
print(valid_mark(100))
print(valid_mark(101))
print(valid_mark(50.5))Expected output:
False
True
True
False
FalseMark points:
- defines
valid_mark(mark); - checks that the input is an integer;
- checks both lower and upper boundaries;
- includes both endpoints as valid while rejecting non-integers;
- produces the expected test outputs.
Common weak answer:
- using strict comparisons
mark > 0 and mark < 100, which rejects boundary values. - checking only the numeric range, which would wrongly accept
50.5.
Answer 8: Pseudocode Bug Fix
Model answer:
def fixed_valid_mark(mark):
if mark >= 0 and mark <= 100:
return True
return False
print(fixed_valid_mark(0))
print(fixed_valid_mark(50))
print(fixed_valid_mark(100))Expected output:
True
True
TrueMark points:
- changes the lower boundary to
>= 0; - changes the upper boundary to
<= 100; - keeps
andso both limits must be satisfied; - returns a Boolean result;
- tests the lower boundary;
- tests the upper boundary.
Common weak answer:
- changing
andtoor, which would accept invalid marks such as-5or120.
Answer 9: Decision Table Tests
Model answer:
def access_action(logged_in, paid_fee, not_suspended):
if logged_in and paid_fee and not_suspended:
return "ALLOW"
return "DENY"
def decision_table_tests():
cases = [
(True, True, True),
(True, True, False),
(True, False, True),
(True, False, False),
(False, True, True),
(False, True, False),
(False, False, True),
(False, False, False)
]
results = []
for logged_in, paid_fee, not_suspended in cases:
results.append(access_action(logged_in, paid_fee, not_suspended))
return results
print(decision_table_tests())Expected output:
['ALLOW', 'DENY', 'DENY', 'DENY', 'DENY', 'DENY', 'DENY', 'DENY']Mark points:
- includes all eight combinations for the three Boolean inputs;
- includes the all-true allow case;
- includes denial cases where
not_suspendedisFalse; - includes denial cases where
paid_feeisFalse; - includes denial cases where
logged_inisFalse;
Common weak answer:
- testing only the allow case or only a few denial cases. A complete decision-table test for three Boolean inputs has eight cases.
Answer 10: Program Skeleton
Model answer:
def load_questions():
return []
def ask_questions(questions):
return 0
def display_score(score):
print("Score:", score)
def main():
questions = load_questions()
score = ask_questions(questions)
display_score(score)
main()Expected output:
Score: 0Mark points:
- defines
load_questions; - defines
ask_questionswith a parameter; - defines
display_score; - defines
mainand calls the functions in order.
Common weak answer:
- writing only comments without a runnable skeleton.