[PYTHON] 100 amateur language processing knocks: 08

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).

Chapter 1: Preparatory movement

08. Ciphertext

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

100 amateur language processing knocks: 41
100 amateur language processing knocks: 71
100 amateur language processing knocks: 56
100 amateur language processing knocks: 24
100 amateur language processing knocks: 50
100 amateur language processing knocks: 59
100 amateur language processing knocks: 62
100 amateur language processing knocks: 60
100 amateur language processing knocks: 92
100 amateur language processing knocks: 30
100 amateur language processing knocks: 06
100 amateur language processing knocks: 84
100 amateur language processing knocks: 81
100 amateur language processing knocks: 33
100 amateur language processing knocks: 46
100 amateur language processing knocks: 88
100 amateur language processing knocks: 89
100 amateur language processing knocks: 40
100 amateur language processing knocks: 45
100 amateur language processing knocks: 22
100 amateur language processing knocks: 61
100 amateur language processing knocks: 94
100 amateur language processing knocks: 54
100 amateur language processing knocks: 04
100 amateur language processing knocks: 78
100 amateur language processing knocks: 12
100 amateur language processing knocks: 14
100 amateur language processing knocks: 08
100 amateur language processing knocks: 42
100 amateur language processing knocks: 19
100 amateur language processing knocks: 73
100 amateur language processing knocks: 75
100 amateur language processing knocks: 98
100 amateur language processing knocks: 83
100 amateur language processing knocks: 95
100 amateur language processing knocks: 32
100 amateur language processing knocks: 96
100 amateur language processing knocks: 87
100 amateur language processing knocks: 72
100 amateur language processing knocks: 79
100 amateur language processing knocks: 23
100 amateur language processing knocks: 05
100 amateur language processing knocks: 00
100 amateur language processing knocks: 02
100 amateur language processing knocks: 37
100 amateur language processing knocks: 21
100 amateur language processing knocks: 68
100 amateur language processing knocks: 11
100 amateur language processing knocks: 90
100 amateur language processing knocks: 74
100 amateur language processing knocks: 66
100 amateur language processing knocks: 28
100 amateur language processing knocks: 64
100 amateur language processing knocks: 34
100 amateur language processing knocks: 36
100 amateur language processing knocks: 77
100 amateur language processing knocks: 01
100 amateur language processing knocks: 16
100 amateur language processing knocks: 27
100 amateur language processing knocks: 10
100 amateur language processing knocks: 03