Skip to content
DeepForgeby svx
All problems
MediumInformation Theoryinfo-146

Caesar Best Shift

Problem

Break a Caesar cipher with a frequency table. For each shift 0..25, decrypt every letter as (c - shift) mod 26 and score the result by summing the English frequencies of the plaintext letters. Return the shift with the highest score, breaking ties toward the smaller shift. freqs is a 26-entry list of letter frequencies.

Hint

Longer ciphertexts make the frequency score much more reliable.

Test cases

case 1
in: ["WKHTXLFNEURZQIRAMXPSVRYHUWKHODCBGRJ",[8.167,1.492,2.782,4.253,12.702,2.228,2.015,6.094,6.966,0.153,0.772,4.025,2.406,6.749,7.507,1.929,0.095,5.987,6.327,9.056…
expected: 3
case 2
in: ["YMNXNXFXJHWJYRJXXFLJKTWYMJHTIJW",[8.167,1.492,2.782,4.253,12.702,2.228,2.015,6.094,6.966,0.153,0.772,4.025,2.406,6.749,7.507,1.929,0.095,5.987,6.327,9.056,2.7…
expected: 5
case 3
in: ["WLSJNIALUJBSCMZUMWCHUNCHAUHXZOH",[8.167,1.492,2.782,4.253,12.702,2.228,2.015,6.094,6.966,0.153,0.772,4.025,2.406,6.749,7.507,1.929,0.095,5.987,6.327,9.056,2.7…
expected: 20
case 4
in: ["GURBARGVZRCNQCEBIVQRFCRESRPGFRPERPL",[8.167,1.492,2.782,4.253,12.702,2.228,2.015,6.094,6.966,0.153,0.772,4.025,2.406,6.749,7.507,1.929,0.095,5.987,6.327,9.056…
expected: 13
Starter codePython
def caesar_best_shift(ciphertext, freqs):
    # Your code here
    pass

4 test cases · run your code against them in the editor