-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex12HW.py
201 lines (174 loc) · 5.44 KB
/
ex12HW.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
189
190
191
192
193
194
195
196
197
198
199
200
201
# Exercise 12.1. Many of the built-in functions use variable-length argument
# tuples. Write a function called sum all that takes any number of arguments
# and returns their sum.
def sum(*x):
a=0
for i in x:
a+=i
return a
# Exercise 12.2. In this example, ties are broken by comparing words, so words
# with the same length appear in reverse alphabetical order. For other
# applications you might want to break ties at random. Modify this example so
# that words with the same length appear in random order.
def sort_by_length(words):
t = []
for word in words:
t.append((len(word), word))
t.sort(reverse=True)
res = []
for length, word in t:
res.append(word)
return res
def sort_by_just_length(words):
t = []
for word in words:
t.append((len(word), word))
t.sort(key=lambda x:x[0], reverse=True)
res = []
for length, word in t:
res.append(word)
return res
################
def sort_by_length_rest_random(words):
t = []
for word in words:
t.append((len(word), word))
t.sort(reverse=True)
t=second_value_random(t)
res = []
for length, word in t:
res.append(word)
return res
def second_value_random(a):
import random
f=[]
t=a+[(0,0)]
temp=[]
for i in range (len(t)-1):
if t[i][0]==t[i+1][0]:
temp+=[t[i]]
else:
temp+=[t[i]]
random.shuffle(temp)
f+=temp
temp=[]
return f
# Exercise 12.3. Write a function called most_frequent that takes a string and
# prints the letters in decreasing order of frequency. Find text samples from
# several different languages and see how letter frequency varies between
# languages
def most_frequent(a):
d=dict()
for x in a:
if x == ' ':
x='space'
else: x=' '+x+' '
if x in d:
d[x]+=1
else:
d[x]=1
f=d.items()
f.sort(key=lambda x:x[1],reverse=True)
for i in range (len(f)):
print f[i][0],f[i][1]
# Exercise 12.4. More anagrams!
# 1. Write a program that reads a word list from a file (see Section 9.1) and
# prints all the sets of words that are anagrams.
# Here is an example of what the output might look like:
# ['deltas', 'desalt', 'lasted', 'salted', 'slated', 'staled']
# ['retainers', 'ternaries']
# ['generating', 'greatening']
# ['resmelts', 'smelters', 'termless']
# Hint: you might want to build a dictionary that maps from a set of letters to a# list of words that can be spelled with those letters. The question is, how can
# you represent the set of letters in a way that can be used as a key?
def all_anagrams(wordlist):
d=dict()
for word in wordlist:
a=list(word)
a.sort()
a=tuple(a)
d[a]=d.get(a,[])+[word]
for x in d:
if len(d[x])>2:
print d[x]
# 2. Modify the previous program so that it prints the largest set of anagrams
# first, followed by the second largest set, and so on.
def anagrams_sorted(wordlist,d):
'''make a clean d=dict() before running this prog'''
t=[]
for word in wordlist:
a=list(word)
a.sort()
a=tuple(a)
d[a]=d.get(a,[])+[word]
for x in d:
c= d.get(x)
t.append(c)
t.sort(key=len,reverse=True)
for i in t:
if len(i) >3:
print i
# 3. In Scrabble a 'bingo' is when you play all seven tiles in your rack, along
# with a letter on the board, to form an eight-letter word. What set of 8
# letters forms the most possible bingos?
def bingo(dic):
ls=[]
for x in dic:
if len(x)>7 and len(dic[x])>2:
ls+=[dic[x]]
ls.sort(key=len,reverse=True)
return ls[0]
# Exercise 12.5. Two words form a 'metathesis pair' if you can transform one into
# the other by swapping two letters; for example, 'converse' and 'conserve.'
# Write a program that finds all of the metathesis pairs in the dictionary.
def metathesis(d):
d2=dict()
for x in d:
if len(d[x])>1:
d2[x]=d[x]
temp=[]
for x in d2:
ls=d2[x]
for i in range(len(ls)):
for j in range(i+1,len(ls)):
if are_meta(ls[i],ls[j]):
temp.append((ls[i],ls[j]))
return temp
def are_meta(a,b):
n=0
for i in range(len(a)):
if a[i]==b[i]:
pass
else:
n+=1
if n>2:
return False
return True
# Exercise 12.6.
# What is the longest English word, that remains a valid English word, as you
# remove its letters one at a time? Now, letters can be removed from either end,
# or the middle, but you cant rearrange any of the letters. Every time you drop
# a letter, you wind up with another English word.
# Write a program to find all words that can be reduced in this way, and then
# find the longest one.
def two_words(a,ls,c):
twos=[]
for x in ls:
for y in a:
if y in x and len(x)==c:
twos.append(x)
return twos
# temp=[]
# for i in range (2,10):
# temp=ex12HW.two_words(temp,a,i)
# longest words: relapsers,scrapings, sheathers
def check(tenlist,ninelist):
b=[]
for word in tenlist:
shorten(word,ninelist,b)
return b
def shorten(word,ninelist,b):
for i in range (0,len(word)):
if word[:i]+word[i+1:] in ninelist:
b.append(word[:i]+word[i+1:])
# this is def not an elegant solution