-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree_letters.py
80 lines (71 loc) · 2.59 KB
/
three_letters.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
import csv
from timeit import default_timer
alphabet = "אבגדהוזחטיכלמנסעפצקרשת"
def word_checker():
words_list = {}
with open("words_with_three_letters.csv", encoding="utf-8-sig") as words_file:
file = csv.reader(words_file)
for line in file:
words_list[line[0]] = int(line[1])
with open(
"first_solution.csv", "w", encoding="utf-8-sig", newline=""
) as letters_file:
writer = csv.writer(letters_file)
writer.writerow(["word", "count", "words", "score", "sum", "average"])
already_in = []
for a in alphabet:
for b in alphabet:
for c in alphabet:
count = 0
situation = {}
num = [
a + b + c,
a + c + b,
b + a + c,
b + c + a,
c + a + b,
c + b + a,
]
for n in num:
if n in words_list and n not in already_in:
count += 1
already_in.append(n)
situation[n] = words_list[n]
if count > 4:
writer.writerow(
[
a + b + c,
count,
", ".join([i for i in situation]),
", ".join([str(i) for i in situation.values()]),
sum(situation.values()),
(sum(situation.values())) // 6,
]
)
def second_solution():
words_list = []
words_count = {}
with open("words_with_three_letters.csv", encoding="utf-8-sig") as words_file:
file = csv.reader(words_file)
for line in file:
words_list.append("".join(sorted(list(line[0]))))
for word in words_list:
try:
words_count[word] += 1
except KeyError:
words_count[word] = 1
with open(
"second_solution.csv", "w", encoding="utf-8-sig", newline=""
) as letters_file:
writer = csv.writer(letters_file)
writer.writerow(["word", "count"])
for word in words_count:
writer.writerow([word, words_count[word]])
start = default_timer()
word_checker()
end = default_timer()
print(f"the time is {end-start}")
start = default_timer()
second_solution()
end = default_timer()
print(f"the time is {end-start}")