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 ticket_type(age):
    if age < 13:
        result = "Child"
    elif age < 60:
        result = "Adult"
    else:
        result = "Senior"
    return result
 
 
print(ticket_type(10))
print(ticket_type(35))
print(ticket_type(60))

Expected output:

Child
Adult
Senior

Mark points:

  • defines ticket_type(age);
  • uses if for the first condition;
  • uses elif for the second condition;
  • uses else for the remaining case;
  • returns the selected result;
  • produces all three expected outputs.

Common weak answer:

  • using three separate if statements that can overwrite an earlier result.

Answer 2: Sentinel Loop

Model answer:

def total_before_sentinel(values):
    total = 0
    for value in values:
        if value == -1:
            break
        total = total + value
    return total
 
 
print(total_before_sentinel([12, 8, 15, -1, 100]))
print(total_before_sentinel([-1, 50]))

Expected output:

35
0

Mark points:

  • initializes an accumulator;
  • loops through the list in order;
  • checks for the sentinel -1;
  • stops when the sentinel is reached;
  • adds only values before the sentinel;
  • ignores values after the sentinel and produces both expected outputs.

Common weak answer:

  • summing the whole list. Values after the sentinel must not be processed.

Answer 3: Decision Table Function

Model answer:

def quiz_action(logged_in, quiz_open, attempts_left):
    if logged_in and quiz_open and attempts_left:
        return "START"
    return "WAIT"
 
 
print(quiz_action(True, True, True))
print(quiz_action(True, True, False))
print(quiz_action(False, True, True))

Expected output:

START
WAIT
WAIT

Mark points:

  • defines the function with three Boolean parameters;
  • combines the conditions with and;
  • returns "START" only when all are true;
  • returns "WAIT" otherwise;
  • produces the three stated test outputs exactly.

Common weak answer:

  • using or, which would start the quiz when only one condition is true.

Answer 4: Trace by Print

Model answer:

def trace_double_total():
    count = 1
    total = 0
    while count <= 4:
        total = total + (count * 2)
        print(count, total)
        count = count + 1
 
 
trace_double_total()

Expected output:

1 2
2 6
3 12
4 20

Mark points:

  • initializes count to 1;
  • initializes total to 0;
  • uses the correct loop condition;
  • updates total using count * 2;
  • 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_hours():
    return [2, 0, 3]
 
 
def calculate_total(hours):
    return sum(hours)
 
 
def format_total(total):
    return "Total hours: " + str(total)
 
 
def main():
    hours = get_hours()
    total = calculate_total(hours)
    message = format_total(total)
    print(message)
 
 
main()

Expected output:

Total hours: 5

Mark points:

  • defines a separate input/data function;
  • returns the specified list;
  • defines a processing function;
  • correctly calculates the total;
  • defines an output-formatting function;
  • returns the exact display string;
  • defines main() and calls the functions in 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_passing(marks):
    count = 0
    for mark in marks:
        if mark >= 50:
            count = count + 1
    return count
 
 
print(count_passing([72, 49, 50, 38, 91]))

Expected output:

3

Mark points:

  • initializes count to 0;
  • loops through each mark;
  • tests whether mark >= 50;
  • increments the count for passing marks;
  • does not increment for failing marks;
  • returns the final count;
  • matches the expected output.

Common weak answer:

  • using mark > 50, which wrongly excludes the boundary mark 50.

Answer 7: Validation Function

Model answer:

def valid_percentage(mark):
    return type(mark) == int and mark >= 0 and mark <= 100
 
 
print(valid_percentage(-1))
print(valid_percentage(0))
print(valid_percentage(100))
print(valid_percentage(101))
print(valid_percentage(50.5))

Expected output:

False
True
True
False
False

Mark points:

  • defines valid_percentage(mark);
  • checks that the input is an integer;
  • checks the lower boundary;
  • checks the upper boundary;
  • produces the expected test outputs.

Common weak answer:

  • checking only the numeric range, which would wrongly accept 50.5.

Answer 8: Pseudocode Bug Fix

Model answer:

def fixed_valid_percentage(mark):
    return type(mark) == int and mark >= 0 and mark <= 100
 
 
print(fixed_valid_percentage(0))
print(fixed_valid_percentage(50))
print(fixed_valid_percentage(100))
print(fixed_valid_percentage(50.5))

Expected output:

True
True
True
False

Mark points:

  • changes the lower boundary to >= 0;
  • changes the upper boundary to <= 100;
  • keeps and so both limits must be satisfied;
  • checks that the mark is an integer;
  • tests the lower boundary;
  • tests the upper boundary and rejects the non-integer case.

Common weak answer:

  • changing and to or, which would accept invalid marks such as -5 or 120.

Answer 9: Complete Decision-Table Tests

Model answer:

def quiz_action(logged_in, quiz_open, attempts_left):
    if logged_in and quiz_open and attempts_left:
        return "START"
    return "WAIT"
 
 
def quiz_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, quiz_open, attempts_left in cases:
        results.append(quiz_action(logged_in, quiz_open, attempts_left))
    return results
 
 
print(quiz_decision_table_tests())

Expected output:

['START', 'WAIT', 'WAIT', 'WAIT', 'WAIT', 'WAIT', 'WAIT', 'WAIT']

Mark points:

  • includes all eight combinations for the three Boolean inputs;
  • includes the all-true start case;
  • includes waiting cases where attempts_left is False;
  • includes waiting cases where quiz_open is False;
  • includes waiting cases where logged_in is False.

Common weak answer:

  • testing only the start case or only a few waiting cases. A complete decision-table test for three Boolean inputs has eight cases.

Answer 10: Program Skeleton

Model answer:

def read_booking_request():
    return None
 
 
def validate_request(request):
    return False
 
 
def store_booking(requests, request):
    if validate_request(request):
        requests.append(request)
 
 
def display_summary(requests):
    print("Accepted bookings:", len(requests))
 
 
def main():
    requests = []
    request = read_booking_request()
    store_booking(requests, request)
    display_summary(requests)
 
 
main()

Expected output:

Accepted bookings: 0

Mark points:

  • defines read_booking_request;
  • defines validate_request(request);
  • defines store_booking(requests, request);
  • defines display_summary(requests);
  • defines main() and calls the functions in a sensible sequence.

Common weak answer:

  • writing only comments without a runnable skeleton.