-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtop_k_frequent_elements.py
More file actions
52 lines (40 loc) · 1.46 KB
/
top_k_frequent_elements.py
File metadata and controls
52 lines (40 loc) · 1.46 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
# https://leetcode.com/problems/top-k-frequent-elements/
# Given a non-empty array of integers, return the k most frequent elements.
#
# For example,
# Given [1,1,1,2,2,3] and k = 2, return [1,2].
#
#
# I m guessing I might be using too many inbuild functions
# In a way ,thats the power of python - but I'll come back and implement some of the inbuilt functions
# used here
from collections import Counter
import unittest
class Solution(object):
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
return_list = []
my_list = Counter(nums).most_common(k)
for i in xrange(k):
return_list.append(my_list[i][0])
return return_list
class TestTopkFrequent(unittest.TestCase):
my_solution = Solution()
def test_k_equals_0(self):
nums = [2,2,2,2,1,1,1,1,1,1,1,1,1,1,3,3]
self.assertEquals(self.my_solution.topKFrequent(nums,0),[])
def test_k_equals_1(self):
nums = [2,2,2,2,1,1,1,1,1,1,1,1,1,1,3,3]
self.assertEquals(self.my_solution.topKFrequent(nums,1),[1])
def test_k_equals_2(self):
nums = [2,2,2,2,1,1,1,1,1,1,1,1,1,1,3,3]
self.assertEquals(self.my_solution.topKFrequent(nums,2),[1,2])
def test_k_equals_3(self):
nums = [2,2,2,2,1,1,1,1,1,1,1,1,1,1,3,3]
self.assertEquals(self.my_solution.topKFrequent(nums,3),[1,2,3])
if __name__ == "__main__":
unittest.main()