-
Notifications
You must be signed in to change notification settings - Fork 164
/
01_BinaryGap.py
130 lines (94 loc) · 4.07 KB
/
01_BinaryGap.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
# Binary Gap
A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at
both ends in the binary representation of N.
For example,
The number 9 has binary representation 1001 and contains a binary gap of length 2.
The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3.
The number 20 has binary representation 10100 and contains one binary gap of length 1.
The number 15 has binary representation 1111 and has no binary gaps.
The number 32 has binary representation 100000 and has no binary gaps.
Write a function:
def solution(N)
that, given a positive integer N, returns the length of its longest binary gap.
The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its
longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation
'100000' and thus no binary gaps.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..2,147,483,647].
Copyright 2009–2022 by Codility Limited. All Rights Reserved.
Unauthorized copying, publication or disclosure prohibited.
"""
import unittest
def solution(N):
"""Determines the maximal 'binary gap' in an integer.
:param N: [int] A positive integer (between 1 and maxint).
:return: [int] The length of the longest sequence of zeros in the binary representation of the integer.
Convert the int to a string of 0/1 chars.
Loop through the chars in the string.
When we see a zero, start the gap counter.
When we see a one, compare with the biggest gap, and save the bigger; reset the gap counter.
When we run out of characters, return the biggest gap.
"""
# Convert the number to a string containing '0' and '1' chars.
binary_string = str(bin(N))[2:]
gap = max_gap = 0
for char in binary_string:
if char == "0":
gap += 1
else:
max_gap = max(gap, max_gap)
gap = 0
return max_gap
class TestBinaryGap(unittest.TestCase):
MAXINT = 2147483647 # The largest input we need worry about.
def test_examples(self):
self.assertEqual(solution(9), 2)
self.assertEqual(solution(529), 4)
self.assertEqual(solution(20), 1)
self.assertEqual(solution(15), 0)
self.assertEqual(solution(32), 0)
def test_example1(self):
self.assertEqual(5, solution(1041))
def test_example2(self):
self.assertEqual(0, solution(15))
def test_extremes(self):
self.assertEqual(0, solution(1))
self.assertEqual(1, solution(5))
self.assertEqual(0, solution(self.MAXINT))
def test_trailing_zeros(self):
self.assertEqual(solution(6), 0)
self.assertEqual(solution(328), 2)
def test_simple1(self):
self.assertEqual(solution(9), 2)
self.assertEqual(solution(11), 1)
self.assertEqual(solution(32), 0)
def test_simple2(self):
self.assertEqual(solution(19), 2)
self.assertEqual(solution(42), 1)
def test_simple3(self):
self.assertEqual(solution(1162), 3)
self.assertEqual(solution(5), 1)
def test_medium1(self):
self.assertEqual(solution(51712), 2)
self.assertEqual(solution(20), 1)
def test_medium2(self):
self.assertEqual(solution(561892), 3)
self.assertEqual(solution(9), 2)
def test_medium3(self):
self.assertEqual(solution(66561), 9)
def test_large1(self):
self.assertEqual(solution(6291457), 20)
def test_large2(self):
self.assertEqual(solution(74901729), 4)
def test_large3(self):
self.assertEqual(solution(805306369), 27)
def test_large4(self):
self.assertEqual(solution(1376796946), 5)
def test_large5(self):
self.assertEqual(solution(1073741825), 29)
def test_large6(self):
self.assertEqual(solution(1610612737), 28)
if __name__ == "__main__":
unittest.main()