It is a challenge record of Language processing 100 knock 2015. The environment is Ubuntu 16.04 LTS + Python 3.5.2 : : Anaconda 4.1.1 (64-bit). Click here for a list of past knocks (http://qiita.com/segavvy/items/fb50ba8097d59475f760).
Implement the function cipher that converts each character of the given character string according to the following specifications.
・ Replace with (219 --character code) characters in lowercase letters ・ Other characters are output as they are
Use this function to encrypt / decrypt English messages.
The finished code:
main.py
# coding: utf-8
def cipher(target):
'''String encryption / decryption
Convert a character string with the following specifications
・ If lowercase letters(219 -Character code)Replace with the character
・ Other characters are output as they are
argument:
target --Target character string
Return value:
Converted string
'''
result = ''
for c in target:
if c.islower():
result += chr(219 - ord(c))
else:
result += c
return result
#Input of target character string
target = input('Please enter a string--> ')
#encryption
result = cipher(target)
print('encryption:' + result)
#Decryption
result2 = cipher(result)
print('Decryption:' + result2)
#Check if it is restored by decryption
if result2 != target:
print('Not back! ??')
Execution result:
Terminal
Please enter a string--> I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .
encryption:I xlfowm'g yvorvev gszg I xlfow zxgfzoob fmwvihgzmw dszg I dzh ivzwrmt : gsv ksvmlnvmzo kldvi lu gsv sfnzm nrmw .
Decryption:I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .
Since there is no indication of the target character string in the problem, use ʻinput () ` to receive from the standard input. Saw. That's all for the 9th knock. If you have any mistakes, I would appreciate it if you could point them out.
Recommended Posts