Caesar cipher

Discussion thread for Caesar cipher.

How do you solve this problem if the “known_word” is made of just one letter? Currently my approach to solving this is:
(1) Go to each separate word in encrypted sentence
(2) Take the first letter and look calculate the shift
(3) Check if the shift is the same in other letters of the word too. If yes, this is the shift you were looking for
(4) Decrypt the sentence using the shift.
It obviously fails if the “known_word” is made of just one letter!


I agree that this is a little misleading. Even when the “known_word” is a single character, you should treat it as a “word”. This means that in step (3), you would need to figure out the shift based on that one-letter word alone.

To be honest, I think we need to improve the problem to avoid making the “known_word” one-letter long. As a workaround for you: if you have a solution you think is good but isn’t passing the “random test case”, try submitting a few times.

Thanks for the response. I did submit it a few times and it passed all the test cases except those where the known_word was a single letter. I will take your suggestions and re-look at my code. Thanks again for taking the time to write.

The submission guidelines are a little confusing. The example text shows lowercase output, but all of the test cases expect uppercase output.

Either is fine, but it has to be consistent.

1 Like

@POGtastic thanks for reporting this, definitely a mistake as I think plaintexts are usually in lowercase and yes, good to be consistent.

Just fixed it!

Similar to the above issue, the example gives the “Input word” in lower case, but all the test cases have an upper case known word.

If this were a program “in-the-wild”, i.e. you might get any kind of input, then you should do some validity checking of those inputs, e.g check that the known word is all lower case, contains no punctuation, numbers or other special characters.

As for the single letter issue, assuming you are working in English, there aren’t that many single letter words, in fact only 2, ‘I’ and ‘a’; somebody correct me if I’m wrong. Obviously, a computer can’t understand English (unless you’ve got some kind of clever machine learning algorithm) so you’d have to return both valid translations; one will be correct English, the other garbage. As for random input you’d have to know what the output is in the first place, which defeats the purpose of the function. I guess you could treat a single letter known word as invalid input and return something appropriate in response.

Hey @Will thanks for pointing that out! Kind of embarrasing on my end, I fixed the example but forgot to fix the actual problem lol. Should all be consistent now which should make things less frustrating for anyone submitting a solution. Old solutions may need to be fixed now that known_word is lowercase as intended.

@djwalker Good point about one-letter words. I guess right now the known_word is generated randomly so in practice people probably resubmit if they get a one-letter known_word and return the wrong decrypted message. We probably should modify the problem so that known_word will be at least two or three letters long which will reduce the chances of a “wrong” decryption.

You’re right that any real-world program would have to sanitize the input and check stuff. I guess we try to always pass valid input so that people can just focus on solving the problem.