Created
October 21, 2015 08:46
-
-
Save dudepare/daf039b74056b0b364c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def find_mode(l): | |
if len(l) == 0: | |
return "ZeroLength" | |
l.sort() | |
sums = {} | |
for item in l: | |
if item in sums.keys(): | |
sums[item] += 1 | |
else: | |
sums[item] = 1 | |
max_count = 0 | |
max_item = l[0] | |
for k, v in sums.items(): | |
if v > max_count: | |
max_count = v | |
max_item = k | |
return max_item | |
class TestFindMode(unittest.TestCase): | |
def test_empty(self): | |
self.assertEqual(find_mode([]), "ZeroLength") | |
def test_equal(self): | |
self.assertEqual(find_mode([1, 2]), 1) | |
def test_positives(self): | |
self.assertEqual(find_mode([2, 3, 5, 5, 5, 2, 4, 1, 3, 1]), 5) | |
def test_negatives(self): | |
self.assertEqual(find_mode([-1, -3, -1, -1, -2, -4, -7, -1]), -1) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should run O(N log N).