Skip to content

Commit a7aecce

Browse files
mindmerocs
authored andcommitted
small changes (YearOfProgramming#313)
1 parent ba58f37 commit a7aecce

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

challenge_7/python/mindm/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Challenge_7
2+
## Missing Number
3+
This method will find the missing number in O(1) space and O(n) time!
4+
Input is an unordered list from 0..n with 1 value missing.
5+
6+
You can run tests with:
7+
```
8+
$ python3 test.py
9+
```
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
5+
def find_missing(input_list):
6+
""" Find the missing number in shuffled list
7+
"""
8+
9+
# Mathematical formula for finding the sum of consequtive natural numbers
10+
# (N*(N+1))/2
11+
total = (len(input_list) * (len(input_list) +1)) / 2
12+
13+
summed = 0
14+
15+
for element in input_list:
16+
summed += element
17+
18+
missing = total - summed
19+
20+
return missing

challenge_7/python/mindm/src/test.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
import unittest
4+
import random
5+
from missing import find_missing
6+
7+
8+
class TestDiff(unittest.TestCase):
9+
10+
def test_ordered(self):
11+
self.assertEqual(find_missing([0,1,2,3,4,5,7]), 6)
12+
13+
def test_100_range(self):
14+
big_range = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
15+
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
16+
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
17+
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 65,
18+
66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
19+
82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
20+
98, 99]
21+
random.shuffle(big_range)
22+
self.assertEqual(find_missing(big_range), 64)
23+
24+
def test_empty(self):
25+
self.assertEqual(find_missing([]), 0)
26+
27+
def test_minimal(self):
28+
self.assertEqual(find_missing([0]), 1)
29+
30+
def test_small_unordered(self):
31+
self.assertEqual(find_missing([3,0,1]), 2)
32+
33+
def test_huge_range_shuffled(self):
34+
huge_range = list(range(1000))
35+
random.shuffle(list(huge_range))
36+
missing = huge_range.pop(random.randint(0,999))
37+
self.assertEqual(find_missing(huge_range), missing)
38+
39+
40+
if __name__ == '__main__':
41+
unittest.main()

0 commit comments

Comments
 (0)