-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-three-words.py
50 lines (38 loc) · 1.2 KB
/
find-three-words.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
def find_three_words(text: str):
def symbols_of_ascii():
'''
Return list of ASCII characters in the ranges
33 - 47 | 58 - 64 | 91 - 96 | 123 - 126
'.', ',', ':', '!', '"', "'", '[', ']', '-', '(', ')', etc.
'''
list = []
list.extend([chr(i) for i in range(33, 48)])
list.extend([chr(i) for i in range(58, 65)])
list.extend([chr(i) for i in range(91, 97)])
list.extend([chr(i) for i in range(123, 127)])
return list
lst_no = symbols_of_ascii()
lst = []
for word in text.lower().split():
if not word in lst_no:
_word = word
if word[-1] in lst_no:
_word = _word[:-1]
if word[0] in lst_no:
_word = _word[1:]
lst.append(_word)
if len(lst) < 3:
return []
_dict = dict()
for word in lst:
_dict[word] = _dict.get(word, 0) + 1
_list = []
for key, value in _dict.items():
_list.append((value, key))
_list.sort(reverse=True)
result = []
for freq, word in _list[0:3]:
result.append(word)
return result
text = input('Type your text: ')
print(find_three_words(text))