def test_small(self): for elements in [ #[7, 2, 7], #[7, 8, 9], #[2, 3, 2, 3], #[1, 2, 3, 4], [1, 1, 3, 3, 1, 3, 3] ]: hi = len(elements) - 1 lo = 0 self.assertEqual(get_majority_element(list(elements), hi, lo), majority_element_naive(elements))
def test_with_five_elements(self): self.assertEqual(3, get_majority_element([3, 3, 3, 3, 3])) self.assertEqual(3, get_majority_element([3, 3, 3, 3, 2])) self.assertEqual(3, get_majority_element([3, 3, 3, 1, 2])) self.assertEqual(None, get_majority_element([3, 3, 1, 2, 4])) self.assertEqual(None, get_majority_element([3, 1, 2, 4, 5])) self.assertEqual(2, get_majority_element([2, 3, 9, 2, 2]))
def test_with_four_elements(self): self.assertEqual(3, get_majority_element([3, 3, 3, 3])) self.assertEqual(3, get_majority_element([3, 3, 3, 2])) self.assertEqual(None, get_majority_element([3, 3, 1, 2])) self.assertEqual(None, get_majority_element([1, 2, 3, 4])) self.assertEqual(None, get_majority_element([1, 2, 3, 4])) self.assertEqual(None, get_majority_element([1, 2, 3, 1]))
from majority_element import get_majority_element, get_majority_element_naive from test.asserts import assert_equal arrays = [[2, 2, 2, 1], [2, 3, 9, 2, 2], [1, 2, 3, 4], [1, 2, 3, 1], [ 512766168, 717383758, 5, 126144732, 5, 573799007, 5, 5, 5, 405079772 ]] i = 1 for array in arrays: assert_equal(get_majority_element_naive(array), get_majority_element(array, 0, len(array) - 1), f"sample {i}") i += 1
def test_with_empty_data(self): self.assertEqual(None, get_majority_element([]))
def test_with_several_elements(self): self.assertEqual( 2, get_majority_element( [2, 124554847, 2, 941795895, 2, 2, 2, 2, 792755190, 756617003]))
def test_with_three_elements(self): self.assertEqual(3, get_majority_element([3, 3, 3])) self.assertEqual(3, get_majority_element([3, 3, 2]))
def test_with_two_elements(self): self.assertEqual(3, get_majority_element([3, 3])) self.assertEqual(None, get_majority_element([2, 3]))
def test_with_one_element(self): self.assertEqual(3, get_majority_element([3]))
def test_large(self): for (elements, answer) in [([0] * 5000 + [1] * 5000, 0)]: hi = len(elements) - 1 lo = 0 self.assertEqual(get_majority_element(elements, hi, lo), answer)