Paper 2 Algorithmic Representation Drills
These are original topic-local Paper 2-style drills, not a complete four-question Paper 2. They use exact function names, return values, test calls, and expected output evidence.
Detailed answers are in Paper 2 Algorithmic Representation Answers.
Revise the topic hub first:
Questions
Question 1: Pseudocode to Python
Convert this pseudocode into a Python function ticket_type(age).
IF age < 13 THEN
result <- "Child"
ELSE IF age < 60 THEN
result <- "Adult"
ELSE
result <- "Senior"
ENDIF
RETURN resultTest:
print(ticket_type(10))
print(ticket_type(35))
print(ticket_type(60))Expected output:
Child
Adult
Senior[6]
Question 2: Sentinel Loop
Write a function total_before_sentinel(values) that represents this sentinel-controlled algorithm:
- read values from the list in order;
- stop when the value is
-1; - return the sum of values before
-1; - ignore
-1and any values after-1.
Test:
print(total_before_sentinel([12, 8, 15, -1, 100]))
print(total_before_sentinel([-1, 50]))Expected output:
35
0[6]
Question 3: Decision Table Function
A learner may start an online quiz only when:
logged_in AND quiz_open AND attempts_leftWrite a function quiz_action(logged_in, quiz_open, attempts_left) that returns "START" or "WAIT".
Test:
print(quiz_action(True, True, True))
print(quiz_action(True, True, False))
print(quiz_action(False, True, True))Expected output:
START
WAIT
WAIT[5]
Question 4: Trace by Print
Write a function trace_double_total() that follows this algorithm and prints the state after each iteration.
count <- 1
total <- 0
WHILE count <= 4
total <- total + (count * 2)
OUTPUT count, total
count <- count + 1
ENDWHILEExpected output:
1 2
2 6
3 12
4 20[5]
Question 5: Modular Program
Write four functions:
get_hours()returns[2, 0, 3];calculate_total(hours)returns the total number of hours;format_total(total)returns"Total hours: 5"for the test data;main()calls the functions in sequence and prints the display string.
Expected output when main() is called:
Total hours: 5[8]
Question 6: Flowchart Implementation
Implement the following flowchart description as a function count_passing(marks).
Start
count <- 0
FOR each mark in marks
Decision: mark >= 50?
Yes: count <- count + 1
No: do not change count
NEXT mark
Return count
EndTest:
print(count_passing([72, 49, 50, 38, 91]))Expected output:
3[7]
Question 7: Validation Function
Write and test a function valid_percentage(mark) that returns True only when mark is an integer from 0 to 100 inclusive.
Test:
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[5]
Question 8: Pseudocode Bug Fix
This flawed condition rejects valid boundary marks:
IF mark > 0 AND mark < 100 THEN
valid <- True
ELSE
valid <- False
ENDIFWrite a corrected Python function fixed_valid_percentage(mark) that returns True only when mark is an integer from 0 to 100 inclusive.
Test:
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[6]
Question 9: Complete Decision-Table Tests
Reuse quiz_action(logged_in, quiz_open, attempts_left) from Question 3.
Write a function quiz_decision_table_tests() that returns a list of result strings for these eight cases, in the given order:
[
(True, True, True),
(True, True, False),
(True, False, True),
(True, False, False),
(False, True, True),
(False, True, False),
(False, False, True),
(False, False, False)
]Expected output:
['START', 'WAIT', 'WAIT', 'WAIT', 'WAIT', 'WAIT', 'WAIT', 'WAIT'][5]
Question 10: Program Skeleton
Create a runnable Python skeleton for a booking program with these functions:
read_booking_request()validate_request(request)store_booking(requests, request)display_summary(requests)main()
For this skeleton, use placeholders so that running main() prints:
Accepted bookings: 0[5]
Review Checklist
After attempting these questions, check whether you can:
- translate pseudocode selection and loops into Python;
- distinguish return values from printed output;
- implement a decision-table rule as Boolean logic;
- print trace evidence without changing the algorithm;
- split code into named modules with clear responsibilities;
- test boundary cases and all major actions.