-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeatedNum.py
63 lines (44 loc) · 1.38 KB
/
repeatedNum.py
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
53
54
55
56
57
58
59
60
61
62
63
import unittest
def approach1(the_list):
lsi = {}
for num in the_list:
if num in lsi:
return num
else:
lsi[num] = 1
return 0
def find_repeat1(the_list):
# Find a number that appears more than once
list.sort(the_list)
for i in range(0, len(the_list)-1,1):
if the_list[i] == the_list[i+1]:
return the_list[i]
return 0
def find_repeat(numbers_list):
#O(n) and O(1) space
# Find the number that appears twice
for i in range(0, len(numbers_list)):
if numbers_list[abs(numbers_list[i])] >= 0:
numbers_list[abs(numbers_list[i])] = - numbers_list[abs(numbers_list[i])]
else:
return abs(numbers_list[i])
return 0
# Tests
class Test(unittest.TestCase):
def test_just_the_repeated_number(self):
actual = find_repeat([1, 1])
expected = 1
self.assertEqual(actual, expected)
def test_short_list(self):
actual = find_repeat([1, 2, 3, 2])
expected = 2
self.assertEqual(actual, expected)
def test_medium_list(self):
actual = find_repeat([1, 2, 5, 5, 5, 5])
expected = 5
self.assertEqual(actual, expected)
def test_long_list(self):
actual = find_repeat([4, 1, 4, 8, 3, 2, 7, 6, 5])
expected = 4
self.assertEqual(actual, expected)
unittest.main(verbosity=2)