-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdict_fns.py~
More file actions
100 lines (87 loc) · 2.71 KB
/
Copy pathdict_fns.py~
File metadata and controls
100 lines (87 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from list_fns import load_words_append as load_words_list
from inlist import in_bisect as list_search
from str_fns import rotate
import time
# memoized Ackermann function of two positive integers
# quickly exceeds recursion depth limits (whenever m > 3, e.g.)!
# could be improved. see: https://stackoverflow.com/questions/13086568/memoization-of-ackermann-function
known = {}
def ack_memo(m, n):
if (m, n) in known:
pass
elif m == 0:
known[m, n] = n + 1
elif n == 0:
known[m, n] = ack_memo(m - 1, 1)
else:
known[m, n] = ack_memo(m - 1, ack_memo(m, n - 1))
return known[m, n]
# precondition, given list t is sorted
def compare_searches(t, d):
target = input("Give me a word ('get me out!' to quit): ")
while target != 'get me out!':
t0l = time.time()
result_list = list_search(t, target)
t1l = time.time()
t0d = time.time()
result_dict = target in d
t1d = time.time()
if result_list:
print(f"{target} found in list, ", end='')
else:
print(f"{target} not found in list, ", end='')
print(f"binary search time {t1l - t0l:.10f} secs")
if result_dict:
print(f"{target} found in dict, ", end='')
else:
print(f"{target} not found in dict, ", end='')
print(f"dict search time {t1d - t0d:.10f} secs")
target = input("Give me a word ('get me out!' to quit): ")
print("Have a good day!")
def print_rotations(word_dict):
for word in word_dict:
for i in range(1, 14):
rotated = rotate(word, i)
if rotated in word_dict:
print(f"{word} rotated {i} is {rotated}")
# checks a given list for duplicates, using a dict to do so
def has_duplicates(t):
d = dict()
for element in t:
if element in d:
return True
d[element] = 1
return False
def histogram(str):
d = dict()
for ch in str:
d[ch] = d.get(ch, 0) + 1
return d
def invert_dict(d):
inverse = dict()
for key in d:
val = d[key]
inverse.setdefault(val, key)
return inverse
def load_pronunciation_dict(filename='c07b.txt'):
fin = open(filename)
d = dict()
for line in fin:
if line[:3] == ';;;':
""" IMPORTANT: comment markers change over dict version;
this is for version 0.7b specifically.
"""
# line is a comment
continue
t = line.split()
word = t[0].lower()
pron = ' '.join(t[1:])
d[word] = pron
return d
def load_words_dict(filename):
fin = open(filename)
d = dict()
for word in fin:
word = word.strip()
d[word] = None
return d