Skip to content

Commit cbc2a15

Browse files
committed
Updating code
1 parent 86e1a2e commit cbc2a15

3 files changed

Lines changed: 41 additions & 29 deletions

File tree

code/metathesis.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

10-
from anagram_sets import *
12+
from __future__ import print_function, division
13+
14+
import anagram_sets
15+
1116

1217
def metathesis_pairs(d):
1318
"""Print all pairs of words that differ by swapping two letters.
1419
1520
d: map from word to list of anagrams
1621
"""
17-
for anagrams in d.itervalues():
22+
for anagrams in d.values():
1823
for word1 in anagrams:
1924
for word2 in anagrams:
2025
if word1 < word2 and word_distance(word1, word2) == 2:
21-
print word1, word2
26+
print(word1, word2)
2227

2328

2429
def word_distance(word1, word2):
@@ -39,5 +44,5 @@ def word_distance(word1, word2):
3944

4045

4146
if __name__ == '__main__':
42-
d = all_anagrams('words.txt')
43-
metathesis_pairs(d)
47+
sets = anagram_sets.all_anagrams('words.txt')
48+
metathesis_pairs(sets)

code/most_frequent.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

10-
import random
12+
from __future__ import print_function, division
1113

1214

1315
def most_frequent(s):
@@ -51,7 +53,7 @@ def read_file(filename):
5153

5254

5355
if __name__ == '__main__':
54-
s = read_file('words.txt')
55-
t = most_frequent(s)
56-
for x in t:
57-
print x
56+
string = read_file('emma.txt')
57+
letter_seq = most_frequent(string)
58+
for x in letter_seq:
59+
print(x)

code/palindrome_soln.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
"""This module contains code from
2-
Think Python by Allen B. Downey
3-
http://thinkpython.com
1+
"""This module contains a code example related to
42
5-
Copyright 2012 Allen B. Downey
6-
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
76
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
810
"""
911

12+
from __future__ import print_function, division
13+
14+
1015
def first(word):
1116
"""Returns the first character of a string."""
1217
return word[0]
@@ -31,8 +36,8 @@ def is_palindrome(word):
3136
return is_palindrome(middle(word))
3237

3338

34-
print is_palindrome('allen')
35-
print is_palindrome('bob')
36-
print is_palindrome('otto')
37-
print is_palindrome('redivider')
39+
print(is_palindrome('allen'))
40+
print(is_palindrome('bob'))
41+
print(is_palindrome('otto'))
42+
print(is_palindrome('redivider'))
3843

0 commit comments

Comments
 (0)