You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a positive integer, return "Odd" if it's an odd number, and "Even" is it's even.
4
+
"""
5
+
6
+
importunittest
7
+
8
+
classOddOrEvenTest(unittest.TestCase):
9
+
10
+
deftest1(self):
11
+
self.assertEqual(odd_or_even(1), "Odd")
12
+
13
+
deftest2(self):
14
+
self.assertEqual(odd_or_even(2), "Even")
15
+
16
+
deftest3(self):
17
+
self.assertEqual(odd_or_even(13), "Odd")
18
+
19
+
deftest4(self):
20
+
self.assertEqual(odd_or_even(196), "Even")
21
+
22
+
deftest5(self):
23
+
self.assertEqual(odd_or_even(123456789), "Odd")
24
+
25
+
26
+
27
+
defodd_or_even(n):
28
+
29
+
ifn%2==0:
30
+
return"Even"
31
+
else:
32
+
return"Odd"
33
+
34
+
defodd_or_even(n):
35
+
36
+
return"Even"ifn%2==0else"Odd"
37
+
38
+
defodd_or_even_bit_wise(n):
39
+
40
+
return"Odd"if (n&1) else"Even"
41
+
42
+
"""
43
+
44
+
45
+
1. What does n & 1 mean?
46
+
=> & is the bitwise AND operator.
47
+
=> It compares each bit of two numbers.
48
+
=> 1 in binary is ...0001.
49
+
=> So n & 1 checks only the last bit of n.
50
+
51
+
52
+
2. Why does the last bit matter?
53
+
=> In binary, even numbers always end with 0, odd numbers end with 1.
54
+
Example:
55
+
-> 4 -> 100 (last bit = 0 -> even)
56
+
-> 7 -> 111 (last bit = 1 -> odd)
57
+
58
+
3. How the check works
59
+
=> If n & 1 == 1 -> last bit is 1 -> Odd.
60
+
=> If n & 1 == 0 -> last bit is 0 -> Even.
61
+
62
+
4. Why use bitwise instead of %?
63
+
-> % 2 is more readable and common in high-level code.
64
+
-> n & 1 is faster at the machine level because it directly inspects the binary representation.
65
+
-> In practice, modern compilers optimize % 2 into the same bitwise operation, so performance difference is negligible.
66
+
-> But n & 1 is a neat trick that shows you understand how numbers are represented in binary.
67
+
68
+
69
+
Method Code Idea
70
+
71
+
Modulus n % 2 == 0 Divide by 2, check remainder
72
+
73
+
Bitwise AND n & 1 == 0 Inspect last binary bit
74
+
75
+
Both are correct - modulus is more readable, bitwise is lower-level and faster in theory.
76
+
77
+
The paranthesis for the case of the remainder but not the other is
78
+
79
+
=> Here, n % 2 == 0 is a comparison expression.
80
+
-> Python's operator precedence makes it clear: % is evaluated first , then ==
81
+
=> so parathesis aren't needed = the intent is obvious.
82
+
83
+
84
+
In the next example
85
+
86
+
-> n & 1 is a bitwisee operation.
87
+
-> It produces either 0 or 1.
88
+
-> Since 0 is falsy and 1 is truthy, we use it directly in the conditional expression.
89
+
-> Parentheses are added for clarity, not necessity, you could write.
90
+
91
+
return "Odd" if n & 1 else "Even"
92
+
93
+
-> it works the same as well. But many developers prefer parantheses around bitwise expressions to make it visually clear that the condition is based on a binary mask, not a logical comparison.
94
+
95
+
No functional difference - paranthesis are just stylistic clarity in the bitwise case.
96
+
97
+
Logical AND , OR , NOT work with truth values (True/ False)
0 commit comments