-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path82.py
More file actions
52 lines (50 loc) · 1.51 KB
/
82.py
File metadata and controls
52 lines (50 loc) · 1.51 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
# Write a Python program that takes multiple words as input from the user and calculates the frequency of each word. Finally, print all the words (sorted in ascending order) grouped by frequency (sorted in descending order).[You can not use built in .count() function]
# Sample Input #1: cow cat cow ant elephant cow dog lion cat goat cow lion tiger elephant
# Sample Output #2:
# {
# 4 : ['cow'],
# 2 : ['cat', 'elephant', 'lion'],
# 1 : ['ant', 'dog', 'goat', 'tiger']
# }
# Sample Input #2:
# cat dog lion cat elephant goat cow lion ant tiger elephant cow cow cow
# Sample Output #2:
# {
# 4 : ['cow'],
# 2 : ['cat', 'elephant', 'lion'],
# 1 : ['ant', 'dog', 'goat', 'tiger']
# }
#Approach no 1:
y = 'cow', 'cat', 'cow', 'ant', 'elephant', 'cow', 'dog', 'lion', 'cat', 'goat', 'cow', 'lion', 'tiger', 'elephant'
lst = []
dct = {}
for i in range(len(y)):
lst.append(y[i])
for j in lst:
val = lst.count(j)
if val not in dct.keys():
dct[val] = [j]
else:
if j in dct[val]:
pass
else:
dct[val].append(j)
print(dct)
#Approach no 2:
y = 'cow', 'cat', 'cow', 'ant', 'elephant', 'cow', 'dog', 'lion', 'cat', 'goat', 'cow', 'lion', 'tiger', 'elephant'
lst = []
dct = {}
dct2 = {}
for i in range(len(y)):
lst.append(y[i])
for j in lst:
val = lst.count(j)
if j not in dct.keys():
dct[j] = [val]
for m, n in dct.items():
for x in n:
if x not in dct2.keys():
dct2[x] = [m]
else:
dct2[x].append(m)
print(dct2)