-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex11.py~
189 lines (149 loc) · 3.45 KB
/
ex11.py~
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#Dictionary excercises
#ex11.1
def word_dict():
fin = open("/Users/Paul/Documents/words.txt","r")
words = dict()
for lines in fin:
word = lines.strip()
words[word] = ''
return words
#ex11.2
def histogram(s):
d = dict()
for c in s:
d[c] = 1 + d.get(c, 0)
return d
dic1 = histogram('tycoon')
#ex11.2
def print_hist(h):
ks = h.keys()
ks.sort()
for c in ks:
print c, h[c]
print_hist(dic1)
#ex11.3
def reverse_lookup(d, v):
res = []
for k in d:
if d[k] == v:
res.append(k)
if res == []:
raise ValueError
else:
return res
#ex11.4
def invert_dict(d):
inverse = dict()
for key, val in d.iteritems():
setdefault(val, []).append(key)
return inverse
#ex11.7
cache = {}
def ack (m,n):
if m == 0:
return n + 1
if n == 0:
return ack(m-1,1)
try:
return cache[m, n]
except KeyError:
cache[m, n] = ack(m-1, ack(m, n-1))
return cache[m, n]
#ex11.8
"""
weak form rsa takes input as number and exports number
keys gen could use revising
"""
import random
import fractions
"""
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
return None # modular inverse does not exist
else:
return x % m
def key_gen(p,q):
n = p*q
n2 = (p-1)*(q-1)
while True:
e = random.randint(1,n2)
if fractions.gcd(n2,e) == 1:
break
d = modinv(e, n2)
return (long(e), long(d), long(n))
(e, d, n) = key_gen(4457, 5783)
def rsaencrypt(plain):
cipher = pow(plain,e, n)
return cipher
def rsadecrypt(cipher):
plain = pow(cipher, d, n)
return plain
"""
#ex11.9
"""
def has_duplicates2(xs):
d = dict()
for c in xs:
if c in d:
return True
else:
d[c] = ''
return False
#ex11.10
def uconvert(n,m):
n = ((n-97)+m)%26 + 97
return n
def rotate_word(strings, n):
strings = strings.lower()
cipher = ''
for i in strings:
ntext = ord(i)
ctext = uconvert(ntext, n)
cipher += chr(ctext)
return str(cipher)
def make_word_dict():
d = dict()
fin = open("/Users/Paul/Documents/words.txt","r")
for line in fin:
word = line.strip().lower()
d[word] = word
return d
def rotate_pair(word, word_dict):
for i in range(1,14):
rotated = rotate_word(word, i)
if rotated in word_dict:
print word, i, rotated
word_dict = make_word_dict()
for word in word_dict:
rotate_pair(word, word_dict)
"""
#ex11.11
word_dict=word_dict()
def read_dictionary(filename='/Users/Paul/Documents/c06d.txt'):
"""read (filename) and build a dictionary that maps from
each word to a string that describes its primary pronunciation.
Secondary pronunciations are added to the dictionary with
a number, in parentheses, at the end of the key, so the
key for the second pronunciation of "abdominal" is "abdominal(2)".
"""
d = dict()
fin = open(filename)
for line in fin:
# skip over the comments
if line[0] == '#': continue
t = line.split()
word = t[0].lower()
pron = ' '.join(t[1:])
d[word] = pron
return d
def fomo(words):
for i in d:
print i
print d[i]