forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests and another approach for find_missing_number.py
- Loading branch information
1 parent
1f6e24c
commit e73e7df
Showing
6 changed files
with
110 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,59 @@ | ||
def find_missing_number(nums): | ||
"""Returns the missing number from a sequence of unique integers | ||
""" | ||
Returns the missing number from a sequence of unique integers | ||
in range [0..n] in O(n) time and space. The difference between | ||
consecutive integers cannot be more than 1. If the sequence is | ||
already complete, the next integer in the sequence will be returned. | ||
""" | ||
import unittest | ||
import random | ||
|
||
|
||
>>> find_missing_number(i for i in range(0, 10000) if i != 1234) | ||
1234 | ||
>>> find_missing_number([4, 1, 3, 0, 6, 5, 2]) | ||
7 | ||
""" | ||
def find_missing_number(nums): | ||
|
||
missing = 0 | ||
for i, num in enumerate(nums): | ||
missing ^= num | ||
missing ^= i + 1 | ||
|
||
return missing | ||
|
||
|
||
def find_missing_number2(nums): | ||
|
||
num_sum = sum(nums) | ||
n = len(nums) | ||
total_sum = n*(n+1) // 2 | ||
missing = total_sum - num_sum | ||
return missing | ||
|
||
|
||
class TestSuite(unittest.TestCase): | ||
|
||
def setUp(self): | ||
"""Initialize seed.""" | ||
random.seed("test") | ||
|
||
def test_find_missing_number(self): | ||
|
||
self.assertEqual(7, find_missing_number([4, 1, 3, 0, 6, 5, 2])) | ||
self.assertEqual(0, find_missing_number([1])) | ||
self.assertEqual(1, find_missing_number([0])) | ||
|
||
nums = [i for i in range(100000) if i != 12345] | ||
random.shuffle(nums) | ||
self.assertEqual(12345, find_missing_number(nums)) | ||
|
||
def test_find_missing_number2(self): | ||
|
||
self.assertEqual(7, find_missing_number2([4, 1, 3, 0, 6, 5, 2])) | ||
self.assertEqual(0, find_missing_number2([1])) | ||
self.assertEqual(1, find_missing_number2([0])) | ||
|
||
nums = [i for i in range(100000) if i != 12345] | ||
random.shuffle(nums) | ||
self.assertEqual(12345, find_missing_number2(nums)) | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
unittest.main() |
This file contains 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