9618/22/M/J/22 question 8: worked solution (May/June 2022, paper 22)

Show as plain text

Cambridge AS Level Computer Science 9618 · 10.2 Arrays Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4. Q17 9618/22/M/J/22 original Q8 Structured · 19 marks Worked solution (a) Check each character of the string against the search character. Stop as soon as there is a match. The compare is case sensitive, so do not change the case. FUNCTION Exists(ThisString : STRING, Search : CHAR) RETURNS BOOLEAN DECLARE Found : BOOLEAN DECLARE Index : INTEGER Found ← FALSE Index ← 1 WHILE Found = FALSE AND Index <= LENGTH(ThisString) IF MID(ThisString, Index, 1) = Search THEN Found ← TRUE ELSE Index ← Index + 1 ENDIF ENDWHILE RETURN Found ENDFUNCTION Conditional loop that runs while the character is not found and not at the end of the string [1] A character extracted from the string inside the loop (MID) [1] Compared with the parameter without changing case [1] If it matches, the loop ends (Found set to TRUE) [1] A BOOLEAN value returned [1] A FOR loop with an immediate RETURN TRUE and RETURN FALSE after the loop also scores. (b) Compare each password with all the passwords after it. Skip empty elements and decrypt before comparing. Stop after the first password that has duplicates. PROCEDURE SearchDuplicates() DECLARE IndexA, IndexB : INTEGER DECLARE ThisPassword : STRING DECLARE Duplicates : BOOLEAN Duplicates ← FALSE IndexA ← 1 WHILE Duplicates = FALSE AND IndexA < 500 IF Secret[IndexA, 2] <> "" THEN ThisPassword ← Decrypt(Secret[IndexA, 2]) FOR IndexB ← IndexA + 1 TO 500 IF Secret[IndexB, 2] <> "" THEN IF Decrypt(Secret[IndexB, 2]) = ThisPassword THEN OUTPUT "Password for ", Secret[IndexA, 1], " also used for ", Secret[IndexB, 1] Duplicates ← TRUE ENDIF ENDIF NEXT IndexB ENDIF IndexA ← IndexA + 1 ENDWHILE IF Duplicates = FALSE THEN OUTPUT "No duplicate passwords found" ENDIF ENDPROCEDURE A conditional loop [1] ...running from 1 to 499 while no duplicate has been found [1] Unused (empty) password elements skipped [1] Decrypt() used and its result assigned to ThisPassword [1] Inner loop from the outer index + 1 to 500 [1] ThisPassword compared with each later password after it is decrypted [1] If a match is found, the outer loop is ended [1] An output message giving both website names is attempted [1] Other points that also score: "No duplicate passwords found" output after the loop if there were none. (c) Describe the steps in words: Start with an empty string for the password and return it at the end [1] Use two loops to make three groups of four characters (or one loop for 12 characters) [1] Use RandomChar() in the loop to get each character [1] Use Exists() to check the character is not already in the password: reject it if TRUE, otherwise add it to the string [1] Put a hyphen between the three groups [1] The result is three groups of four characters with hyphens and no repeated characters (a fully working algorithm) [1] KEY IDEA Decrypt before comparing, only compare each pair once, and build passwords by rejecting any random character that already exists. TOTAL 19 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Awab Aqib. Tap the picture to open it on YouTube.