Skip to content

Course home

Sign up

Develop code

EasyMediumHard
1234567891011121314151617
Question 3

A program encrypts a message using a Caesar-style alphabetic shift cipher.

The letters of the alphabet are shifted a set number of places. A positive shift moves the letters to the right (towards Z/z). A negative shift moves them to the left (towards A/a).

Spaces, punctuation, symbols, and numbers must not be encrypted.

When the end of the alphabet is reached with a shape shift, wrapping must occur so the alphabet acts as a loop. For example, a shift of +3 encodes 'z' to 'c', and a shift of -2 encodes 'a' to 'y'. Case is preserved throughout encryption.

The program must produce the encrypted ciphertext shown in the test table below:

Table 1: Caesar Cipher Test Cases

PlaintextShiftCiphertext
Hello World!3Khoor Zruog!
Zebra-99-2Xczpy-99
Code 12314Qcrs 123

Below is an incomplete Python implementation of this program. It contains five sections where you must choose between alternative lines of code.

def encrypt_message(plain_text, shift):
    cipher_text = ""
    for char in plain_text:
        # --- CHOICE 1 ---
        # Option A1: if char.isalpha():
        # Option A2: if char.isalnum():

            # --- CHOICE 2 ---
            # Option B1: start = ord('a') if char.islower() else ord('A')
            # Option B2: start = ord('z') if char.islower() else ord('Z')

            # --- CHOICE 3 ---
            # Option C1: position = ord(char) - start
            # Option C2: position = ord(char)

            # --- CHOICE 4 ---
            # Option D1: new_position = (position + shift) % 26
            # Option D2: new_position = (position + shift) % 52

            # --- CHOICE 5 ---
            # Option E1: cipher_text += chr(new_position + start)
            # Option E2: cipher_text += chr(new_position)
        else:
            cipher_text += char
    return cipher_text

Identify the correct option (1 or 2) for each of the five choices to make the program function correctly according to the specification.

[5]
Markscheme

Develop code Questions

  1. GCSE
  2. /Computer Science
  3. /Develop code

51 exam-style questions on Edexcel GCSE Computer Science Develop code, covering Decomposition and abstraction to solve problems, Read, write, analyse and refine programs, Converting algorithms into programs, Techniques for readable, maintainable code, Identifying and correcting program errors, and Evaluating program fitness and efficiency. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank