diff --git a/code/invert_dict.py b/code/invert_dict.py index 01588e3..8a569f9 100644 --- a/code/invert_dict.py +++ b/code/invert_dict.py @@ -17,9 +17,9 @@ def invert_dict(d): Returns: dict """ - inverse = {} - for key, val in d.iteritems(): - inverse.setdefault(val, []).append(key) + inverse = {} # create an empty dictionary + for key, val in d.iteritems(): # loop the dictionary + inverse.setdefault(val, []).append(key) # if not exist, create a new list return inverse diff --git a/code/rotate_pairs.py b/code/rotate_pairs.py index 0a98464..fa170e6 100644 --- a/code/rotate_pairs.py +++ b/code/rotate_pairs.py @@ -9,15 +9,41 @@ from rotate import rotate_word +''' +def rotate_letter(letter, n): + """Rotates a letter by n places. Does not change other chars. + letter: single-letter string + n: int + Returns: single-letter string + """ + + start = ord('a') + + c = ord(letter) - start + i = (c + n) % 26 + start + return chr(i) + +def rotate_word(word, n): + """Rotates a word by n places. + word: string + n: integer + Returns: string + """ + + res = '' + for letter in word: + res += rotate_letter(letter, n) + return res +''' def make_word_dict(): """Read the words in words.txt and return a dictionary that contains the words as keys""" d = dict() fin = open('words.txt') - for line in fin: + for line in fin: word = line.strip().lower() - d[word] = word + d[word] = word # create a list[] dict d return d