forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_patterns.py
More file actions
Latest commit
44 lines (37 loc) · 1.27 KB
/
Copy pathword_patterns.py
File metadata and controls
44 lines (37 loc) · 1.27 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
defget_word_pattern(word: str) ->str:
"""
>>> get_word_pattern("pattern")
'0.1.2.2.3.4.5'
>>> get_word_pattern("word pattern")
'0.1.2.3.4.5.6.7.7.8.2.9'
>>> get_word_pattern("get word pattern")
'0.1.2.3.4.5.6.7.3.8.9.2.2.1.6.10'
"""
word=word.upper()
next_num=0
letter_nums= {}
word_pattern= []
forletterinword:
ifletternotinletter_nums:
letter_nums[letter] =str(next_num)
next_num+=1
word_pattern.append(letter_nums[letter])
return".".join(word_pattern)
if__name__=="__main__":
importpprint
importtime
start_time=time.time()
withopen("dictionary.txt") asin_file:
word_list=in_file.read().splitlines()
all_patterns: dict= {}
forwordinword_list:
pattern=get_word_pattern(word)
ifpatterninall_patterns:
all_patterns[pattern].append(word)
else:
all_patterns[pattern] = [word]
withopen("word_patterns.txt", "w") asout_file:
out_file.write(pprint.pformat(all_patterns))
total_time=round(time.time() -start_time, 2)
print(f"Done! {len(all_patterns):,} word patterns found in {total_time} seconds.")
# Done! 9,581 word patterns found in 0.58 seconds.