-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathodd_check.py
48 lines (41 loc) · 1 KB
/
odd_check.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
def is_odd(number: int) -> bool:
"""
Test if a number is a odd number.
:param number: the number to be checked.
:return: True if the number is odd, otherwise False.
>>> is_odd(-1)
True
>>> is_odd(-2)
False
>>> is_odd(0)
False
>>> is_odd(3)
True
>>> is_odd(4)
False
>>> all([is_odd(i) for i in range(1, 100, 2)])
True
"""
return number % 2 != 0
def is_odd_faster(number: int) -> bool:
"""
Test if a number is a odd number using bit operator.
:param number: the number to be checked.
:return: True if the number is odd, otherwise False.
>>> is_odd_faster(-1)
True
>>> is_odd_faster(-2)
False
>>> is_odd_faster(0)
False
>>> is_odd_faster(3)
True
>>> is_odd_faster(4)
False
>>> all([is_odd_faster(i) for i in range(1, 100, 2)])
True
"""
return number & 1 != 0
if __name__ == "__main__":
from doctest import testmod
testmod()