Task:
Encrypt this!
You want to create secret messages which can be deciphered by the Decipher this! kata. Here are the conditions:
- Your message is a string containing space separated words.
- You need to encrypt each word in the message using the following rules:
- The first letter must be converted to its ASCII code.
- The second letter must be switched with the last letter
- Keepin' it simple: There are no special characters in the input.
Examples:
encrypt_this("Hello") == "72olle"
encrypt_this("good") == "103doo"
encrypt_this("hello world") == "104olle 119drlo"
Solution:
def encrypt_this(text):
words = text.split()
for k, w in enumerate(words):
if len(w) > 2 :
words[k] = str(ord(w[0])) + w[-1] + w[2:-1] + w[1]
else:
words[k] = str(ord(w[0])) + w[1:]
return " ".join(words)
print(encrypt_this('A wise old owl lived in an oak'))
Read more ...