Course 4.2Lesson 2 of 5
A Caesar code can be read without knowing the shift by counting letters. In English the letter e is the most common, so in a long secret the most common letter probably stands for e, and the shift is the place of that letter minus the place of e, with percent 26 so that it stays from 0 to 25. A dictionary counts the letters and max with key equals counts.get picks the commonest. A short secret can fool the rule, because with few letters the commonest one may not be e. Then try every one of the 26 shifts, and keep the shift that gives the most common English words.
Try the code and the simulations beside the lesson.
After this lesson you can
max, and % 26 in one tool.In the course on text you hid a message with a Caesar code. Can you read a secret without knowing the shift? Yes, by counting letters. In English the letter e is the most common. So the most common letter in the secret probably stands for e.
If the commonest letter of the secret is l, and l is 7 places after e, then the shift is 7.
Before you run any code, think. We have a long secret of about 130 letters. If the rule is right, then:
l, and it appears more than any other letter,(place of l - place of e) % 26, which is ,Predict first, then run it.
count_letters is the counting pattern: a dictionary, with the letter as the key. It ignores spaces and marks with isalpha().shift_text is the function of the text course. A negative shift goes back.max(counts, key=counts.get) picks the letter with the biggest count.(ord(commonest) - ord("e")) % 26 is the shift. The % 26 keeps the answer from 0 to 25. Without it, a letter before e in the alphabet (such as a) would give a negative number, and % 26 turns it into the right shift.The letter l appears 28 times in this secret, and the program finds the shift 7. The message is readable.
The rule needs a long text. Look at a short one.
This is an important lesson in the use of statistics: a rule that works on many items can fail on few. Always ask how much data you have.
In a short message the bars wobble, and another letter may be tallest. Then trying every shift, and checking for real words, is safer.
When the text is short, we can try every shift (there are only 26) and look at the answers. But a computer cannot "look". It needs a score: how English does the answer look? A simple score: count how many of the words are common English words.
The method has a weakness: it needs a list of common words, and a message with no common words will fool it. A bigger set of words helps. But the method does not need the text to be long.
| Method | Works when | Weakness |
|---|---|---|
| the commonest letter | the text is long | short texts |
| try all 26, and score | any length, with common words | needs a word list |
A good tool can use both: first the quick rule, and if the answer scores badly, try all the shifts.
Pick one or two, and do them without looking at a solution. Write the expected answers first.
% 26. A shift outside 0 to 25.max picks the first. Is that right?-shift.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
e is the most common letter.e, with % 26.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
x. What shift does the rule guess?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.