Skip to content

Commit

Permalink
add tests and another approach for find_missing_number.py
Browse files Browse the repository at this point in the history
  • Loading branch information
goswami-rahul committed Apr 2, 2018
1 parent 1f6e24c commit e73e7df
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Thanks for your interest in contributing! There are many ways to contribute to t
- [single_number2](bit/single_number2.py)
- [single_number](bit/single_number.py)
- [subsets](bit/subsets.py)
- [add_without_operator](bit/add_without_operator.py)
- [add_bitwise_operator](bit/add_without_operator.py)
- [calculator](calculator)
- [math_parser](calculator/math_parser.py)
- [dfs](dfs)
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ python版数据结构和算法实现的简约版小示例
- [single_number2:寻找出现1次的数(2)](bit/single_number2.py)
- [single_number:寻找出现1次的数(1)](bit/single_number.py)
- [subsets: 求所有子集](bit/subsets.py)
- [add_without_operator:无操作符的加法](bit/add_without_operator.py)
- [add_bitwise_operator:无操作符的加法](bit/add_without_operator.py)
- [calculator:计算](calculator)
- [math_parser: 数字解析](calculator/math_parser.py)
- [dfs:深度优先搜索](dfs)
Expand Down
19 changes: 13 additions & 6 deletions bit/add_without_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@
Input: 2 3
Output: 5
"""
import unittest


def addWithoutOperator(x, y):
def add_bitwise_operator(x, y):

while y != 0:
carry = x & y
x = x ^ y
y = carry << 1
print(x)
return x


class TestSuite(unittest.TestCase):

def test_add_bitwise_operator(self):

def main():
x, y = map(int, input('Enter two positive integers: ').split())
addWithoutOperator(x, y)
self.assertEqual(5432 + 97823, add_bitwise_operator(5432, 97823))
self.assertEqual(0, add_bitwise_operator(0, 0))
self.assertEqual(10, add_bitwise_operator(10, 0))
self.assertEqual(10, add_bitwise_operator(0, 10))


if __name__ == '__main__':
main()
unittest.main()
50 changes: 46 additions & 4 deletions bit/count_ones.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,58 @@
Number of loops is
equal to the number of 1s in the binary representation."""
import unittest

def count_ones(n):

def count_ones_recur(n):
"""Using Brian Kernighan’s Algorithm. (Recursive Approach)"""
if not n: return 0
return 1 + count_ones(n & (n-1))

def count_ones(n):
if not n:
return 0
return 1 + count_ones_recur(n & (n-1))


def count_ones_iter(n):
"""Using Brian Kernighan’s Algorithm. (Iterative Approach)"""

count = 0
while n:
n &= (n-1)
count += 1
return count


class TestSuite(unittest.TestCase):

def test_count_ones_recur(self):

# 8 -> 1000
self.assertEqual(1, count_ones_recur(8))

# 109 -> 1101101
self.assertEqual(5, count_ones_recur(109))

# 63 -> 111111
self.assertEqual(6, count_ones_recur(63))

# 0 -> 0
self.assertEqual(0, count_ones_recur(0))

def test_count_ones_iter(self):

# 8 -> 1000
self.assertEqual(1, count_ones_iter(8))

# 109 -> 1101101
self.assertEqual(5, count_ones_iter(109))

# 63 -> 111111
self.assertEqual(6, count_ones_iter(63))

# 0 -> 0
self.assertEqual(0, count_ones_iter(0))


if __name__ == '__main__':

unittest.main()
55 changes: 48 additions & 7 deletions bit/find_missing_number.py
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()
2 changes: 1 addition & 1 deletion tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
│   ├── shortest_distance_from_all_buildings.py
│   └── word_ladder.py
├── bit
│   ├── add_without_operator.py
│   ├── add_bitwise_operator.py
│   ├── bytes_int_conversion.py
│   ├── count_ones.py
│   ├── find_missing_number.py
Expand Down

0 comments on commit e73e7df

Please sign in to comment.