-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstEx.py
More file actions
121 lines (83 loc) · 2.25 KB
/
FirstEx.py
File metadata and controls
121 lines (83 loc) · 2.25 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
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
# 1 exercise
# def reverse_list(lst):
# if len(lst)<=1:
# return lst
# return reverse_list(lst[1:]) + [lst[0]]
# lst = [1,2,3,4,5]
# print(reverse_list(lst))
#2 exercise
# grades = {
# "Alice": [85, 90, 78],
# "Bob": [72, 88, 91],
# "Charlie": [90, 92, 85]
# }
# avg = {}
# for name in grades:
# scores = grades[name]
# average = sum(scores) / 3
# avg[name] = round(average,2)
# print(avg)
# list = []
# for names in avg:
# list.append(int(avg[names]))
# print(max(list))
#3 exercise
# listof = {}
# lst = [5,1,7,8]
# def count_elements(lst):
# for i in range(0,len(lst)):
# listof[f'{i}'] = lst[i]
# count_elements(lst)
# print(listof)
#6 exercise
# def prime_factors(n):
# factors = []
# d = 2
# while n > 1:
# while n % d == 0:
# factors.append(d)
# n //= d
# d += 1
# return factors
#7 exericse
# def remove_duplicates_preserve_order(lst):
# seen_objects = set()
# result = []
# for item in lst:
# if item not in seen_objects:
# seen_objects.add(item)
# result.append(item)
# return result
# nums = [3, 1, 2, 3, 2, 4, 1, 5]
# print(remove_duplicates_preserve_order(nums))
#5 exercise
# def is_palindrome(word):
# word = list(word)
# if len(word)<=1:
# return word
# return is_palindrome(word[1:]) + [word[0]]
# thestring = "faaf"
# print(is_palindrome(thestring)==list(thestring))
#4 exercise
def word_statistics(text):
for ch in [".", "!", "?", ","]:
text = text.replace(ch, "")
words = text.lower().split()
#turn the sentence to wordss
word_freq = {}
for i in words:
word_freq[i] = word_freq.get(i, 0) + 1
#counting word frequencies
un_words = []
for j in word_freq:
un_words.append(j)
un_words = tuple(un_words)
unwordscount = len(un_words)
#tuple with unique words
sortedlist = sorted(word_freq.items(), key=lambda item: item[1], reverse=True)
top3 = []
for i in range(3):
top3.append(sortedlist[i][0])
print(unwordscount, word_freq, top3)
text = "Data is the new oil. Data drives the world. Oil is limited."
word_statistics(text)