-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_countingfrequeny.py
More file actions
36 lines (29 loc) · 1.01 KB
/
Copy path3_countingfrequeny.py
File metadata and controls
36 lines (29 loc) · 1.01 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
# Counting Frequency
"""
Given an unsorted list of some elements(may or may not be integers), Find the frequency of each
distinct element in the list using a dictionary.
Example:
Input : [1, 1, 1, 5, 5, 3, 1, 3, 3, 1,4, 4, 4, 2, 2, 2, 2]
Output :{ 1 : 5,2 : 4,3 : 3, 4 : 3, 5 : 2}
Explanation : Here 1 occurs 5 times, 2 occurs 4 times and so on...
"""
import unittest
def counting_frequency(lst):
lst.sort()
d1={}
for i in lst:
if (i not in d1):
frequency=lst.count(i)
d1[i]=frequency
return d1
class TestCountingFrequency(unittest.TestCase):
def test_1(self):
lst = [1, 1, 1, 5, 5, 3, 1, 3, 3, 1, 4, 4, 4, 2, 2, 2, 2]
d = {1: 5, 2: 4, 3: 3, 4: 3, 5: 2}
self.assertEqual(counting_frequency(lst), d)
def test_2(self):
lst = ['a', 'b', 'c', 'a', 'b']
d = {'a': 2, 'b': 2, 'c': 1}
self.assertEqual(counting_frequency(lst), d)
if __name__ == '__main__':
unittest.main(verbosity=2)