First, you’ve been given a terrible specification. It includes a couple of magic numbers without explaining where they came from. I implemented it anyway for fun and got a working program, and here’s what I figured out.
Given the key ‘gaurav’:
- Take the first letter ‘g’. Its “offset” is 6 greater than ‘a’. That is,
ord('g') - ord('a') == 6
. - Use that offset value of 6 for 7 (6 + 1) letters in a row. That is, for each character in the plaintext, if it’s a letter, add 6 to it (and subtract 26 if the result goes past ‘z’).
- Consume one of those 7 occurrences of the 6 offset for each character in the plaintext, even if it’s not used! If the character is a space of punctuation, don’t add 6 to it but still count it as one of the 7 uses!
- The next letter in the key is ‘a’. That gives an offset of 0. Use it 1 (0 + 1) time.
- The next letter is ‘u’ with an offset of 20. Use it 21 times.
- And so on.
So your algorithm will look like:
O + 6 => U : #1 of 7 6s
u + 6 => a : #2 of 7 6s
r + 6 => x : #3 of 7 6s
" " : #4 of 7 6s
g + 6 => m : #5 of 7 6s
r + 6 => x : #6 of 7 6s
e + 6 => k : #7 of 7 6s
a + 0 => a : #1 of 1 0s
t +20 => n : #1 of 21 20s
e +20 => y : #2 of 21 20s
...
and so on
What I’m not telling you how to do (it’s your contest 🙂 ):
- Preserve case. For instance,
chr(ord('U') + 20) == 'i'
, but you don’t want to translate an uppercase ‘U’ to a lowercase ‘i’. - Create a stream of those offsets and use one up for each character in your input stream.
Hint:
- You want to generate an iterator of offsets instead of creating a list of them all at once.
solved Cipher Text Program : Coding Competetion [closed]