-
Notifications
You must be signed in to change notification settings - Fork 311
/
ch-1.py
43 lines (31 loc) · 1.18 KB
/
ch-1.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
from typing import List
import unittest
def bitwise_or_with_trailing_zero(ints: List[int]) -> bool:
"""
Determine if it's possible to select two or more elements from the list
such that their bitwise OR has at least one trailing zero.
Args:
ints (List[int]): The input list of positive integers.
Returns:
bool: True if such a selection is possible, False otherwise.
"""
n = len(ints)
for i in range(n):
for j in range(i + 1, n):
if (ints[i] | ints[j]) % 2 == 0:
return True
return False
# Unit tests
class TestBitwiseOrWithTrailingZero(unittest.TestCase):
def test_example_1(self):
self.assertTrue(bitwise_or_with_trailing_zero([1, 2, 3, 4, 5]))
def test_example_2(self):
self.assertTrue(bitwise_or_with_trailing_zero([2, 3, 8, 16]))
def test_example_3(self):
self.assertFalse(bitwise_or_with_trailing_zero([1, 2, 5, 7, 9]))
def test_no_trailing_zero(self):
self.assertFalse(bitwise_or_with_trailing_zero([1, 3, 5, 7, 9]))
def test_all_even(self):
self.assertTrue(bitwise_or_with_trailing_zero([2, 4, 6, 8, 10]))
if __name__ == "__main__":
unittest.main()