Edexcel GCSE Computer Science June 2024 Paper 2, Question 2: the Caesar cipher
Pearson Edexcel 1CP2/02 June 2024, Question 2: choose the correct lines to make a Caesar cipher work, shifting capital and lower case letters with wrap-around in both directions and leaving other characters alone. Each choice explained, with the cipher to run.
Question 2 of the Pearson Edexcel GCSE Computer Science Paper 2 sat on 21 May 2024 (1CP2/02, Application of Computational Thinking) is worth 10 marks. In ten places the file offers several commented-out lines, and you uncomment the right one. To choose, you have to understand how the whole cipher works.
We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2024 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.
The question in short
A Caesar cipher shifts each letter a set number of places along the alphabet. A positive shift goes right and a negative shift goes left. Going past Z carries on from A, and going back past A carries on from Z. Spaces, symbols and digits are not changed.
The paper's test data: "The Rainbow" with a shift of 4 gives "Xli Vemrfsa". "Alphabet Soup" with -5 gives "Vgkcvwzo Njpk". "123 ^& Bye" with 9 gives "123 ^& Khn".
How it works
For each character:
- Is it a letter?
letter.isalpha(). If not, add it to the ciphertext unchanged. - Turn it into its ASCII code with
ord()and add the shift. - Is it a capital?
letter.isupper(). If the new code is pastord('Z'), take 26 off. If it is beforeord('A'), add 26. - Otherwise is it lower case?
letter.islower(). The same two checks, againstord('z')andord('a'). - Turn the code back into a character with
chr()and add it to the ciphertext.
The ten choices
| The line must | The right choice |
|---|---|
| test for a letter | if (letter.isalpha ()): |
| test for a capital | if (letter.isupper ()): |
| catch a capital shifted past the end | if (value > ord ('Z')): |
| catch a capital shifted before the start | elif (value < ord ('A')): |
| test for lower case | elif (letter.islower ()): |
| catch lower case past the end | if (value > ord ('z')): |
| catch lower case before the start | elif (value < ord ('a')): |
| make the new letter | newLetter = chr (value) |
| add an encrypted letter | cipherText = cipherText + newLetter |
| add an unchanged character | cipherText = cipherText + letter |
Where the marks are lost
- Uncommenting two lines in one group. The mark for that group is lost, even if one of them is right.
>=againstord('Z'). Z itself is a valid result. Only codes past Z wrap round.- Mixing up
ordandchr.ordgoes from a character to a number.chrcomes back. - Adding
letterwherenewLetteris wanted. The output would be the plaintext. - Not running the test data. All three rows, including the negative shift and the symbols.
Run it
The whole cipher, written out. Type a message and a shift.
The program
from bugbot import *
connect()
plainText = input("Message: ")
shift = int(input("Shift: "))
cipherText = ""
for letter in plainText:
if letter.isalpha():
value = ord(letter) + shift
if letter.isupper():
if value > ord("Z"):
value = value - 26
elif value < ord("A"):
value = value + 26
elif letter.islower():
if value > ord("z"):
value = value - 26
elif value < ord("a"):
value = value + 26
newLetter = chr(value)
cipherText = cipherText + newLetter
else:
cipherText = cipherText + letter
print(cipherText)
Questions
How does a Caesar cipher work?
Each letter of the message is replaced by the letter a fixed number of places further along the alphabet, wrapping round from Z to A. To decrypt, shift the same number of places the other way.
How do I shift a letter in Python?
Turn it into its code with ord(), add the shift, correct it by 26 if it has gone past the end or the start of the alphabet, and turn it back with chr().
What do isalpha(), isupper() and islower() do?
They are string methods that return True or False. isalpha() is True for letters, isupper() for capital letters and islower() for lower case letters.
More from this paper
- Question 3: Price a purchase by count or by weight, with constants and validation 10 marks
- Question 5: A menu loop with getChoice, getShape and addShape subprograms 15 marks
Every Edexcel 1CP2 question we have worked
Learn it step by step
- F3.2 Character codes and conversion Strings, lists and records
- F11.6 Encryption Cyber security
- F3.1 String handling Strings, lists and records
This is our own explanation of a published exam question. It is not written or endorsed by Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.