Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 1 of 3.
Test the highest return value first, so the highest possible value is returned. Use LEFT, RIGHT and MID to pick out the digits:
FUNCTION TestNum(ThisNum : STRING) RETURNS INTEGER
IF LEFT(ThisNum, 3) = RIGHT(ThisNum, 3) THEN
RETURN 3
ENDIF
IF RIGHT(ThisNum, 3) = "000" THEN
RETURN 2
ENDIF
IF MID(ThisNum, 4, 1) = MID(ThisNum, 5, 1) AND MID(ThisNum, 5, 1) = MID(ThisNum, 6, 1) THEN
RETURN 1
ENDIF
RETURN 0
ENDFUNCTIONFunction heading and ending with the STRING parameter and INTEGER return type [1]
Test for return value 3: first three digits equal last three digits [1]
Test for return value 2: last three digits are "000" [1]
Test for return value 1: last three digits all the same [1]
The highest value is returned when more than one condition is true (3 is tested before 2 before 1) [1]
Zero returned when no condition is met [1]
▶ Starts at Q6 (1:33:40)
Starts at Q6 (1:33:40) · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.