-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_12.py
140 lines (97 loc) · 2.71 KB
/
exercise_12.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
from binary_search import make_word_list
from random import random
from structshape import structshape
from exercise_11 import invert_dict
def in_both_at_same_place(word1, word2):
i = 0
count = 0
while i < len(word1):
if word1[i] == word2[i]:
count += 1
i += 1
if count == len(word1)-2:
return True
return False
def sumall(*args):
print(sum(args))
def has_match(t1, t2):
for x, y in zip(t1, t2):
if x == y:
return True
return False
def enum_erate(t):
for i, e in enumerate(t):
print(i, e)
def sort_by_length(words):
t = []
for word in words:
if len(word) > 0:
t.append((len(word), random(), word))
t.sort(reverse=True)
res = []
for length, _, word in t:
res.append(word)
return res
def most_frequent(wl1):
d = {}
for s in wl1:
for letter in s:
d[letter] = d.get(letter, 0) + 1
res = []
for letter, freq in d.items():
res.append((freq, letter))
res.sort(reverse=True)
return res
def get_anagrams(wl2):
d = {}
for s in wl2:
if len(s) > 1:
d[s] = tuple(sorted(list(s)))
dic = invert_dict(d)
res = []
for x in dic.values():
if len(x) > 1:
res.append(x)
return res
word_list = make_word_list()
list_of_anagrams = get_anagrams(word_list)
list_anagrams_by_decr_length = sort_by_length(list_of_anagrams)
'''
# Sum function with more than 2 arguments
sumall(1, 2, 3, 4, 5, 34, 34, 54, 56, 68, 79)
# Zip built-in function takes any number of arguments
print(list(zip([1, 2, 3], 'abc', 'sbc', 'lkj', '123', (4, 5, 6))))
# Traversing two sequences at once using zip, for loop and tuple assignment
print(has_match([1, 2, 3], [5, 4, 3]))
# Enumerate function and tuple assignment for traversing
enum_erate(['dfhsd', 'fbhasbfh', 'gbjsd', 'bsfj'])
# DSU
print(sort_by_length(word_list)[:10])
print(structshape(sort_by_length(word_list)))
# Frequencies of letters - most_frequent
print(most_frequent(word_list))
# Anagrams in a list, and anagrams in decreasing order of possible words
word_list = make_word_list()
list_of_anagrams = get_anagrams(word_list)
list_anagrams_by_decr_length = sort_by_length(list_of_anagrams)
print(list_anagrams_by_decr_length)
# Bingo(scrabbles)
t = []
for x in list_anagrams_by_decr_length:
for y in x:
if len(y) == 8:
t.append(x)
break
for i in t:
if len(i) == 7:
print(i)
# Metathesis pairs - from anagrams
t = []
for x in list_anagrams_by_decr_length:
i = 0
while i < len(x)-1:
if in_both_at_same_place(x[i], x[i+1]):
t.append([x[i], x[i+1]])
i += 1
print(t)
'''