9618/43

Computer Science 9618/43October/November 2023

Cambridge A-Level · Practical · worked solutions for every part, with the mark scheme

3
questions
75
marks
150
minutes

Topics Programming Paradigms (Procedural and Object-oriented) · File Processing and Exception Handling · Recursion · Algorithms and Abstract Data Types

Q1Programming Paradigms (Procedural and Object-oriented)RecursionFree sample

This iterative pseudocode algorithm for the function IterativeVowels() takes a string as a parameter and counts the number of lower-case vowels in this string.
The vowels are the letters a, e, i, o and u.

FUNCTION IterativeVowels(Value : STRING) RETURNS INTEGER
  DECLARE Total : INTEGER 
  DECLARE LengthString : INTEGER 
  DECLARE FirstCharacter : CHAR 
  Total ← 0
  LengthString ← LENGTH(Value)
  FOR X ← 0 TO LengthString - 1
    FirstCharacter ← MID(Value, 0, 1)
    IF FirstCharacter = 'a' OR FirstCharacter = 'e' OR
       FirstCharacter = 'i' OR FirstCharacter = 'o' OR 
       FirstCharacter = 'u' THEN
       Total ← Total + 1
    ENDIF
    Value ← MID(Value, 1, LENGTH(Value)-1)
  NEXT X
  RETURN Total
ENDFUNCTION

The pseudocode function MID(X, Y, Z) returns Z number of characters from string X, starting at the character in position Y. The first character in a string is in position 0, for example:

MID("computer", 0, 3) returns "com"

The pseudocode function LENGTH(X) returns the number of characters in the string X, for example:

LENGTH("computer") returns 8

(a)
(i)

Write program code for the function IterativeVowels().

Save your program as Question1_N23.

Copy and paste the program code into part 1(a)(i) in the evidence document.

5M
DifficultyMedium
Worked solution

Answer

def IterativeVowels(Value):
    Total = 0
    LengthString = len(Value)
    for X in range(LengthString):
        FirstCharacter = Value[0:1]
        if FirstCharacter == "a" or FirstCharacter == "e" or FirstCharacter == "i" or FirstCharacter == "o" or FirstCharacter == "u":
            Total = Total + 1
        Value = Value[1:]
    return Total
Final answer

See program code

Detailed explanation

Background Concept

This task is about translating an algorithm from pseudocode into real program code. In Paper 4, that means writing a working program in a high-level language such as Python. The key programming ideas here are:

  • a function takes an input parameter and returns a value
  • a count-controlled loop repeats a fixed number of times
  • string processing lets us inspect one character at a time
  • selection using if checks whether the current character is a vowel
  • an accumulator stores the running total

The given pseudocode does not move through the string using an index. Instead, it repeatedly looks at the first character, then shortens the string by removing that first character. That is an unusual but perfectly valid method, and it is best to copy that same logic into the program.

Understanding the Question

You are given a complete iterative pseudocode function called IterativeVowels() and asked to write the equivalent program code. The function receives a string and counts the number of lower-case vowels: a, e, i, o, u.

Important details from the stem:

  • LENGTH(Value) becomes len(Value) in Python.
  • MID(Value, 0, 1) means take the first character.
  • The string positions start at 0.
  • After each check, the string is shortened by removing its first character.

So the program must not just count vowels somehow; it should follow the same logic as the algorithm given.

Approach

The cleanest approach is to mirror the pseudocode line by line:

  1. Define the function with one parameter.
  2. Set the total count to 0.
  3. Store the original length of the string.
  4. Loop that many times.
  5. Take the first character of the current string.
  6. If it is a vowel, increase the total.
  7. Remove the first character from the string.
  8. Return the total.

Using Value[0:1] is a good Python equivalent of MID(Value, 0, 1) because it returns a one-character string.

Step-by-Step Reasoning

def IterativeVowels(Value):

  • This defines the function with the required identifier and one parameter.

Total = 0

  • This is the accumulator. It starts at zero because no vowels have been counted yet.

LengthString = len(Value)

  • The original length is stored before the loop starts.
  • That matches the pseudocode, which calculates the length once.

for X in range(LengthString):

  • The pseudocode runs from 0 to LengthString - 1, which means exactly LengthString repetitions.
  • In Python, range(LengthString) gives exactly that number of loop cycles.
  • The variable X is not actually used inside the loop, but keeping it makes the translation close to the original algorithm.

FirstCharacter = Value[0:1]

  • This takes the first character from the current version of the string.
  • At the first pass it is the original first character; later it is the new first character after earlier characters have been removed.

The if statement checks all five vowels.

  • If the first character is any one of a, e, i, o, or u, then Total is increased by 1.
  • Only lower-case vowels are checked, because that is what the question states.

Value = Value[1:]

  • This removes the first character from the string.
  • So if Value was house, it becomes ouse, then use, then se, then e, then an empty string.
  • That exactly matches the pseudocode line using MID(Value, 1, LENGTH(Value)-1).

return Total

  • After every character has been processed, the function sends back the count.

Key Takeaways

  • Translate pseudocode by matching each construct to the equivalent programming syntax.
  • A string can be processed either by indexing or by repeatedly shortening it.
  • An accumulator variable is the standard way to count matches in a loop.
  • In Python, len() and slicing are common tools for string algorithms.

Common Mistakes

  • Using range(LengthString - 1) and missing the last character.
  • Forgetting to return the total at the end of the function.
  • Using = instead of == inside the if condition.
  • Checking uppercase vowels even though the question asks for lower-case vowels only.
  • Writing Value[1] instead of Value[1:]; Value[1] gives one character, not the shortened remainder of the string.

Things to Be Careful About

  • Keep the function name exactly as IterativeVowels().
  • The parameter must be passed into the function; do not hard-code test data inside it.
  • The loop must run the original number of times, even though the string is being shortened.
  • If you use a different valid Python method, it still has to give the same result as the pseudocode.
  • Make sure the final code is complete and runnable, not just a fragment of logic.
Techniques used
define a function with a parameter and return valueiterate through a string with a count-controlled loopextract the first character from a stringtest whether a character is one of several vowelsreturn the accumulated count
(ii)

Write program code to call the function IterativeVowels() with the parameter "house" from the main program.

Output the return value.

Save your program.

Copy and paste the program code into part 1(a)(ii) in the evidence document.

2M
DifficultyEasy
Worked solution

Answer

print(IterativeVowels("house"))
Final answer

See program code

Detailed explanation

Background Concept

After writing a function, the next step is to call it from the main program. Calling a function means giving it the required argument and then using the value it returns. In Python, if a function returns a value, that value can be printed directly with print(...).

Understanding the Question

This part does not ask you to rewrite the function. It asks you to use the function already created in part (a)(i). The required parameter is the string "house", and you must output the return value.

So there are really only two actions:

  • call IterativeVowels() with the given string
  • display the integer it returns

Approach

The shortest correct answer is to place the function call inside print(...). That way:

  1. the function executes
  2. it returns the number of vowels
  3. the value is displayed on screen

You do not need an extra variable unless you want one.

Step-by-Step Reasoning

IterativeVowels("house")

  • This calls the function and passes the string house as the argument.
  • The quotes are required because house is a string literal.

print(IterativeVowels("house"))

  • Python first evaluates the function call.
  • The function counts the lower-case vowels in house.
  • Then print(...) outputs the returned number.

The vowels in house are o, u and e, so the returned value is 3.

Key Takeaways

  • A function call must use the correct function name and the correct argument.
  • Returned values can be printed directly.
  • String literals must be enclosed in quotes.

Common Mistakes

  • Writing print(IterativeVowels) without brackets, which prints the function object instead of calling it.
  • Forgetting the quotes around house.
  • Calling the wrong function name.
  • Writing the function call correctly but not outputting the returned value.

Things to Be Careful About

  • Make sure this code is in the main program, not indented inside another function by mistake.
  • Use the exact case of the function identifier: IterativeVowels.
  • The question asks for the parameter "house", so do not substitute a different test string in the exam answer.
Techniques used
call a function with a string literal argumentuse the function return valueoutput the result to the console
(iii)

Test your program.

Take a screenshot of the output.

Save your program.

Copy and paste the screenshot into part 1(a)(iii) in the evidence document.

1M
DifficultyEasy
Worked solution

Answer

Using the call IterativeVowels("house"), the output is:

3
Final answer

3

Detailed explanation

Background Concept

Testing a program means running it with known input and checking whether the output matches the expected result. For a simple function like this, you can work out the correct answer manually and compare it with what the program prints.

Understanding the Question

This part asks for the output produced when the program is tested using the call from part (a)(ii):

  • function called: IterativeVowels()
  • parameter: "house"

The screenshot in the real exam would show whatever appears in the console. Here, we provide the expected console output.

Approach

Count the lower-case vowels in the word house:

  • h is not a vowel
  • o is a vowel
  • u is a vowel
  • s is not a vowel
  • e is a vowel

That gives a total of 3, so the output should be 3.

Step-by-Step Reasoning

Start with the string house.

  • First character h → not counted
  • Remaining string ouse
  • First character o → counted, total becomes 1
  • Remaining string use
  • First character u → counted, total becomes 2
  • Remaining string se
  • First character s → not counted
  • Remaining string e
  • First character e → counted, total becomes 3

The function returns 3, and the main program prints that value.

So the console shows:

3

Key Takeaways

  • Good testing uses input where the correct answer can be checked by hand.
  • You should be able to predict console output from the logic of the program.
  • Testing confirms both the function and the call in the main program are correct.

Common Mistakes

  • Counting the letters instead of counting only vowels.
  • Including consonants by mistake.
  • Forgetting that only lower-case vowels are being tested.
  • Showing the input word in the output even though the given code only prints the returned number.

Things to Be Careful About

  • The expected output depends on exactly what the print statement shows. Here it prints only the integer.
  • If extra text is added in your own program, the screenshot would differ, but the returned value is still 3.
  • Make sure you test with "house", not the string from another part of the question.
Techniques used
run the completed programtrace the string to count matching charactersverify the console output against the expected value
(b)
(i)

Rewrite the function IterativeVowels() as a recursive function with the identifier RecursiveVowels().

Save your program.

Copy and paste the program code into part 1(b)(i) in the evidence document.

6M
DifficultyMedium-Hard
Worked solution

Answer

def RecursiveVowels(Value):
    if len(Value) == 0:
        return 0
    FirstCharacter = Value[0:1]
    if FirstCharacter == "a" or FirstCharacter == "e" or FirstCharacter == "i" or FirstCharacter == "o" or FirstCharacter == "u":
        return 1 + RecursiveVowels(Value[1:])
    else:
        return RecursiveVowels(Value[1:])
Final answer

See program code

Detailed explanation

Background Concept

A recursive function is a function that calls itself. Recursion works by reducing a problem into a smaller version of the same problem until a stopping condition is reached. Every correct recursive solution needs two parts:

  • a base case that stops the recursion
  • a recursive case that moves toward the base case

This question is a classic example of recursion on a string. To count vowels in a whole string, you can:

  • look at the first character
  • count it if needed
  • solve the same problem for the rest of the string

That is why recursion is appropriate here: each step solves a smaller string of the same form.

Understanding the Question

You must rewrite the iterative function as a recursive function called RecursiveVowels(). The behaviour must stay the same:

  • input: a string
  • output: the number of lower-case vowels in that string

The original iterative version repeatedly removes the first character and updates a total. In the recursive version, instead of using a loop and accumulator, the total is built from return values.

Approach

Use the natural recursive pattern for strings:

  1. If the string is empty, return 0. That is the base case.
  2. Otherwise, take the first character.
  3. If it is a vowel, return 1 + the result for the rest of the string.
  4. If it is not a vowel, return just the result for the rest of the string.

This exactly mirrors the meaning of the iterative version, but without a loop.

Step-by-Step Reasoning

def RecursiveVowels(Value):

  • Defines the new function with the required identifier.

if len(Value) == 0:

  • This is the base case.
  • An empty string has no characters left to inspect, so it contains zero vowels.
  • The recursion must stop here.

return 0

  • Sends back the correct count for the empty string.

FirstCharacter = Value[0:1]

  • If the string is not empty, the first character is extracted.
  • Using a slice keeps the value as a one-character string.

The next if checks whether that first character is one of the five lower-case vowels.

If it is a vowel:

  • return 1 + RecursiveVowels(Value[1:])
  • The 1 counts the current character.
  • Value[1:] is the rest of the string with the first character removed.
  • The function calls itself to count vowels in that smaller string.

If it is not a vowel:

  • return RecursiveVowels(Value[1:])
  • Nothing is added for the current character.
  • The function still processes the rest of the string.

For example, with imagine:

  • i is a vowel → 1 + RecursiveVowels("magine")
  • m is not → RecursiveVowels("agine")
  • a is a vowel → 1 + RecursiveVowels("gine")
  • g is not → RecursiveVowels("ine")
  • i is a vowel → 1 + RecursiveVowels("ne")
  • n is not → RecursiveVowels("e")
  • e is a vowel → 1 + RecursiveVowels("")
  • empty string returns 0

Then the returns combine to give 4.

Key Takeaways

  • Every recursive solution needs a clear base case.
  • Each recursive call must make the problem smaller.
  • Recursion can replace loops when the problem naturally breaks into a smaller version of itself.
  • For string recursion, a common pattern is “first character + rest of string”.

Common Mistakes

  • Forgetting the base case, which causes infinite recursion.
  • Using Value[1] instead of Value[1:]; that passes only one character, not the remainder of the string.
  • Not returning the recursive call, so the result is lost.
  • Trying to use a loop and recursion together unnecessarily.
  • Accessing the first character before checking for the empty string, which can cause an index error.

Things to Be Careful About

  • The identifier must be exactly RecursiveVowels().
  • The question still wants lower-case vowels only.
  • The base case should be checked before taking the first character.
  • Each call must reduce the string length, otherwise the recursion will never end.
  • In Python, return 1 + RecursiveVowels(...) is the key line that builds the final count back up the call stack.
Techniques used
write a recursive base caseinspect the first character of the current stringreduce the problem by passing a shorter substringreturn 1 plus the recursive result for matching charactersreturn the recursive result unchanged for non-matching characters
(ii)

Write program code to call the function RecursiveVowels() with the parameter "imagine" from the main program.

Output the return value.

Save your program.

Copy and paste the program code into part 1(b)(ii) in the evidence document.

1M
DifficultyEasy
Worked solution

Answer

print(RecursiveVowels("imagine"))
Final answer

See program code

Detailed explanation

Background Concept

Calling a recursive function from the main program is the same as calling any other function. The recursion happens inside the function itself. From the outside, you simply pass an argument and receive a returned value.

Understanding the Question

This part asks you to use the recursive function written in part (b)(i). The required argument is "imagine", and the returned value must be output.

So the code needs to:

  • call RecursiveVowels()
  • pass the string "imagine"
  • print the returned integer

Approach

As in part (a)(ii), the simplest valid code is to place the function call inside print(...).

Step-by-Step Reasoning

RecursiveVowels("imagine")

  • This starts the recursive process on the string imagine.
  • The function keeps stripping the first character and counting vowels until the string becomes empty.

print(RecursiveVowels("imagine"))

  • Python evaluates the recursive function call first.
  • When the final integer has been returned, print(...) displays it.

The vowels in imagine are i, a, i and e, so the returned value is 4.

Key Takeaways

  • Recursive functions are called in exactly the same way as iterative ones.
  • The caller does not need to manage the recursion; it only uses the returned result.
  • Printing the function call directly is a simple and correct pattern.

Common Mistakes

  • Forgetting the quotes around imagine.
  • Using the old function name instead of RecursiveVowels.
  • Writing the call but not displaying the result.
  • Leaving the previous test call only and not adding the new one.

Things to Be Careful About

  • Use the exact spelling and case of RecursiveVowels.
  • Make sure the recursive function is defined before it is called.
  • If your program contains multiple print statements, the console output may show more than one line, but this part specifically requires the result of RecursiveVowels("imagine").
Techniques used
call a recursive function with a string literal argumentuse the recursive return valueoutput the result to the console
(iii)

Test your program.

Take a screenshot of the output.

Save your program.

Copy and paste the screenshot into part 1(b)(iii) in the evidence document.

1M
DifficultyEasy
Worked solution

Answer

Using the call RecursiveVowels("imagine"), the output is:

4
Final answer

4

Detailed explanation

Background Concept

Testing recursion still comes down to the same idea as testing iteration: choose input, predict the correct output, run the program and compare the result. For recursive routines, it is especially helpful to trace how the problem gets smaller at each call.

Understanding the Question

This part asks for the output when the recursive version is called with "imagine". In the real exam, you would show this by running the program and taking a screenshot. Here, the required answer is the expected output.

Approach

Count the lower-case vowels in imagine:

  • i yes
  • m no
  • a yes
  • g no
  • i yes
  • n no
  • e yes

That gives 4 vowels.

Step-by-Step Reasoning

A recursive trace looks like this conceptually:

  • RecursiveVowels("imagine")
  • first character i is a vowel, so result is 1 + RecursiveVowels("magine")
  • m is not a vowel, so continue with RecursiveVowels("agine")
  • a is a vowel, so add 1
  • g is not a vowel
  • i is a vowel, so add 1
  • n is not a vowel
  • e is a vowel, so add 1
  • empty string returns 0

Adding the counted vowels gives 4.

So the console output is:

4

Key Takeaways

  • You can manually verify recursive functions by following the chain of smaller calls.
  • The returned values combine as the calls finish.
  • Expected output for a test case should always be justifiable from the algorithm.

Common Mistakes

  • Missing one of the vowels when counting by hand.
  • Treating consonants as vowels.
  • Forgetting that the recursive version should produce the same kind of result as the iterative version.
  • Showing output from a different test string.

Things to Be Careful About

  • The answer here assumes the required call RecursiveVowels("imagine") is what is being tested.
  • If your full program still prints earlier test results as well, your actual console may have extra lines, but the returned value for this call is still 4.
  • Make sure the string is all lower-case, because the function only checks lower-case vowels.
Techniques used
run the recursive programtrace the recursive character checksverify the console output against the expected value

The rest of this paper

2 more questions
  • Q2Programming Paradigms (Procedural and Object-oriented) · Algorithms and Abstract Data Types · File Processing and Exception Handling29M
  • Q3Programming Paradigms (Procedural and Object-oriented) · File Processing and Exception Handling30M
Loading the full paper…