Skip to content

Commit b905623

Browse files
goswami-rahulHai Hoang Dang
authored and
Hai Hoang Dang
committed
add some bit problems (#294)
* add arrays/max_ones_index * add 2 algorithms to algorithms/bit * update all READMEs
1 parent b39f4e5 commit b905623

File tree

11 files changed

+166
-23
lines changed

11 files changed

+166
-23
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
English | [简体中文](https://github.com/yunshuipiao/algorithms/blob/master/README_CN.md) | [Deutsch](README_GE.md) | [日本語](README_JP.md)
1+
English | [简体中文](README_CN.md) | [Deutsch](README_GE.md) | [日本語](README_JP.md)
22

33
[![Open Source Helpers](https://www.codetriage.com/keon/algorithms/badges/users.svg)](https://www.codetriage.com/keon/algorithms)
44
[![Build Status](https://travis-ci.org/keon/algorithms.svg?branch=master)](https://travis-ci.org/keon/algorithms)
@@ -58,6 +58,7 @@ If you want to uninstall algorithms, it is as simple as:
5858
- [garage](algorithms/arrays/garage.py)
5959
- [josephus_problem](algorithms/arrays/josephus_problem.py)
6060
- [longest_non_repeat](algorithms/arrays/longest_non_repeat.py/)
61+
- [max_ones_index](algorithms/arrays/max_ones_index.py)
6162
- [merge_intervals](algorithms/arrays/merge_intervals.py)
6263
- [missing_ranges](algorithms/arrays/missing_ranges.py)
6364
- [plus_one](algorithms/arrays/plus_one.py)
@@ -86,19 +87,21 @@ If you want to uninstall algorithms, it is as simple as:
8687
- [shortest_distance_from_all_buildings](algorithms/bfs/shortest_distance_from_all_buildings.py)
8788
- [word_ladder](algorithms/bfs/word_ladder.py)
8889
- [bit](algorithms/bit)
90+
- [add_bitwise_operator](algorithms/bit/add_bitwise_operator.py)
91+
- [bit_operation](algorithms/bit/bit_operation.py)
8992
- [bytes_int_conversion](algorithms/bit/bytes_int_conversion.py)
93+
- [count_flips_to_convert](algorithms/bit/count_flips_to_convert.py)
9094
- [count_ones](algorithms/bit/count_ones.py)
95+
- [find_difference](algorithms/bit/find_difference.py)
9196
- [find_missing_number](algorithms/bit/find_missing_number.py)
97+
- [flip_bit_longest_sequence](algorithms/bit/flip_bit_longest_sequence.py)
9298
- [power_of_two](algorithms/bit/power_of_two.py)
9399
- [reverse_bits](algorithms/bit/reverse_bits.py)
94100
- [single_number](algorithms/bit/single_number.py)
95101
- [single_number2](algorithms/bit/single_number2.py)
96102
- [single_number3](algorithms/bit/single_number3.py)
97103
- [subsets](algorithms/bit/subsets.py)
98-
- [add_bitwise_operator](algorithms/bit/add_bitwise_operator.py)
99-
- [bit_operation](algorithms/bit/bit_operation.py)
100104
- [swap_pair](algorithms/bit/swap_pair.py)
101-
- [find_difference](algorithms/bit/find_difference.py)
102105
- [has_alternative_bit](algorithms/bit/has_alternative_bit.py)
103106
- [insert_bit](algorithms/bit/insert_bit.py)
104107
- [remove_bit](algorithms/bit/remove_bit.py)

README_CN.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[English](https://github.com/yunshuipiao/algorithms/blob/master/README.md) | 简体中文 | [日本語](README_JP.md)
1+
[English](README.md) | 简体中文 | [Deutsch](README_GE.md) | [日本語](README_JP.md)
22

33
Python版数据结构和算法
44
=========================================
@@ -70,7 +70,8 @@ pip3 uninstall -y algorithms
7070
- [flatten:数组降维](algorithms/arrays/flatten.py)
7171
- [garage:停车场](algorithms/arrays/garage.py)
7272
- [josephus_problem: 约瑟夫问题](algorithms/arrays/josephus_problem.py)
73-
- [longest_non_repeat:最长不重复子串](algorithms/arrays/longest_non_repeat.py/)
73+
- [max_ones_index](algorithms/arrays/max_ones_index.py)
74+
- [longest_non_repeat:最长不重复子串](algorithms/arrays/longest_non_repeat.py/)
7475
- [merge_intervals:合并重叠间隔](algorithms/arrays/merge_intervals.py)
7576
- [missing_ranges:遗失的范围](algorithms/arrays/missing_ranges.py)
7677
- [plus_one:加一运算](algorithms/arrays/plus_one.py)
@@ -101,7 +102,9 @@ pip3 uninstall -y algorithms
101102
- [bit:位操作](algorithms/bit)
102103
- [bytes_int_conversion:字节整数转换](algorithms/bit/bytes_int_conversion.py)
103104
- [count_ones:统计1出现的次数](algorithms/bit/count_ones.py)
105+
- [count_flips_to_convert](algorithms/bit/count_flips_to_convert.py)
104106
- [find_missing_number:寻找缺失数](algorithms/bit/find_missing_number.py)
107+
- [flip_bit_longest_sequence](algorithms/bit/flip_bit_longest_sequence.py)
105108
- [power_of_two:2的n次方数判断](algorithms/bit/power_of_two.py)
106109
- [reverse_bits:反转位](algorithms/bit/reverse_bits.py)
107110
- [single_number2:寻找出现1次的数(2)](algorithms/bit/single_number2.py)

README_GE.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[English](README.md) | [简体中文](https://github.com/yunshuipiao/algorithms/blob/master/README_CN.md) | Deutsch | [日本語](README_JP.md)
1+
[English](README.md) | [简体中文](README_CN.md) | Deutsch | [日本語](README_JP.md)
22

33
[![Open Source Helpers](https://www.codetriage.com/keon/algorithms/badges/users.svg)](https://www.codetriage.com/keon/algorithms)
44
[![Build Status](https://travis-ci.org/keon/algorithms.svg?branch=master)](https://travis-ci.org/keon/algorithms)
@@ -65,6 +65,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
6565
- [garage](algorithms/arrays/garage.py)
6666
- [josephus_problem](algorithms/arrays/josephus_problem.py)
6767
- [longest_non_repeat](algorithms/arrays/longest_non_repeat.py/)
68+
- [max_ones_index](algorithms/arrays/max_ones_index.py)
6869
- [merge_intervals](algorithms/arrays/merge_intervals.py)
6970
- [missing_ranges](algorithms/arrays/missing_ranges.py)
7071
- [plus_one](algorithms/arrays/plus_one.py)
@@ -95,7 +96,9 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
9596
- [bit](algorithms/bit)
9697
- [bytes_int_conversion](algorithms/bit/bytes_int_conversion.py)
9798
- [count_ones](algorithms/bit/count_ones.py)
99+
- [count_flips_to_convert](algorithms/bit/count_flips_to_convert.py)
98100
- [find_missing_number](algorithms/bit/find_missing_number.py)
101+
- [flip_bit_longest_sequence](algorithms/bit/flip_bit_longest_sequence.py)
99102
- [power_of_two](algorithms/bit/power_of_two.py)
100103
- [reverse_bits](algorithms/bit/reverse_bits.py)
101104
- [single_number](algorithms/bit/single_number.py)

README_JP.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ if __name__ == "__main__":
5454
- [flatten](arrays/flatten.py)
5555
- [garage](arrays/garage.py)
5656
- [josephus_problem](arrays/josephus_problem.py)
57+
- [max_ones_index](algorithms/arrays/max_ones_index.py)
5758
- [longest_non_repeat](arrays/longest_non_repeat.py/)
5859
- [merge_intervals](arrays/merge_intervals.py)
5960
- [missing_ranges](arrays/missing_ranges.py)
@@ -85,7 +86,9 @@ if __name__ == "__main__":
8586
- [bit : ビット](bit)
8687
- [bytes_int_conversion](bit/bytes_int_conversion.py)
8788
- [count_ones](bit/count_ones.py)
89+
- [count_flips_to_convert](algorithms/bit/count_flips_to_convert.py)
8890
- [find_missing_number](bit/find_missing_number.py)
91+
- [flip_bit_longest_sequence](algorithms/bit/flip_bit_longest_sequence.py)
8992
- [power_of_two](bit/power_of_two.py)
9093
- [reverse_bits](bit/reverse_bits.py)
9194
- [single_number](bit/single_number.py)

algorithms/arrays/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .garage import *
44
from .josephus import *
55
from .longest_non_repeat import *
6+
from .max_ones_index import *
67
from .merge_intervals import *
78
from .missing_ranges import *
89
from .move_zeros import *

algorithms/arrays/max_ones_index.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Find the index of 0 to be replaced with 1 to get
3+
longest continuous sequence
4+
of 1s in a binary array.
5+
Returns index of 0 to be
6+
replaced with 1 to get longest
7+
continuous sequence of 1s.
8+
If there is no 0 in array, then
9+
it returns -1.
10+
11+
e.g.
12+
let input array = [1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1]
13+
If we replace 0 at index 3 with 1, we get the longest continuous
14+
sequence of 1s in the array.
15+
So the function return => 3
16+
"""
17+
18+
19+
def max_ones_index(arr):
20+
21+
n = len(arr)
22+
max_count = 0
23+
max_index = 0
24+
prev_zero = -1
25+
prev_prev_zero = -1
26+
27+
for curr in range(n):
28+
29+
# If current element is 0,
30+
# then calculate the difference
31+
# between curr and prev_prev_zero
32+
if arr[curr] == 0:
33+
if curr - prev_prev_zero > max_count:
34+
max_count = curr - prev_prev_zero
35+
max_index = prev_zero
36+
37+
prev_prev_zero = prev_zero
38+
prev_zero = curr
39+
40+
if n - prev_prev_zero > max_count:
41+
max_index = prev_zero
42+
43+
return max_index

algorithms/bit/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
from .has_alternative_bit import *
1414
from .insert_bit import *
1515
from .remove_bit import *
16+
from .count_flips_to_convert import *
17+
from .flip_bit_longest_sequence import *
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Write a function to determine the number of bits you would need to
3+
flip to convert integer A to integer B.
4+
For example:
5+
Input: 29 (or: 11101), 15 (or: 01111)
6+
Output: 2
7+
"""
8+
9+
10+
def count_flips_to_convert(a, b):
11+
12+
diff = a ^ b
13+
14+
# count number of ones in diff
15+
count = 0
16+
while diff:
17+
diff &= (diff - 1)
18+
count += 1
19+
return count
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
You have an integer and you can flip exactly one bit from a 0 to 1.
3+
Write code to find the length of the longest sequence of 1s you could create.
4+
For example:
5+
Input: 1775 ( or: 11011101111)
6+
Output: 8
7+
"""
8+
9+
10+
def flip_bit_longest_seq(num):
11+
12+
curr_len = 0
13+
prev_len = 0
14+
max_len = 0
15+
16+
while num:
17+
if num & 1 == 1: # last digit is 1
18+
curr_len += 1
19+
20+
elif num & 1 == 0: # last digit is 0
21+
if num & 2 == 0: # second last digit is 0
22+
prev_len = 0
23+
else:
24+
prev_len = curr_len
25+
curr_len = 0
26+
27+
max_len = max(max_len, prev_len + curr_len)
28+
num = num >> 1 # right shift num
29+
30+
return max_len + 1

tests/test_array.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
from algorithms.arrays import delete_nth, delete_nth_naive
2-
from algorithms.arrays import flatten, flatten_iter
3-
from algorithms.arrays import garage
4-
from algorithms.arrays import josephus
5-
from algorithms.arrays import longest_non_repeat_v1, longest_non_repeat_v2
6-
from algorithms.arrays import Interval, merge_intervals
7-
from algorithms.arrays import missing_ranges
8-
from algorithms.arrays import move_zeros
9-
from algorithms.arrays import plus_one_v1, plus_one_v2, plus_one_v3
10-
from algorithms.arrays import rotate_v1, rotate_v2, rotate_v3
11-
from algorithms.arrays import summarize_ranges
12-
from algorithms.arrays import three_sum
13-
from algorithms.arrays import two_sum
1+
from algorithms.arrays import (
2+
delete_nth, delete_nth_naive,
3+
flatten_iter, flatten,
4+
garage,
5+
josephus,
6+
longest_non_repeat_v1, longest_non_repeat_v2,
7+
Interval, merge_intervals,
8+
missing_ranges,
9+
move_zeros,
10+
plus_one_v1, plus_one_v2, plus_one_v3,
11+
rotate_v1, rotate_v2, rotate_v3,
12+
summarize_ranges,
13+
three_sum,
14+
two_sum,
15+
max_ones_index
16+
)
1417

1518
import unittest
1619

@@ -152,6 +155,15 @@ def test_longest_non_repeat_v2(self):
152155
self.assertEqual(longest_non_repeat_v2(string), 3)
153156

154157

158+
class TestMaxOnesIndex(unittest.TestCase):
159+
160+
def test_max_ones_index(self):
161+
162+
self.assertEqual(9, max_ones_index([1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1]))
163+
self.assertEqual(3, max_ones_index([1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1]))
164+
self.assertEqual(-1, max_ones_index([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))
165+
166+
155167
class TestMergeInterval(unittest.TestCase):
156168

157169
def test_merge(self):

tests/test_bit.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from algorithms.bit import (
22
add_bitwise_operator,
33
count_ones_iter, count_ones_recur,
4+
count_flips_to_convert,
45
find_missing_number, find_missing_number2,
6+
flip_bit_longest_seq,
57
is_power_of_two,
68
reverse_bits,
79
single_number,
@@ -19,8 +21,13 @@
1921
import unittest
2022
import random
2123

24+
2225
class TestSuite(unittest.TestCase):
2326

27+
def setUp(self):
28+
"""Initialize seed."""
29+
random.seed("test")
30+
2431
def test_add_bitwise_operator(self):
2532
self.assertEqual(5432 + 97823, add_bitwise_operator(5432, 97823))
2633
self.assertEqual(0, add_bitwise_operator(0, 0))
@@ -55,9 +62,15 @@ def test_count_ones_iter(self):
5562
# 0 -> 0
5663
self.assertEqual(0, count_ones_iter(0))
5764

58-
def setUp(self):
59-
"""Initialize seed."""
60-
random.seed("test")
65+
def test_count_flips_to_convert(self):
66+
# 29: 11101 and 15: 01111
67+
self.assertEqual(2, count_flips_to_convert(29, 15))
68+
# 45: 0000101101 and 987: 1111011011
69+
self.assertEqual(8, count_flips_to_convert(45, 987))
70+
# 34: 100010
71+
self.assertEqual(0, count_flips_to_convert(34, 34))
72+
# 34: 100010 and 53: 110101
73+
self.assertEqual(4, count_flips_to_convert(34, 53))
6174

6275
def test_find_missing_number(self):
6376

@@ -79,6 +92,16 @@ def test_find_missing_number2(self):
7992
random.shuffle(nums)
8093
self.assertEqual(12345, find_missing_number2(nums))
8194

95+
def test_flip_bit_longest_seq(self):
96+
# 1775: 11011101111
97+
self.assertEqual(8, flip_bit_longest_seq(1775))
98+
# 5: 101
99+
self.assertEqual(3, flip_bit_longest_seq(5))
100+
# 71: 1000111
101+
self.assertEqual(4, flip_bit_longest_seq(71))
102+
# 0: 0
103+
self.assertEqual(1, flip_bit_longest_seq(0))
104+
82105
def test_is_power_of_two(self):
83106

84107
self.assertTrue(is_power_of_two(64))
@@ -215,5 +238,6 @@ def test_remove_bit(self):
215238
self.assertEqual(5, remove_bit(21, 4))
216239
self.assertEqual(10, remove_bit(21, 0))
217240

241+
218242
if __name__ == '__main__':
219243
unittest.main()

0 commit comments

Comments
 (0)