├── .DS_Store ├── exercises ├── .DS_Store ├── ex03_03.py ├── ex03_04.py ├── ex03_05.py ├── ex04_02.py ├── ex05_03.py ├── ex05_04.py ├── ex06_05.py ├── ex06_06.py ├── ex06_07.py ├── ex06_08.py ├── ex07_03.py ├── ex07_04.py ├── ex07_05.py ├── ex08_10.py ├── ex08_12.py ├── ex09_07.py ├── ex09_08.py ├── ex09_09.py ├── ex10_06.py ├── ex10_07.py ├── ex10_08.py ├── ex10_09.py ├── ex10_10.py ├── ex10_11.py ├── ex10_12.py ├── ex10_13.py ├── ex11_09.py ├── ex11_10.py ├── ex11_11.py ├── ex12_03.py ├── ex12_04.py ├── ex12_05.py ├── ex12_06.py ├── ex13_09.py ├── ex14_06.py ├── ex15_04.py ├── ex16_06.py └── ex16_07.py ├── extra ├── .DS_Store ├── c06d.txt ├── pg1661.txt ├── polygon.py ├── polygon.pyc ├── swampy-2.1.1.zip ├── thinkpython_v2.0.9.pdf ├── words.txt └── words2.txt └── readme.txt /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gregorulm/think_python/9e15b538b157c6ec809bb404a9169eff386e67e9/.DS_Store -------------------------------------------------------------------------------- /exercises/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gregorulm/think_python/9e15b538b157c6ec809bb404a9169eff386e67e9/exercises/.DS_Store -------------------------------------------------------------------------------- /exercises/ex03_03.py: -------------------------------------------------------------------------------- 1 | def right_justify(s): 2 | padding = 70 - len(s) 3 | return " " * padding + s 4 | 5 | print right_justify("python") 6 | -------------------------------------------------------------------------------- /exercises/ex03_04.py: -------------------------------------------------------------------------------- 1 | def do_twice(f, val): 2 | f(val) 3 | f(val) 4 | 5 | def do_four(f, val): 6 | do_twice(f, val) 7 | do_twice(f, val) 8 | 9 | def print_twice(s): 10 | print s 11 | print s 12 | 13 | do_four(print_twice, "spam") 14 | -------------------------------------------------------------------------------- /exercises/ex03_05.py: -------------------------------------------------------------------------------- 1 | def grid(n): 2 | filled = "+----" 3 | empty = "| " 4 | grid = "" 5 | 6 | for i in range(n): 7 | grid += filled * n + filled[0] + "\n" 8 | grid += 4 * (empty * n + empty[0] + "\n") 9 | 10 | grid += filled * n + filled[0] + "\n" 11 | 12 | return grid 13 | 14 | 15 | print grid(2) 16 | print grid(4) 17 | 18 | -------------------------------------------------------------------------------- /exercises/ex04_02.py: -------------------------------------------------------------------------------- 1 | from swampy.TurtleWorld import * 2 | from polygon import * 3 | 4 | def flower(turtle, petals, radius, angle): 5 | for i in range(petals): 6 | petal(t, radius, angle) 7 | lt(t, 360.0/petals) 8 | 9 | 10 | def petal(turtle, radius, angle): 11 | for i in range(2): 12 | arc(turtle, radius, angle) 13 | lt(turtle, 180-angle) 14 | 15 | 16 | world = TurtleWorld() 17 | t = Turtle() 18 | t.delay = 0.01 19 | 20 | petals = 5 21 | arc_radius = 120 22 | angle = 60 23 | 24 | flower(t, petals, arc_radius, angle) 25 | 26 | die(t) 27 | wait_for_user() 28 | -------------------------------------------------------------------------------- /exercises/ex05_03.py: -------------------------------------------------------------------------------- 1 | def check_fermat(a, b, c, n): 2 | if n > 2: 3 | if a ** n + b ** n == c ** n: 4 | return "It seems that Fermat was wrong." 5 | else: 6 | return "No, that doesn't work." 7 | 8 | def fermat_input(): 9 | a = int(raw_input("Enter variable a: ")) 10 | b = int(raw_input("Enter variable b: ")) 11 | c = int(raw_input("Enter variable c: ")) 12 | n = int(raw_input("Enter exponent n: ")) 13 | print check_fermat(a, b, c, n) 14 | 15 | #print check_fermat(1, 2, 3, 3) 16 | fermat_input() 17 | -------------------------------------------------------------------------------- /exercises/ex05_04.py: -------------------------------------------------------------------------------- 1 | def is_triangle(a, b, c): 2 | if a > b + c or b > a + b or c > a + b: 3 | return "Not a triangle." 4 | else: 5 | return "Triangle." 6 | 7 | def triangle_input(): 8 | a = int(raw_input("Enter length of side a: ")) 9 | b = int(raw_input("Enter length of side b: ")) 10 | c = int(raw_input("Enter length of side c: ")) 11 | print is_triangle(a, b, c) 12 | 13 | #print is_triangle(1,2,3) # degenerate triangle 14 | triangle_input() 15 | -------------------------------------------------------------------------------- /exercises/ex06_05.py: -------------------------------------------------------------------------------- 1 | def ackermann(m, n): 2 | if m == 0: 3 | return n + 1 4 | elif m > 0 and n == 0: 5 | return ackermann(m - 1, 1) 6 | elif m > 0 and n > 0: 7 | return ackermann(m - 1, ackermann(m, n - 1)) 8 | 9 | print ackermann(3, 4) 10 | -------------------------------------------------------------------------------- /exercises/ex06_06.py: -------------------------------------------------------------------------------- 1 | def is_palindrome(word): 2 | 3 | def first(word): 4 | return word[0] 5 | 6 | def last(word): 7 | return word[-1] 8 | 9 | def middle(word): 10 | return word[1:-1] 11 | 12 | if len(word) <= 1: 13 | return True 14 | else: 15 | if first(word) == last(word): 16 | return is_palindrome(middle(word)) 17 | else: 18 | return False 19 | 20 | print is_palindrome("nope") 21 | print is_palindrome("amanaplanacanalpanama") 22 | -------------------------------------------------------------------------------- /exercises/ex06_07.py: -------------------------------------------------------------------------------- 1 | def is_power(a, b): 2 | if a < b: 3 | return False 4 | elif a == b: 5 | return True 6 | else: 7 | if a % b == 0: 8 | return is_power(a/b, b) 9 | else: 10 | return False 11 | 12 | print is_power(16, 2) 13 | print is_power(15, 2) 14 | -------------------------------------------------------------------------------- /exercises/ex06_08.py: -------------------------------------------------------------------------------- 1 | def gcd(a, b): 2 | if b == 0: 3 | return a 4 | else: 5 | return gcd(b, a % b) 6 | 7 | print gcd(169, 13) 8 | print gcd(14, 3) 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /exercises/ex07_03.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def compare_square(): 4 | 5 | def square(a): 6 | x = 1.0 # initial estimate 7 | while True: 8 | y = (x + a/x) / 2 9 | if abs(y - x) < 0.0000001: 10 | break 11 | x = y 12 | return x 13 | 14 | for i in range(1, 10): 15 | a = i * 1.0 16 | square_newton = square(a) 17 | square_lib = math.sqrt(a) 18 | difference = abs(square_newton - square_lib) 19 | print '{0:.1f} {1:.12f} {2:.12f} {3:.12e}'.\ 20 | format(a, square_newton, square_lib, difference) 21 | 22 | compare_square() 23 | 24 | -------------------------------------------------------------------------------- /exercises/ex07_04.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def eval_loop(): 4 | 5 | s = "" 6 | 7 | add = raw_input("> ") 8 | while add != "done": 9 | s += add 10 | add = raw_input("> ") 11 | 12 | print eval(s) 13 | 14 | eval_loop() 15 | 16 | -------------------------------------------------------------------------------- /exercises/ex07_05.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def estimate_pi(): 4 | 5 | second_term = 0.0 6 | k = 0 7 | 8 | while True: 9 | num = math.factorial(4.0 * k) * (1103 + 26390 * k) 10 | denom = math.pow(math.factorial(k), 4) * math.pow(396, 4 * k) 11 | second_term += (num / denom) 12 | if (num / denom) < 1e-15: 13 | break 14 | k += 1 15 | 16 | return ((2 * math.sqrt(2)) / 9801) * second_term 17 | 18 | print estimate_pi(), 1 / math.pi 19 | 20 | -------------------------------------------------------------------------------- /exercises/ex08_10.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | def is_palindrome(s): 4 | return s == s[::-1] 5 | 6 | print is_palindrome("abcd") 7 | print is_palindrome("amanaplanacanalpanama") 8 | -------------------------------------------------------------------------------- /exercises/ex08_12.py: -------------------------------------------------------------------------------- 1 | def rotate_word(s, shift): 2 | result = "" 3 | for char in s: 4 | result += chr((((ord(char) + (shift % 26)) % 123) % 97) + 97) 5 | return result 6 | 7 | print rotate_word("cheer", 7) 8 | print rotate_word("melon", -10) 9 | 10 | -------------------------------------------------------------------------------- /exercises/ex09_07.py: -------------------------------------------------------------------------------- 1 | def consecutive_double_letters(): 2 | 3 | fin = open('words.txt') 4 | 5 | for line in fin: 6 | word = line.strip() 7 | for i in range(len(word)): 8 | if len(word) >= 6 and i < len(word) - 5 \ 9 | and word[i] == word[i+1] \ 10 | and word[i+2] == word[i+3] \ 11 | and word[i+4] == word[i+5]: 12 | print word 13 | 14 | consecutive_double_letters() 15 | -------------------------------------------------------------------------------- /exercises/ex09_08.py: -------------------------------------------------------------------------------- 1 | def odometer_readings(): 2 | 3 | def palindromic(s): 4 | return s == (s)[::-1] 5 | 6 | for i in range(100000, 1000000): 7 | 8 | if palindromic(str(i)[2:]) \ 9 | and palindromic(str(i + 1)[1:]) \ 10 | and palindromic(str(i + 2)[1:5]) \ 11 | and palindromic(str(i + 3)): 12 | print i, i+1, i+2, i+ 3 13 | 14 | odometer_readings() 15 | -------------------------------------------------------------------------------- /exercises/ex09_09.py: -------------------------------------------------------------------------------- 1 | def reversed_ages(): 2 | 3 | age_differences = [i for i in range(14, 45)] 4 | 5 | result = "" 6 | for delta in age_differences: 7 | temp = "" 8 | for i in range(120): 9 | if str(i).zfill(2) == str(i + delta)[::-1]: 10 | temp += str(i).zfill(2) + " " + str(i + delta) + "\n" 11 | if len(temp) > len(result): 12 | result = temp 13 | 14 | return result 15 | 16 | print reversed_ages() 17 | -------------------------------------------------------------------------------- /exercises/ex10_06.py: -------------------------------------------------------------------------------- 1 | def is_sorted(t): 2 | 3 | for i in range(len(t) - 1): 4 | if t[i] > t[i+1]: 5 | return False 6 | return True 7 | 8 | print is_sorted([1,2,2]) 9 | print is_sorted(['b','a']) 10 | -------------------------------------------------------------------------------- /exercises/ex10_07.py: -------------------------------------------------------------------------------- 1 | def is_anagram(s, t): 2 | s = s.lower().replace(" ", "") 3 | t = t.lower().replace(" ", "") 4 | 5 | first = list(s) 6 | second = list(t) 7 | 8 | first.sort() 9 | second.sort() 10 | 11 | return first == second 12 | 13 | print is_anagram("Mary", "Army") 14 | print is_anagram("Jim Morrison", "Mr Mojo Risin") 15 | print is_anagram("War", "Peace") 16 | -------------------------------------------------------------------------------- /exercises/ex10_08.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def birthday_paradox(): 4 | 5 | def has_duplicates(t): 6 | return len(set(t)) < len(t) 7 | 8 | def month(): 9 | months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", \ 10 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] 11 | return months[random.randrange(12)] 12 | 13 | def day(): 14 | days = [31, 28, 31, 30, 31, 30, \ 15 | 31, 31, 30, 31, 30, 31] 16 | if random.randrange(4) == 0: 17 | days[1] = 29 18 | return random.randrange(1, days[random.randrange(12)] + 1) 19 | 20 | def create_birthdays(): 21 | birthdays = [] 22 | for i in range(23): 23 | birthdays.append(month() + "-" + str(day()).zfill(2)) 24 | return birthdays 25 | 26 | def matches(): 27 | runs = 1000 28 | count = 0 29 | 30 | for i in range(runs): 31 | if has_duplicates(create_birthdays()): 32 | count += 1.0 33 | return count / runs 34 | 35 | return matches() 36 | 37 | 38 | print birthday_paradox() 39 | -------------------------------------------------------------------------------- /exercises/ex10_09.py: -------------------------------------------------------------------------------- 1 | def remove_duplicates(t): 2 | result = [] 3 | t.sort() 4 | 5 | if len(t) == 0: 6 | return [] 7 | 8 | result.append(t[0]) 9 | 10 | for i in range(len(t)): 11 | if t[i] != result[-1]: 12 | result.append(t[i]) 13 | return result 14 | 15 | print remove_duplicates([1,2,3,4]) 16 | print remove_duplicates([1,2,2,2,4]) 17 | print remove_duplicates([1,2,2,2]) 18 | print remove_duplicates([2,2,2]) 19 | print remove_duplicates([]) 20 | print remove_duplicates([4,5,3,4]) 21 | -------------------------------------------------------------------------------- /exercises/ex10_10.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | def process_file(): 4 | 5 | fin = open('words.txt') 6 | 7 | def append(): 8 | result = [] 9 | for line in fin: 10 | word = line.strip() 11 | result.append(word) 12 | return result 13 | 14 | def concatenate(): # significantly faster! 15 | result = [] 16 | for line in fin: 17 | word = line.strip() 18 | result += [word] 19 | return result 20 | 21 | start = time.clock() 22 | append() 23 | end = time.clock() 24 | print "Append:\t\t", end - start 25 | 26 | start = time.clock() 27 | concatenate() 28 | end = time.clock() 29 | print "Concatenate:\t", end - start 30 | 31 | process_file() 32 | -------------------------------------------------------------------------------- /exercises/ex10_11.py: -------------------------------------------------------------------------------- 1 | def create_list(): 2 | fin = open('words.txt') 3 | 4 | result = [] 5 | for line in fin: 6 | word = line.strip() 7 | result += [word] 8 | return result 9 | 10 | def bisect(sorted_list, term): 11 | min = 0 12 | max = len(sorted_list) - 1 13 | 14 | while True: 15 | i = (min + max) / 2 16 | if max < min: 17 | return None 18 | if term == sorted_list[i]: 19 | return i 20 | elif term < sorted_list[i]: 21 | max = i - 1 22 | else: 23 | min = i + 1 24 | 25 | #test = ["a", "b", "c", "d", "e", "f"] 26 | #print bisect(test, "f") 27 | 28 | word_list = create_list() 29 | 30 | i = bisect(word_list, "owl") # returns 70015 31 | print word_list[i] == "owl" # True 32 | 33 | print bisect(word_list, "owwl") # returns None 34 | 35 | -------------------------------------------------------------------------------- /exercises/ex10_12.py: -------------------------------------------------------------------------------- 1 | def reverse_pair(): 2 | 3 | def create_list(): 4 | fin = open('words.txt') 5 | 6 | result = [] 7 | for line in fin: 8 | word = line.strip() 9 | result += [word] 10 | return result 11 | 12 | def in_list(sorted_list, term): 13 | min = 0 14 | max = len(sorted_list) - 1 15 | 16 | while True: 17 | i = (min + max) / 2 18 | if max < min: 19 | return False 20 | if term == sorted_list[i]: 21 | return True 22 | elif term < sorted_list[i]: 23 | max = i - 1 24 | else: 25 | min = i + 1 26 | 27 | word_list = create_list() 28 | 29 | for word in word_list: 30 | if in_list(word_list, word[::-1]): 31 | print word, word[::-1] 32 | 33 | reverse_pair() 34 | -------------------------------------------------------------------------------- /exercises/ex10_13.py: -------------------------------------------------------------------------------- 1 | def interlocked_words(n): 2 | 3 | def create_list(): 4 | fin = open('words.txt') 5 | 6 | result = [] 7 | for line in fin: 8 | word = line.strip() 9 | result += [word] 10 | return result 11 | 12 | def in_list(sorted_list, term): 13 | min = 0 14 | max = len(sorted_list) - 1 15 | 16 | while True: 17 | i = (min + max) / 2 18 | if max < min: 19 | return False 20 | if term == sorted_list[i]: 21 | return True 22 | elif term < sorted_list[i]: 23 | max = i - 1 24 | else: 25 | min = i + 1 26 | 27 | word_list = create_list() 28 | 29 | if n == 2: 30 | for word in word_list: 31 | if in_list(word_list, word[::2]) \ 32 | and in_list(word_list, word[1::2]): 33 | print word[::2], word[1::2], word 34 | 35 | elif n == 3: 36 | for word in word_list: 37 | if in_list(word_list, word[::3]) \ 38 | and in_list(word_list, word[1::3]) \ 39 | and in_list(word_list, word[2::3]): 40 | print word[::3], word[1::3], word[2::3], word 41 | 42 | #interlocked_words(2) 43 | interlocked_words(3) 44 | -------------------------------------------------------------------------------- /exercises/ex11_09.py: -------------------------------------------------------------------------------- 1 | def has_duplicates(t): 2 | result = {} 3 | for element in t: 4 | if element in result: 5 | return True 6 | else: 7 | result[element] = 1 8 | return False 9 | 10 | print has_duplicates([1,2,3,4]) 11 | print has_duplicates([1,2,2,4]) 12 | -------------------------------------------------------------------------------- /exercises/ex11_10.py: -------------------------------------------------------------------------------- 1 | def rotate_pairs(): 2 | 3 | def create_dict(): 4 | fin = open('words.txt') 5 | result = {} 6 | 7 | for line in fin: 8 | word = line.strip() 9 | result[word] = word 10 | return result 11 | 12 | def rotate_word(s, shift): 13 | result = "" 14 | for char in s: 15 | result += chr((((ord(char) + (shift % 26)) % 123) % 97) + 97) 16 | return result 17 | 18 | def find_pairs(): 19 | for word in words: 20 | 21 | all_rotations = [] 22 | 23 | for i in range(1, 26): 24 | rotated = rotate_word(word, i) 25 | all_rotations += [rotated] 26 | 27 | for i in range(len(all_rotations)): 28 | if all_rotations[i] in words: 29 | print word, i + 1, all_rotations[i] 30 | 31 | words = create_dict() 32 | find_pairs() 33 | 34 | 35 | rotate_pairs() 36 | 37 | """ 38 | def test(s, shift): 39 | def rotate_word(s, shift): 40 | result = "" 41 | for char in s: 42 | result += chr((((ord(char) + (shift % 26)) % 123) % 97) + 97) 43 | return result 44 | return rotate_word(s, shift) 45 | 46 | print test("fit", 9) 47 | print test("orc", -9) 48 | """ 49 | -------------------------------------------------------------------------------- /exercises/ex11_11.py: -------------------------------------------------------------------------------- 1 | def homophones_puzzler(): 2 | 3 | def create_dict(): 4 | fin = open('words.txt') 5 | result = {} 6 | for line in fin: 7 | word = line.strip() 8 | if len(word) == 5: 9 | result[word] = (word[1:], word[0] + word[2:]) 10 | return result 11 | 12 | def create_pronunciation_dict(): 13 | d = dict() 14 | fin = open('c06d.txt') 15 | for line in fin: 16 | 17 | # skip over the comments 18 | if line[0] == '#': 19 | continue 20 | 21 | t = line.split() 22 | word = t[0].lower() 23 | pron = ' '.join(t[1:]) 24 | d[word] = pron 25 | 26 | return d 27 | 28 | words = create_dict() 29 | p = create_pronunciation_dict() 30 | 31 | for elements in words.items(): 32 | first = elements[0] 33 | second = elements[1][0] 34 | third = elements[1][1] 35 | if first in p and second in p and third in p: 36 | if p[first] == p[second] == p[third]: 37 | print first, second, third 38 | 39 | homophones_puzzler() 40 | 41 | """ 42 | 43 | def read_dictionary(filename='c06d'): 44 | 45 | d = dict() 46 | fin = open(filename) 47 | for line in fin: 48 | 49 | # skip over the comments 50 | if line[0] == '#': continue 51 | 52 | t = line.split() 53 | word = t[0].lower() 54 | pron = ' '.join(t[1:]) 55 | d[word] = pron 56 | 57 | return d 58 | 59 | if __name__ == '__main__': 60 | d = read_dictionary() 61 | for k, v in d.items(): 62 | print k, v 63 | """ 64 | -------------------------------------------------------------------------------- /exercises/ex12_03.py: -------------------------------------------------------------------------------- 1 | def letter_frequencies(filename): 2 | 3 | freq = {} 4 | output = [] 5 | fin = open(filename).read() 6 | 7 | for char in fin: 8 | if char.isalpha(): 9 | char = char.lower() 10 | if char in freq: 11 | freq[char] += 1 12 | else: 13 | freq[char] = 1 14 | 15 | for key, value in freq.items(): 16 | output.append((value, key)) 17 | 18 | output.sort(reverse = True) 19 | 20 | for (count, letter) in output: 21 | print letter, count 22 | 23 | letter_frequencies("pg1661.txt") 24 | -------------------------------------------------------------------------------- /exercises/ex12_04.py: -------------------------------------------------------------------------------- 1 | def find_anagrams(): 2 | 3 | def create_dict(): 4 | fin = open('words.txt') 5 | res = {} 6 | 7 | for line in fin: 8 | word = line.strip() 9 | s = list(word) 10 | s.sort() 11 | 12 | sorted_char_string = "" 13 | for char in s: 14 | sorted_char_string += char 15 | 16 | if sorted_char_string in res: 17 | res[sorted_char_string] += [word] 18 | else: 19 | res[sorted_char_string] = [word] 20 | 21 | return res 22 | 23 | def sort_dict(d): 24 | res = [] 25 | 26 | for key, value in d.items(): 27 | res += [(len(value), value)] 28 | return res 29 | 30 | dictionary = create_dict() 31 | return sort_dict(dictionary) 32 | 33 | # part 1 and 2 of the exercise 34 | def print_anagrams(anagram_list, sorted): 35 | if sorted: 36 | anagram_list.sort(reverse = True) 37 | for (count, anagrams) in anagram_list: 38 | if count >= 2: 39 | print count, anagrams 40 | 41 | # part 3 42 | def scrabble_bingos(anagram_list): 43 | #anagram_list.sort(reverse = True) 44 | 45 | res = [] 46 | for (count, anagrams) in anagram_list: 47 | if len(anagrams[0]) == 8 and count >= 2: 48 | res += [(count, anagrams)] 49 | 50 | res.sort(reverse = True) 51 | print res[0] 52 | 53 | anagram_list = find_anagrams() 54 | #print_anagrams(anagram_list, True) 55 | scrabble_bingos(anagram_list) 56 | -------------------------------------------------------------------------------- /exercises/ex12_05.py: -------------------------------------------------------------------------------- 1 | def metathesis_pairs(): 2 | 3 | def create_dict(): 4 | fin = open('words.txt') 5 | res = {} 6 | 7 | for line in fin: 8 | word = line.strip() 9 | res[word] = [] 10 | 11 | return res 12 | 13 | def find_pairs(d): 14 | res = [] 15 | 16 | for key, value in d.items(): 17 | k = list(key) 18 | for i in range(len(key) - 1): 19 | for j in range(1, len(key)): 20 | k[i], k[j] = k[j], k[i] 21 | candidate = make_string(k) 22 | if candidate in d: 23 | print key, candidate 24 | return res 25 | 26 | def make_string(k): 27 | res = "" 28 | for element in k: 29 | res += element 30 | return res 31 | 32 | d = create_dict() 33 | find_pairs(d) 34 | 35 | print metathesis_pairs() 36 | -------------------------------------------------------------------------------- /exercises/ex12_06.py: -------------------------------------------------------------------------------- 1 | def reduce_words(): 2 | 3 | def create_dict(): 4 | fin = open('words.txt') 5 | res = {} 6 | 7 | for line in fin: 8 | word = line.strip() 9 | res[word] = [] 10 | 11 | for element in ["a", "i", ""]: 12 | res[element] = [] 13 | 14 | return res 15 | 16 | def add_children(d): 17 | for key in d.keys(): 18 | children = [] 19 | for i in range(len(key)): 20 | candidate = key[:i] + key[i+1:] 21 | if candidate in d and candidate not in children: 22 | children.append(candidate) 23 | d[key] = children 24 | 25 | return d 26 | 27 | def recursive_trails(d): 28 | res = [] 29 | 30 | def helper(key, result): 31 | if d[key] == []: 32 | return 33 | if key in ["a", "i", ""]: 34 | res.append((len(result[0]), result)) 35 | else: 36 | for entry in d[key]: 37 | return helper(entry, result + [entry]) 38 | 39 | for key,value in d.items(): 40 | helper(key, [key]) 41 | 42 | return res 43 | 44 | def top_n(results, n): 45 | results.sort(reverse = True) 46 | for i in range(n): 47 | print results[i] 48 | 49 | d = create_dict() 50 | add_children(d) # creates dictionary entries for words that are reducible 51 | trails = recursive_trails(d) 52 | top_n(trails, 20) 53 | 54 | reduce_words() 55 | -------------------------------------------------------------------------------- /exercises/ex13_09.py: -------------------------------------------------------------------------------- 1 | import string 2 | import math 3 | 4 | def zipfs_law(): 5 | 6 | def create_dict(): 7 | fin = open('pg1661.txt') 8 | res = {} 9 | 10 | for line in fin: 11 | line = line.lower() 12 | for c in string.punctuation: 13 | line = line.replace(c, " ") 14 | words = line.split() 15 | 16 | for word in words: 17 | if word in res: 18 | res[word] = res[word] + 1 19 | else: 20 | res[word] = 1 21 | return res 22 | 23 | def create_ranks(d): 24 | res = [] 25 | for key, value in d.items(): 26 | res += [(value, key)] 27 | res.sort(reverse = True) 28 | return res 29 | 30 | def output(ranks): 31 | f = open("plot_res.csv", "w") 32 | for i in range(len(ranks)): 33 | line = str(math.log(ranks[i][0])) + ", " + str(math.log(i + 1)) + "\n" 34 | f.write(line) 35 | f.close() 36 | 37 | d = create_dict() 38 | ranks = create_ranks(d) 39 | output(ranks) 40 | 41 | zipfs_law() 42 | -------------------------------------------------------------------------------- /exercises/ex14_06.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | import string 3 | 4 | def get_zip(): 5 | zip_code = raw_input("Enter valid US zip Code, e.g. 32707: \n>") 6 | conn_str = "http://www.uszip.com/zip/" + zip_code 7 | 8 | conn = urllib.urlopen(conn_str) 9 | 10 | lines = [] 11 | town = "" 12 | population = 0 13 | 14 | for line in conn: 15 | lines += [line.strip()] 16 | 17 | for i in range(len(lines)): 18 | 19 | if lines[i][:8] == "
": 20 | s = lines[i+2] 21 | s = s[len("

"):] 22 | s = s[:s.find("<")] 23 | s = s.strip() 24 | 25 | for c in string.punctuation: 26 | s = s.replace(c, "") 27 | 28 | town = s 29 | 30 | if lines[i].lower().find("total population") != -1: 31 | s = lines[i] 32 | start = s.find("Total population") + \ 33 | len("Total population") + len("
") 34 | end = s.find(" 0: 25 | print delta 26 | else: 27 | today_next_year = today.replace(year = today.year + 1) 28 | print today_next_year - normalized_birthday 29 | 30 | #b = datetime(1990, 8, 14) 31 | #birthday(b) 32 | 33 | 34 | # Part 3: 35 | def double_day(b1, b2): 36 | delta = b1 - b2 37 | double_day = b1 + delta 38 | return double_day 39 | 40 | #b1 = datetime(2000, 2, 20) 41 | #b2 = datetime(1990, 8, 14) 42 | #print double_day(b1, b2) 43 | 44 | 45 | -------------------------------------------------------------------------------- /extra/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gregorulm/think_python/9e15b538b157c6ec809bb404a9169eff386e67e9/extra/.DS_Store -------------------------------------------------------------------------------- /extra/polygon.py: -------------------------------------------------------------------------------- 1 | """This module contains code from 2 | Think Python by Allen B. Downey 3 | http://thinkpython.com 4 | 5 | Copyright 2012 Allen B. Downey 6 | License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html 7 | 8 | """ 9 | 10 | import math 11 | 12 | try: 13 | # see if Swampy is installed as a package 14 | from swampy.TurtleWorld import * 15 | except ImportError: 16 | # otherwise see if the modules are on the PYTHONPATH 17 | from TurtleWorld import * 18 | 19 | 20 | def square(t, length): 21 | """Draws a square with sides of the given length. 22 | 23 | Returns the Turtle to the starting position and location. 24 | """ 25 | for i in range(4): 26 | fd(t, length) 27 | lt(t) 28 | 29 | 30 | def polyline(t, n, length, angle): 31 | """Draws n line segments. 32 | 33 | t: Turtle object 34 | n: number of line segments 35 | length: length of each segment 36 | angle: degrees between segments 37 | """ 38 | for i in range(n): 39 | fd(t, length) 40 | lt(t, angle) 41 | 42 | 43 | def polygon(t, n, length): 44 | """Draws a polygon with n sides. 45 | 46 | t: Turtle 47 | n: number of sides 48 | length: length of each side. 49 | """ 50 | angle = 360.0/n 51 | polyline(t, n, length, angle) 52 | 53 | 54 | def arc(t, r, angle): 55 | """Draws an arc with the given radius and angle. 56 | 57 | t: Turtle 58 | r: radius 59 | angle: angle subtended by the arc, in degrees 60 | """ 61 | arc_length = 2 * math.pi * r * abs(angle) / 360 62 | n = int(arc_length / 4) + 1 63 | step_length = arc_length / n 64 | step_angle = float(angle) / n 65 | 66 | # making a slight left turn before starting reduces 67 | # the error caused by the linear approximation of the arc 68 | lt(t, step_angle/2) 69 | polyline(t, n, step_length, step_angle) 70 | rt(t, step_angle/2) 71 | 72 | 73 | def circle(t, r): 74 | """Draws a circle with the given radius. 75 | 76 | t: Turtle 77 | r: radius 78 | """ 79 | arc(t, r, 360) 80 | 81 | 82 | # the following condition checks whether we are 83 | # running as a script, in which case run the test code, 84 | # or being imported, in which case don't. 85 | 86 | if __name__ == '__main__': 87 | world = TurtleWorld() 88 | 89 | bob = Turtle() 90 | bob.delay = 0.001 91 | 92 | # draw a circle centered on the origin 93 | radius = 100 94 | pu(bob) 95 | fd(bob, radius) 96 | lt(bob) 97 | pd(bob) 98 | circle(bob, radius) 99 | 100 | wait_for_user() 101 | -------------------------------------------------------------------------------- /extra/polygon.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gregorulm/think_python/9e15b538b157c6ec809bb404a9169eff386e67e9/extra/polygon.pyc -------------------------------------------------------------------------------- /extra/swampy-2.1.1.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gregorulm/think_python/9e15b538b157c6ec809bb404a9169eff386e67e9/extra/swampy-2.1.1.zip -------------------------------------------------------------------------------- /extra/thinkpython_v2.0.9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gregorulm/think_python/9e15b538b157c6ec809bb404a9169eff386e67e9/extra/thinkpython_v2.0.9.pdf -------------------------------------------------------------------------------- /extra/words2.txt: -------------------------------------------------------------------------------- 1 | aa 2 | aah 3 | aahed 4 | aahing 5 | aahs 6 | aal 7 | aalii 8 | aaliis 9 | aals 10 | aardvark 11 | aardvarks 12 | aardwolf 13 | aardwolves 14 | aas 15 | aasvogel 16 | aasvogels 17 | aba 18 | abaca 19 | abacas 20 | abaci 21 | aback 22 | abacus 23 | abacuses 24 | abaft 25 | abaka 26 | abakas 27 | abalone 28 | abalones 29 | abamp 30 | abampere 31 | abamperes 32 | abamps 33 | abandon 34 | abandoned 35 | abandoning 36 | abandonment 37 | abandonments 38 | abandons 39 | abas 40 | abase 41 | abased 42 | abasedly 43 | abasement 44 | abasements 45 | abaser 46 | abasers 47 | abases 48 | abash 49 | abashed 50 | abashes 51 | abashing 52 | abasing 53 | abatable 54 | abate 55 | abated 56 | abatement 57 | abatements 58 | abater 59 | abaters 60 | abates 61 | abating 62 | abatis 63 | abatises 64 | abator 65 | abators 66 | abattis 67 | abattises 68 | abattoir 69 | abattoirs 70 | abaxial 71 | abaxile 72 | abbacies 73 | abbacy 74 | abbatial 75 | abbe 76 | abbes 77 | abbess 78 | abbesses 79 | abbey 80 | abbeys 81 | abbot 82 | abbotcies 83 | abbotcy 84 | abbots 85 | abbreviate 86 | abbreviated 87 | abbreviates 88 | abbreviating 89 | abbreviation 90 | abbreviations 91 | abdicate 92 | abdicated 93 | abdicates 94 | abdicating 95 | abdication 96 | abdications 97 | abdomen 98 | abdomens 99 | abdomina 100 | abdominal 101 | abdominally 102 | abduce 103 | abduced 104 | abducens 105 | abducent 106 | abducentes 107 | abduces 108 | abducing 109 | abduct 110 | abducted 111 | abducting 112 | abductor 113 | abductores 114 | abductors 115 | abducts 116 | abeam 117 | abed 118 | abele 119 | abeles 120 | abelmosk 121 | abelmosks 122 | aberrant 123 | aberrants 124 | aberration 125 | aberrations 126 | abet 127 | abetment 128 | abetments 129 | abets 130 | abettal 131 | abettals 132 | abetted 133 | abetter 134 | abetters 135 | abetting 136 | abettor 137 | abettors 138 | abeyance 139 | abeyances 140 | abeyancies 141 | abeyancy 142 | abeyant 143 | abfarad 144 | abfarads 145 | abhenries 146 | abhenry 147 | abhenrys 148 | abhor 149 | abhorred 150 | abhorrence 151 | abhorrences 152 | abhorrent 153 | abhorrer 154 | abhorrers 155 | abhorring 156 | abhors 157 | abidance 158 | abidances 159 | abide 160 | abided 161 | abider 162 | abiders 163 | abides 164 | abiding 165 | abied 166 | abies 167 | abigail 168 | abigails 169 | abilities 170 | ability 171 | abioses 172 | abiosis 173 | abiotic 174 | abject 175 | abjectly 176 | abjectness 177 | abjectnesses 178 | abjuration 179 | abjurations 180 | abjure 181 | abjured 182 | abjurer 183 | abjurers 184 | abjures 185 | abjuring 186 | ablate 187 | ablated 188 | ablates 189 | ablating 190 | ablation 191 | ablations 192 | ablative 193 | ablatives 194 | ablaut 195 | ablauts 196 | ablaze 197 | able 198 | ablegate 199 | ablegates 200 | abler 201 | ables 202 | ablest 203 | ablings 204 | ablins 205 | abloom 206 | abluent 207 | abluents 208 | ablush 209 | abluted 210 | ablution 211 | ablutions 212 | ably 213 | abmho 214 | abmhos 215 | abnegate 216 | abnegated 217 | abnegates 218 | abnegating 219 | abnegation 220 | abnegations 221 | abnormal 222 | abnormalities 223 | abnormality 224 | abnormally 225 | abnormals 226 | abo 227 | aboard 228 | abode 229 | aboded 230 | abodes 231 | aboding 232 | abohm 233 | abohms 234 | aboideau 235 | aboideaus 236 | aboideaux 237 | aboil 238 | aboiteau 239 | aboiteaus 240 | aboiteaux 241 | abolish 242 | abolished 243 | abolishes 244 | abolishing 245 | abolition 246 | abolitions 247 | abolla 248 | abollae 249 | aboma 250 | abomas 251 | abomasa 252 | abomasal 253 | abomasi 254 | abomasum 255 | abomasus 256 | abominable 257 | abominate 258 | abominated 259 | abominates 260 | abominating 261 | abomination 262 | abominations 263 | aboon 264 | aboral 265 | aborally 266 | aboriginal 267 | aborigine 268 | aborigines 269 | aborning 270 | abort 271 | aborted 272 | aborter 273 | aborters 274 | aborting 275 | abortion 276 | abortions 277 | abortive 278 | aborts 279 | abos 280 | abought 281 | aboulia 282 | aboulias 283 | aboulic 284 | abound 285 | abounded 286 | abounding 287 | abounds 288 | about 289 | above 290 | aboveboard 291 | aboves 292 | abracadabra 293 | abradant 294 | abradants 295 | abrade 296 | abraded 297 | abrader 298 | abraders 299 | abrades 300 | abrading 301 | abrasion 302 | abrasions 303 | abrasive 304 | abrasively 305 | abrasiveness 306 | abrasivenesses 307 | abrasives 308 | abreact 309 | abreacted 310 | abreacting 311 | abreacts 312 | abreast 313 | abri 314 | abridge 315 | abridged 316 | abridgement 317 | abridgements 318 | abridger 319 | abridgers 320 | abridges 321 | abridging 322 | abridgment 323 | abridgments 324 | abris 325 | abroach 326 | abroad 327 | abrogate 328 | abrogated 329 | abrogates 330 | abrogating 331 | abrupt 332 | abrupter 333 | abruptest 334 | abruptly 335 | abscess 336 | abscessed 337 | abscesses 338 | abscessing 339 | abscise 340 | abscised 341 | abscises 342 | abscisin 343 | abscising 344 | abscisins 345 | abscissa 346 | abscissae 347 | abscissas 348 | abscond 349 | absconded 350 | absconding 351 | absconds 352 | absence 353 | absences 354 | absent 355 | absented 356 | absentee 357 | absentees 358 | absenter 359 | absenters 360 | absenting 361 | absently 362 | absentminded 363 | absentmindedly 364 | absentmindedness 365 | absentmindednesses 366 | absents 367 | absinth 368 | absinthe 369 | absinthes 370 | absinths 371 | absolute 372 | absolutely 373 | absoluter 374 | absolutes 375 | absolutest 376 | absolution 377 | absolutions 378 | absolve 379 | absolved 380 | absolver 381 | absolvers 382 | absolves 383 | absolving 384 | absonant 385 | absorb 386 | absorbed 387 | absorbencies 388 | absorbency 389 | absorbent 390 | absorber 391 | absorbers 392 | absorbing 393 | absorbingly 394 | absorbs 395 | absorption 396 | absorptions 397 | absorptive 398 | abstain 399 | abstained 400 | abstainer 401 | abstainers 402 | abstaining 403 | abstains 404 | abstemious 405 | abstemiously 406 | abstention 407 | abstentions 408 | absterge 409 | absterged 410 | absterges 411 | absterging 412 | abstinence 413 | abstinences 414 | abstract 415 | abstracted 416 | abstracter 417 | abstractest 418 | abstracting 419 | abstraction 420 | abstractions 421 | abstractly 422 | abstractness 423 | abstractnesses 424 | abstracts 425 | abstrict 426 | abstricted 427 | abstricting 428 | abstricts 429 | abstruse 430 | abstrusely 431 | abstruseness 432 | abstrusenesses 433 | abstruser 434 | abstrusest 435 | absurd 436 | absurder 437 | absurdest 438 | absurdities 439 | absurdity 440 | absurdly 441 | absurds 442 | abubble 443 | abulia 444 | abulias 445 | abulic 446 | abundance 447 | abundances 448 | abundant 449 | abundantly 450 | abusable 451 | abuse 452 | abused 453 | abuser 454 | abusers 455 | abuses 456 | abusing 457 | abusive 458 | abusively 459 | abusiveness 460 | abusivenesses 461 | abut 462 | abutilon 463 | abutilons 464 | abutment 465 | abutments 466 | abuts 467 | abuttal 468 | abuttals 469 | abutted 470 | abutter 471 | abutters 472 | abutting 473 | abuzz 474 | abvolt 475 | abvolts 476 | abwatt 477 | abwatts 478 | aby 479 | abye 480 | abyed 481 | abyes 482 | abying 483 | abys 484 | abysm 485 | abysmal 486 | abysmally 487 | abysms 488 | abyss 489 | abyssal 490 | abysses 491 | acacia 492 | acacias 493 | academe 494 | academes 495 | academia 496 | academias 497 | academic 498 | academically 499 | academics 500 | academies 501 | academy 502 | acajou 503 | acajous 504 | acaleph 505 | acalephae 506 | acalephe 507 | acalephes 508 | acalephs 509 | acanthi 510 | acanthus 511 | acanthuses 512 | acari 513 | acarid 514 | acaridan 515 | acaridans 516 | acarids 517 | acarine 518 | acarines 519 | acaroid 520 | acarpous 521 | acarus 522 | acaudal 523 | acaudate 524 | acauline 525 | acaulose 526 | acaulous 527 | accede 528 | acceded 529 | acceder 530 | acceders 531 | accedes 532 | acceding 533 | accelerate 534 | accelerated 535 | accelerates 536 | accelerating 537 | acceleration 538 | accelerations 539 | accelerator 540 | accelerators 541 | accent 542 | accented 543 | accenting 544 | accentor 545 | accentors 546 | accents 547 | accentual 548 | accentuate 549 | accentuated 550 | accentuates 551 | accentuating 552 | accentuation 553 | accentuations 554 | accept 555 | acceptabilities 556 | acceptability 557 | acceptable 558 | acceptance 559 | acceptances 560 | accepted 561 | acceptee 562 | acceptees 563 | accepter 564 | accepters 565 | accepting 566 | acceptor 567 | acceptors 568 | accepts 569 | access 570 | accessed 571 | accesses 572 | accessibilities 573 | accessibility 574 | accessible 575 | accessing 576 | accession 577 | accessions 578 | accessories 579 | accessory 580 | accident 581 | accidental 582 | accidentally 583 | accidentals 584 | accidents 585 | accidie 586 | accidies 587 | acclaim 588 | acclaimed 589 | acclaiming 590 | acclaims 591 | acclamation 592 | acclamations 593 | acclimate 594 | acclimated 595 | acclimates 596 | acclimating 597 | acclimation 598 | acclimations 599 | acclimatization 600 | acclimatizations 601 | acclimatize 602 | acclimatizes 603 | accolade 604 | accolades 605 | accommodate 606 | accommodated 607 | accommodates 608 | accommodating 609 | accommodation 610 | accommodations 611 | accompanied 612 | accompanies 613 | accompaniment 614 | accompaniments 615 | accompanist 616 | accompany 617 | accompanying 618 | accomplice 619 | accomplices 620 | accomplish 621 | accomplished 622 | accomplisher 623 | accomplishers 624 | accomplishes 625 | accomplishing 626 | accomplishment 627 | accomplishments 628 | accord 629 | accordance 630 | accordant 631 | accorded 632 | accorder 633 | accorders 634 | according 635 | accordingly 636 | accordion 637 | accordions 638 | accords 639 | accost 640 | accosted 641 | accosting 642 | accosts 643 | account 644 | accountabilities 645 | accountability 646 | accountable 647 | accountancies 648 | accountancy 649 | accountant 650 | accountants 651 | accounted 652 | accounting 653 | accountings 654 | accounts 655 | accouter 656 | accoutered 657 | accoutering 658 | accouters 659 | accoutre 660 | accoutred 661 | accoutrement 662 | accoutrements 663 | accoutres 664 | accoutring 665 | accredit 666 | accredited 667 | accrediting 668 | accredits 669 | accrete 670 | accreted 671 | accretes 672 | accreting 673 | accrual 674 | accruals 675 | accrue 676 | accrued 677 | accrues 678 | accruing 679 | accumulate 680 | accumulated 681 | accumulates 682 | accumulating 683 | accumulation 684 | accumulations 685 | accumulator 686 | accumulators 687 | accuracies 688 | accuracy 689 | accurate 690 | accurately 691 | accurateness 692 | accuratenesses 693 | accursed 694 | accurst 695 | accusal 696 | accusals 697 | accusant 698 | accusants 699 | accusation 700 | accusations 701 | accuse 702 | accused 703 | accuser 704 | accusers 705 | accuses 706 | accusing 707 | accustom 708 | accustomed 709 | accustoming 710 | accustoms 711 | ace 712 | aced 713 | acedia 714 | acedias 715 | aceldama 716 | aceldamas 717 | acentric 718 | acequia 719 | acequias 720 | acerate 721 | acerated 722 | acerb 723 | acerbate 724 | acerbated 725 | acerbates 726 | acerbating 727 | acerber 728 | acerbest 729 | acerbic 730 | acerbities 731 | acerbity 732 | acerola 733 | acerolas 734 | acerose 735 | acerous 736 | acers 737 | acervate 738 | acervuli 739 | aces 740 | acescent 741 | acescents 742 | aceta 743 | acetal 744 | acetals 745 | acetamid 746 | acetamids 747 | acetate 748 | acetated 749 | acetates 750 | acetic 751 | acetified 752 | acetifies 753 | acetify 754 | acetifying 755 | acetone 756 | acetones 757 | acetonic 758 | acetose 759 | acetous 760 | acetoxyl 761 | acetoxyls 762 | acetum 763 | acetyl 764 | acetylene 765 | acetylenes 766 | acetylic 767 | acetyls 768 | ache 769 | ached 770 | achene 771 | achenes 772 | achenial 773 | aches 774 | achier 775 | achiest 776 | achievable 777 | achieve 778 | achieved 779 | achievement 780 | achievements 781 | achiever 782 | achievers 783 | achieves 784 | achieving 785 | achiness 786 | achinesses 787 | aching 788 | achingly 789 | achiote 790 | achiotes 791 | achoo 792 | achromat 793 | achromats 794 | achromic 795 | achy 796 | acicula 797 | aciculae 798 | acicular 799 | aciculas 800 | acid 801 | acidhead 802 | acidheads 803 | acidic 804 | acidified 805 | acidifies 806 | acidify 807 | acidifying 808 | acidities 809 | acidity 810 | acidly 811 | acidness 812 | acidnesses 813 | acidoses 814 | acidosis 815 | acidotic 816 | acids 817 | acidy 818 | acierate 819 | acierated 820 | acierates 821 | acierating 822 | aciform 823 | acinar 824 | acing 825 | acini 826 | acinic 827 | acinose 828 | acinous 829 | acinus 830 | acknowledge 831 | acknowledged 832 | acknowledgement 833 | acknowledgements 834 | acknowledges 835 | acknowledging 836 | acknowledgment 837 | acknowledgments 838 | aclinic 839 | acmatic 840 | acme 841 | acmes 842 | acmic 843 | acne 844 | acned 845 | acnes 846 | acnode 847 | acnodes 848 | acock 849 | acold 850 | acolyte 851 | acolytes 852 | aconite 853 | aconites 854 | aconitic 855 | aconitum 856 | aconitums 857 | acorn 858 | acorns 859 | acoustic 860 | acoustical 861 | acoustically 862 | acoustics 863 | acquaint 864 | acquaintance 865 | acquaintances 866 | acquaintanceship 867 | acquaintanceships 868 | acquainted 869 | acquainting 870 | acquaints 871 | acquest 872 | acquests 873 | acquiesce 874 | acquiesced 875 | acquiescence 876 | acquiescences 877 | acquiescent 878 | acquiescently 879 | acquiesces 880 | acquiescing 881 | acquire 882 | acquired 883 | acquirer 884 | acquirers 885 | acquires 886 | acquiring 887 | acquisition 888 | acquisitions 889 | acquisitive 890 | acquit 891 | acquits 892 | acquitted 893 | acquitting 894 | acrasin 895 | acrasins 896 | acre 897 | acreage 898 | acreages 899 | acred 900 | acres 901 | acrid 902 | acrider 903 | acridest 904 | acridine 905 | acridines 906 | acridities 907 | acridity 908 | acridly 909 | acridness 910 | acridnesses 911 | acrimonies 912 | acrimonious 913 | acrimony 914 | acrobat 915 | acrobatic 916 | acrobats 917 | acrodont 918 | acrodonts 919 | acrogen 920 | acrogens 921 | acrolein 922 | acroleins 923 | acrolith 924 | acroliths 925 | acromia 926 | acromial 927 | acromion 928 | acronic 929 | acronym 930 | acronyms 931 | across 932 | acrostic 933 | acrostics 934 | acrotic 935 | acrotism 936 | acrotisms 937 | acrylate 938 | acrylates 939 | acrylic 940 | acrylics 941 | act 942 | acta 943 | actable 944 | acted 945 | actin 946 | actinal 947 | acting 948 | actings 949 | actinia 950 | actiniae 951 | actinian 952 | actinians 953 | actinias 954 | actinic 955 | actinide 956 | actinides 957 | actinism 958 | actinisms 959 | actinium 960 | actiniums 961 | actinoid 962 | actinoids 963 | actinon 964 | actinons 965 | actins 966 | action 967 | actions 968 | activate 969 | activated 970 | activates 971 | activating 972 | activation 973 | activations 974 | active 975 | actively 976 | actives 977 | activism 978 | activisms 979 | activist 980 | activists 981 | activities 982 | activity 983 | actor 984 | actorish 985 | actors 986 | actress 987 | actresses 988 | acts 989 | actual 990 | actualities 991 | actuality 992 | actualization 993 | actualizations 994 | actualize 995 | actualized 996 | actualizes 997 | actualizing 998 | actually 999 | actuarial 1000 | actuaries 1001 | actuary 1002 | actuate 1003 | actuated 1004 | actuates 1005 | actuating 1006 | actuator 1007 | actuators 1008 | acuate 1009 | acuities 1010 | acuity 1011 | aculeate 1012 | acumen 1013 | acumens 1014 | acupuncture 1015 | acupunctures 1016 | acupuncturist 1017 | acupuncturists 1018 | acutance 1019 | acutances 1020 | acute 1021 | acutely 1022 | acuteness 1023 | acutenesses 1024 | acuter 1025 | acutes 1026 | acutest 1027 | acyclic 1028 | acyl 1029 | acylate 1030 | acylated 1031 | acylates 1032 | acylating 1033 | acyls 1034 | ad 1035 | adage 1036 | adages 1037 | adagial 1038 | adagio 1039 | adagios 1040 | adamance 1041 | adamances 1042 | adamancies 1043 | adamancy 1044 | adamant 1045 | adamantlies 1046 | adamantly 1047 | adamants 1048 | adamsite 1049 | adamsites 1050 | adapt 1051 | adaptabilities 1052 | adaptability 1053 | adaptable 1054 | adaptation 1055 | adaptations 1056 | adapted 1057 | adapter 1058 | adapters 1059 | adapting 1060 | adaption 1061 | adaptions 1062 | adaptive 1063 | adaptor 1064 | adaptors 1065 | adapts 1066 | adaxial 1067 | add 1068 | addable 1069 | addax 1070 | addaxes 1071 | added 1072 | addedly 1073 | addend 1074 | addenda 1075 | addends 1076 | addendum 1077 | adder 1078 | adders 1079 | addible 1080 | addict 1081 | addicted 1082 | addicting 1083 | addiction 1084 | addictions 1085 | addictive 1086 | addicts 1087 | adding 1088 | addition 1089 | additional 1090 | additionally 1091 | additions 1092 | additive 1093 | additives 1094 | addle 1095 | addled 1096 | addles 1097 | addling 1098 | address 1099 | addressable 1100 | addressed 1101 | addresses 1102 | addressing 1103 | addrest 1104 | adds 1105 | adduce 1106 | adduced 1107 | adducent 1108 | adducer 1109 | adducers 1110 | adduces 1111 | adducing 1112 | adduct 1113 | adducted 1114 | adducting 1115 | adductor 1116 | adductors 1117 | adducts 1118 | adeem 1119 | adeemed 1120 | adeeming 1121 | adeems 1122 | adenine 1123 | adenines 1124 | adenitis 1125 | adenitises 1126 | adenoid 1127 | adenoidal 1128 | adenoids 1129 | adenoma 1130 | adenomas 1131 | adenomata 1132 | adenopathy 1133 | adenyl 1134 | adenyls 1135 | adept 1136 | adepter 1137 | adeptest 1138 | adeptly 1139 | adeptness 1140 | adeptnesses 1141 | adepts 1142 | adequacies 1143 | adequacy 1144 | adequate 1145 | adequately 1146 | adhere 1147 | adhered 1148 | adherence 1149 | adherences 1150 | adherend 1151 | adherends 1152 | adherent 1153 | adherents 1154 | adherer 1155 | adherers 1156 | adheres 1157 | adhering 1158 | adhesion 1159 | adhesions 1160 | adhesive 1161 | adhesives 1162 | adhibit 1163 | adhibited 1164 | adhibiting 1165 | adhibits 1166 | adieu 1167 | adieus 1168 | adieux 1169 | adios 1170 | adipic 1171 | adipose 1172 | adiposes 1173 | adiposis 1174 | adipous 1175 | adit 1176 | adits 1177 | adjacent 1178 | adjectival 1179 | adjectivally 1180 | adjective 1181 | adjectives 1182 | adjoin 1183 | adjoined 1184 | adjoining 1185 | adjoins 1186 | adjoint 1187 | adjoints 1188 | adjourn 1189 | adjourned 1190 | adjourning 1191 | adjournment 1192 | adjournments 1193 | adjourns 1194 | adjudge 1195 | adjudged 1196 | adjudges 1197 | adjudging 1198 | adjudicate 1199 | adjudicated 1200 | adjudicates 1201 | adjudicating 1202 | adjudication 1203 | adjudications 1204 | adjunct 1205 | adjuncts 1206 | adjure 1207 | adjured 1208 | adjurer 1209 | adjurers 1210 | adjures 1211 | adjuring 1212 | adjuror 1213 | adjurors 1214 | adjust 1215 | adjustable 1216 | adjusted 1217 | adjuster 1218 | adjusters 1219 | adjusting 1220 | adjustment 1221 | adjustments 1222 | adjustor 1223 | adjustors 1224 | adjusts 1225 | adjutant 1226 | adjutants 1227 | adjuvant 1228 | adjuvants 1229 | adman 1230 | admass 1231 | admen 1232 | administer 1233 | administers 1234 | administrable 1235 | administrant 1236 | administrants 1237 | administration 1238 | administrations 1239 | administrative 1240 | administratively 1241 | administrator 1242 | administrators 1243 | adminstration 1244 | adminstrations 1245 | admiral 1246 | admirals 1247 | admiration 1248 | admirations 1249 | admire 1250 | admired 1251 | admirer 1252 | admirers 1253 | admires 1254 | admiring 1255 | admiringly 1256 | admissibilities 1257 | admissibility 1258 | admissible 1259 | admissibly 1260 | admission 1261 | admissions 1262 | admit 1263 | admits 1264 | admittance 1265 | admittances 1266 | admitted 1267 | admittedly 1268 | admitter 1269 | admitters 1270 | admitting 1271 | admix 1272 | admixed 1273 | admixes 1274 | admixing 1275 | admixt 1276 | admixture 1277 | admixtures 1278 | admonish 1279 | admonished 1280 | admonishes 1281 | admonishing 1282 | adnate 1283 | adnation 1284 | adnations 1285 | adnexa 1286 | adnexal 1287 | adnoun 1288 | adnouns 1289 | ado 1290 | adobe 1291 | adobes 1292 | adolescence 1293 | adolescences 1294 | adolescent 1295 | adolescents 1296 | adopt 1297 | adopted 1298 | adoptee 1299 | adoptees 1300 | adopter 1301 | adopters 1302 | adopting 1303 | adoption 1304 | adoptions 1305 | adoptive 1306 | adopts 1307 | adorable 1308 | adorably 1309 | adoration 1310 | adorations 1311 | adore 1312 | adored 1313 | adorer 1314 | adorers 1315 | adores 1316 | adoring 1317 | adorn 1318 | adorned 1319 | adorner 1320 | adorners 1321 | adorning 1322 | adorns 1323 | ados 1324 | adown 1325 | adoze 1326 | adrenal 1327 | adrenals 1328 | adriamycin 1329 | adrift 1330 | adroit 1331 | adroiter 1332 | adroitest 1333 | adroitly 1334 | adroitness 1335 | adroitnesses 1336 | ads 1337 | adscript 1338 | adscripts 1339 | adsorb 1340 | adsorbed 1341 | adsorbing 1342 | adsorbs 1343 | adularia 1344 | adularias 1345 | adulate 1346 | adulated 1347 | adulates 1348 | adulating 1349 | adulator 1350 | adulators 1351 | adult 1352 | adulterate 1353 | adulterated 1354 | adulterates 1355 | adulterating 1356 | adulteration 1357 | adulterations 1358 | adulterer 1359 | adulterers 1360 | adulteress 1361 | adulteresses 1362 | adulteries 1363 | adulterous 1364 | adultery 1365 | adulthood 1366 | adulthoods 1367 | adultly 1368 | adults 1369 | adumbral 1370 | adunc 1371 | aduncate 1372 | aduncous 1373 | adust 1374 | advance 1375 | advanced 1376 | advancement 1377 | advancements 1378 | advancer 1379 | advancers 1380 | advances 1381 | advancing 1382 | advantage 1383 | advantageous 1384 | advantageously 1385 | advantages 1386 | advent 1387 | adventitious 1388 | adventitiously 1389 | adventitiousness 1390 | adventitiousnesses 1391 | advents 1392 | adventure 1393 | adventurer 1394 | adventurers 1395 | adventures 1396 | adventuresome 1397 | adventurous 1398 | adverb 1399 | adverbially 1400 | adverbs 1401 | adversaries 1402 | adversary 1403 | adverse 1404 | adversity 1405 | advert 1406 | adverted 1407 | adverting 1408 | advertise 1409 | advertised 1410 | advertisement 1411 | advertisements 1412 | advertiser 1413 | advertisers 1414 | advertises 1415 | advertising 1416 | advertisings 1417 | adverts 1418 | advice 1419 | advices 1420 | advisabilities 1421 | advisability 1422 | advisable 1423 | advise 1424 | advised 1425 | advisee 1426 | advisees 1427 | advisement 1428 | advisements 1429 | adviser 1430 | advisers 1431 | advises 1432 | advising 1433 | advisor 1434 | advisories 1435 | advisors 1436 | advisory 1437 | advocacies 1438 | advocacy 1439 | advocate 1440 | advocated 1441 | advocates 1442 | advocating 1443 | advowson 1444 | advowsons 1445 | adynamia 1446 | adynamias 1447 | adynamic 1448 | adyta 1449 | adytum 1450 | adz 1451 | adze 1452 | adzes 1453 | cold 1454 | schooled 1455 | shoe 1456 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | This folder contains solutions to almost all end-of-chapter exercises of Allen Downey's Think Python: How to Think Like a Computer Scientist. The solutions refer to the exercises in version 2.0.9 (May 2013) of the book. 2 | 3 | Exercises involving "Turtle World" (ch. 4), GUI programming (ch. 19), and OOP (ch. 15 - 18) were either partly or completely skipped. 4 | 5 | The folder /extra contains a number of supporting files: 6 | 7 | - Allen Downey: Think Python v.2.0.9 (PDF) 8 | - polygon.py, which is required for some exercises 9 | - pg1661.txt, a book from Project Gutenberg, used in some exercises 10 | - words.txt, a list of 100k+ words, used in some exercises 11 | - words2.txt, a small subset of words.txt 12 | - c06d.txt, the Carnegie Mellon Pronouncing Dictionary, used in some exercises 13 | - swampy-2.1.1, a library that is required for Turtle World exercises 14 | 15 | All .py files were written in Python 2.7. --------------------------------------------------------------------------------