Loading....

Computer Science (9-1) 0984 | Paper 2 Mock 01 | Algorithms, Programming and Logic

Question 1 Report

A UK postcode follows one of these formats (where A = letter, 9 = digit):

  • A9 9AA (e.g., M1 1AA)
  • A99 9AA (e.g., M60 1NW)
  • AA9 9AA (e.g., CR2 6XH)
  • AA99 9AA (e.g., CR26 8NW)

All postcodes have a space separating the outward and inward parts, and the inward part is always 9AA (digit then two letters).

(a) Write pseudocode for a function IsLetter that returns TRUE if a character is an uppercase letter. [2]

(b) Write pseudocode for a function IsDigit that returns TRUE if a character is a digit. [2]

(c) Write pseudocode for a function ValidateInward that checks the inward part (last 3 characters) follows the pattern: digit, letter, letter. [3]

Answer Details

(a) The IsLetter function checks whether a character falls within the range of uppercase letters 'A' to 'Z' by comparing its value against both boundaries. [2]

FUNCTION IsLetter(Ch : CHAR) RETURNS BOOLEAN
    IF Ch >= 'A' AND Ch <= 'Z' THEN
        RETURN TRUE
    ELSE
        RETURN FALSE
    ENDIF
ENDFUNCTION

Character comparisons work because characters are stored as numeric codes (e.g. ASCII), where 'A' through 'Z' are consecutive values 65 through 90. If Ch is greater than or equal to 'A' and less than or equal to 'Z', it must be an uppercase letter. Any character outside this range (digits, lowercase letters, punctuation, spaces) returns FALSE.

(b) The IsDigit function works on the same principle, checking whether a character falls within the range '0' to '9'. [2]

FUNCTION IsDigit(Ch : CHAR) RETURNS BOOLEAN
    IF Ch >= '0' AND Ch <= '9' THEN
        RETURN TRUE
    ELSE
        RETURN FALSE
    ENDIF
ENDFUNCTION

The digit characters '0' through '9' occupy consecutive codes (ASCII 48-57). A character that is both >= '0' and <= '9' is guaranteed to be a digit.

(c) The ValidateInward function checks that the inward part of a UK postcode follows the required pattern: one digit followed by two letters (9AA). [3]

FUNCTION ValidateInward(Inward : STRING) RETURNS BOOLEAN
    IF LENGTH(Inward) <> 3 THEN
        RETURN FALSE
    ENDIF
    IF IsDigit(Inward[1]) AND IsLetter(Inward[2]) AND IsLetter(Inward[3]) THEN
        RETURN TRUE
    ELSE
        RETURN FALSE
    ENDIF
ENDFUNCTION

The function performs two checks:

  1. Length check: The inward part must be exactly 3 characters. If the length is not 3, the function immediately returns FALSE without further checks.
  2. Pattern check: Position 1 must be a digit (checked using IsDigit), and positions 2 and 3 must both be uppercase letters (checked using IsLetter). All three conditions must be TRUE (connected with AND) for the inward part to be valid. For example, "6XH" would pass (6 is a digit, X and H are letters), but "XXH" would fail (X is not a digit) and "6X3" would fail (3 is not a letter).
1
2
3
4
5
6
7
8
9
10
11