I am writing a encrypting program whose process is as follows:
- Spaces in a message replaced by
'X'
's - Each word in the original message is reversed
- Consecutive sequences of words, called “blocks”, have the order of the words
reversed. The size of these blocks is a parameter to the encryption function and acts
as the “key”. For example, if the message was
'THE PRICE OF FREEDOM IS ETERNAL VIGILENCE'
and the block size is four then each block of four words will be reversed, producing'FREEDOM OF PRICE THE VIGILENCE ETERNAL IS'
(ignoring the other two steps above). Notice that the last block only has three words in this case, but it is still reversed nonetheless.
Now I've been able to replace the spaces and reverse everything, but the last step has stumped me.
The following code is what I have so far.
def encrypt (words, block):
words = words [::-1]
midpoint = len(words)/block
first_half = words[0:midpoint]
second_half = words[midpoint:]
words = first_half + second_half
words = words.replace(' ', 'X')
return words
def decrypt (wordsde, block):
wordsde = wordsde[::-1]
midpoint = len(wordsde) / block
first_half = wordsde[:midpoint]
second_half = wordsde[midpoint:]
wordsde = first_half + second_half
wordsde = wordsde.replace('X', ' ')
wordsde = wordsde.strip()
return wordsde
but every time, it can't read the block or something:
File "__main__", line 18, in __main__
Failed example:
encrypt('WHO WATCHES THE WATCHERS', 2) # Test 4
Expected:
'SEHCTAWXOHWXSREHCTAWXEHT'
Got:
'SREHCTAWXEHTXSEHCTAWXOHW'
Trying:
encrypt('PARANOIA IS OUR PROFESSION', 3) # Test 5
Expecting:
'RUOXSIXAIONARAPXNOISSEFORP'
**********************************************************************
File "__main__", line 22, in __main__
Failed example:
encrypt('PARANOIA IS OUR PROFESSION', 3) # Test 5
Expected:
'RUOXSIXAIONARAPXNOISSEFORP'
Got:
'NOISSEFORPXRUOXSIXAIONARAP'
Trying:
encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 4) # Test 6
Expecting:
'MODEERFXFOXECIRPXEHTXECNELIGIVXLANRETEXSI'
**********************************************************************
File "__main__", line 26, in __main__
Failed example:
encrypt('THE PRICE OF FREEDOM IS ETERNAL VIGILENCE', 4) # Test 6
Expected:
'MODEERFXFOXECIRPXEHTXECNELIGIVXLANRETEXSI'
Got:
'ECNELIGIVXLANRETEXSIXMODEERFXFOXECIRPXEHT'