Frequency analysis

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Frequency analysis

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does frequency analysis count in a ciphertext?
A How many words are in the message
B How often each letter appears
2. In a long English message, which letter is usually the most common?
A Z
B E
3. A ciphertext has the letter X far more often than any other letter. What is a good first guess?
A X stands for E
B X stands for Z
4. Why does frequency analysis work on a substitution cipher?
A Each plaintext letter always becomes the same ciphertext letter, so the letter pattern stays
B The cipher hides every letter pattern completely
5. Which method gives a codebreaker a way to check a guess about a short word?
A Guess that a one-letter word is A or I
B Guess that every short word is the same

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1frequency analysis AA tally of how many times each ciphertext letter appears, where Q is the most common and so is probably E
2counts = {"Q": 14, "X": 3, "M": 9} BA substitution cipher keeps letter patterns because each plaintext letter always becomes the same ciphertext letter, so the frequencies still show.
3E is the most common letter in English, so the most common ciphertext letter is probably E Cfor letter in ciphertext: counts[letter] = counts.get(letter, 0) + 1
4Counting letter frequencies means going through the ciphertext and adding 1 to a tally for each letter Dmost_common = max(counts, key=counts.get) guess = {most_common: "E"}
5Ciphertext letter Q is the least common, so Q must be E. EE is the most common letter in English, so the most common ciphertext letter is the best guess for E, not the least common.
6A substitution cipher hides letter patterns, so frequency analysis cannot crack it. FCounting how often each letter appears in ciphertext and matching the most common ones to common English letters like E to crack a substitution cipher

Definitions

frequency analysis

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
counts = {} ___ ch in cipher: counts[ch] = counts.get(ch, 0) + 1
2.
counts = {"X": 9, "Q": 4, "M": 2} top = ___(counts, key=counts.get) print(top)
3.
top = "X" shift = ord(top) - ord("_") print(shift)
4.
word = "X" if len(word) == _: print("Try A or I")
5.
guess = {"X": "E"} for ch in cipher: print(_____.get(ch, "_"), end="")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A cipher = "KHOOR ZRUOG
B letter = "O"
C n = cipher.count(letter)
D print(letter, n)
Question 2
A from collections import *
B counts = Counter("KHOOR")
C top = counts.least_common(1)
D print(top)
Question 3
A top = "H"
B shift = ord("E") - ord(top)
C print(shift)
Question 4
A d = {}
B for c in "KHOOR":
C d[c] = 1
D print(d)
Question 5
A d = {"K": 1, "H": 1, "O": 2}
B best = "K"
C for c in d:
D if d[c] < d[best]:
E best = c

Answer Key

Frequency analysis
Section A: Quick Questions
Q1
B — How often each letter appears
Q2
B — E
Q3
A — X stands for E
Q4
A — Each plaintext letter always becomes the same ciphertext letter, so the letter pattern stays
Q5
A — Guess that a one-letter word is A or I
Section B: Match the Pairs
Q1
frequency analysis → Counting how often each letter appears in ciphertext and matching the most common ones to common English letters like E to crack a substitution cipher
Q2
counts = {"Q": 14, "X": 3, "M": 9} → A tally of how many times each ciphertext letter appears, where Q is the most common and so is probably E
Q3
E is the most common letter in English, so the most common ciphertext letter is probably E → most_common = max(counts, key=counts.get) guess = {most_common: "E"}
Q4
Counting letter frequencies means going through the ciphertext and adding 1 to a tally for each letter → for letter in ciphertext: counts[letter] = counts.get(letter, 0) + 1
Q5
Ciphertext letter Q is the least common, so Q must be E. → E is the most common letter in English, so the most common ciphertext letter is the best guess for E, not the least common.
Q6
A substitution cipher hides letter patterns, so frequency analysis cannot crack it. → A substitution cipher keeps letter patterns because each plaintext letter always becomes the same ciphertext letter, so the frequencies still show.
Section C: Definitions
Q1
frequency analysis — Counting how often each letter appears in ciphertext and matching the most common ones to common English letters like E, to work out a substitution cipher's key.
Section D: Fill in the Blanks
Q1
counts = {} for ch in cipher: counts[ch] = counts.get(ch, 0) + 1
Q2
counts = {"X": 9, "Q": 4, "M": 2} top = max(counts, key=counts.get) print(top)
Q3
top = "X" shift = ord(top) - ord("E") print(shift)
Q4
word = "X" if len(word) == 1: print("Try A or I")
Q5
guess = {"X": "E"} for ch in cipher: print(guess.get(ch, "_"), end="")
Section E: Spot the Error
Q1
A — The string is missing its closing quote. It should be cipher = "KHOOR ZRUOG" so the ciphertext can be counted.
Q2
C — Frequency analysis needs the MOST common ciphertext letter to match with E. The method is most_common(1). There is no least_common method.
Q3
B — The shift is the ciphertext letter minus the plain letter E. The subtraction is reversed, giving -3 instead of 3. It should be ord(top) - ord("E").
Q4
C — This resets each letter's count to 1 every time, so repeated letters like O are never counted properly. It should add 1 to the old count: d[c] = d.get(c, 0) + 1.
Q5
D — Using < picks the least common letter. To find the most common ciphertext letter, the test must be d[c] > d[best].