9618/23/M/J/25 question 5: worked solution (May/June 2025, paper 23)
Show as plain textHide plain text
Cambridge AS Level Computer Science 9618 · 11.1 Programming Basics Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 1 of 3. Q31 9618/23/M/J/25 original Q5 Structured · 8 marks Worked solution Count the '1' characters one at a time. If the count is odd, add '1'; if even, add '0'. FUNCTION Parity(BitString : STRING) RETURNS STRING DECLARE Index, Count : INTEGER Count ← 0 FOR Index ← 1 TO LENGTH(BitString) IF MID(BitString, Index, 1) = '1' THEN Count ← Count + 1 ENDIF NEXT Index IF Count MOD 2 = 1 THEN BitString ← BitString & '1' ELSE BitString ← BitString & '0' ENDIF RETURN BitString ENDFUNCTION Function heading with STRING parameter, RETURNS STRING and ENDFUNCTION [1] Loop for the length of BitString [1] Current character extracted inside the loop (MID) [1] Character compared with '1' (or '0') inside the loop [1] Count increased inside the loop, and set to 0 before it [1] Test for an even or odd number of 1s (MOD 2) [1] '1' or '0' appended as needed with & [1] Amended BitString returned [1] KEY IDEA Count the 1s with a loop, use MOD 2 to see if the count is odd or even, then append the matching bit. TOTAL 8 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Awab Aqib. Tap the picture to open it on YouTube.