Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsMy Caesar cipher challenge
create a encoder/decoder function which takes a string and a dictionary in this case the dictionary key and the string to decode. The idea of the caesar cypher is to replace plain text with another letter a fixed position down the alphabet chart .
key = {'a':'n', 'b':'o', 'c':'p', 'd':'q', 'e':'r', 'f':'s','g':'t','h':'u','i':'v','j':'w','k':'x', 'l':'y',
'm':'z', 'n':'a', 'o':'b', 'p':'c','q':'d', 'r':'e', 's':'f', 't':'g', 'u':'h', 'v':'i', 'w':'j', 'x':'k','y':'l',
'z':'m', 'A':'N', 'B':'O', 'C':'P', 'D':'Q', 'E':'R', 'F':'S','G':'T', 'H':'U', 'I':'V', 'J':'W', 'K':'X', 'L':'Y',
'M':'Z', 'N':'A','O':'B', 'P':'C', 'Q':'D', 'R':'E', 'S':'F', 'T':'G', 'U':'H', 'V':'I',
'W':'J', 'X':'K', 'Y':'L', 'Z':'M'}
# the string to decode
"Pnrfne pvcure? V zhpu cersre Pnrfne fnynq!"
# hint the key in the dictionary is the cypher and the values are the plain text.
Your function should conver P to C and so on.
# i have solved it but nice to see what solutions others come up with.
# if you get it right your function should return the string "Caesar cipher? I much prefer Caesar salad!"
# happy coding
3 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsHere is my first pass:
key = {'a': 'n', 'b': 'o', 'c': 'p', 'd': 'q', 'e': 'r', 'f': 's',
'g': 't', 'h': 'u', 'i': 'v', 'j': 'w', 'k': 'x', 'l': 'y',
'm': 'z', 'n': 'a', 'o': 'b', 'p': 'c', 'q': 'd', 'r': 'e',
's': 'f', 't': 'g', 'u': 'h', 'v': 'i', 'w': 'j', 'x': 'k',
'y': 'l', 'z': 'm', 'A': 'N', 'B': 'O', 'C': 'P', 'D': 'Q',
'E': 'R', 'F': 'S', 'G': 'T', 'H': 'U', 'I': 'V', 'J': 'W',
'K': 'X', 'L': 'Y', 'M': 'Z', 'N': 'A', 'O': 'B', 'P': 'C',
'Q': 'D', 'R': 'E', 'S': 'F', 'T': 'G', 'U': 'H', 'V': 'I',
'W': 'J', 'X': 'K', 'Y': 'L', 'Z': 'M'}
# the string to decode
cipher = "Pnrfne pvcure? V zhpu cersre Pnrfne fnynq!"
# Method 1: loop and join
mapped = []
for char in cipher:
if char.isalpha():
mapped.append(key[char])
else:
mapped.append(char)
print(''.join(mapped))
# Method 2: map function and join
def mapper(char):
if char.isalpha():
return key[char]
else:
return char
print(''.join(map(mapper, cipher)))
Andreas cormack
Python Web Development Techdegree Graduate 33,011 PointsHi Chris
great job !!.
This was my way of doing it
# method 1
def decoder(my_string):
mapped = []
for letter in my_string:
if letter in key.keys():
mapped.append(key[letter])
else:
mapped.append(letter)
print("".join(mapped))
# method 2
def decoder(my_string):
mapped = []
for letter in my_string:
mapped.append(key.get(letter,letter))
print(''.join(mapped))
Chris Freeman
Treehouse Moderator 68,468 PointsThe jury is back! I used timeit.timeit to compare the results of 100,000 loops. I can't deny I like the results
timing CF #1
3.4009001255
timing CF #2
3.48306393623
timing AC #1
14.3936920166
timing AC #2
4.16476297379
I think your solution #1 was slowed by the if letter in key.keys() search.
Fun challenge. Thanks!!
Andreas cormack
Python Web Development Techdegree Graduate 33,011 Pointsin that case your first example is the best one in terms of performance. Glad you like the challenge.