return true if the input integer is even Explanation: Lets take a look at the following decimal to binary conversions 2 => 10 14 => 1110 100 => 1100100 3 => 11 13 => 1101 101 => 1100101 from the above examples we can observe that for all the odd integers ther
(number: int)
| 1 | def is_even(number: int) -> bool: |
| 2 | """ |
| 3 | return true if the input integer is even |
| 4 | Explanation: Lets take a look at the following decimal to binary conversions |
| 5 | 2 => 10 |
| 6 | 14 => 1110 |
| 7 | 100 => 1100100 |
| 8 | 3 => 11 |
| 9 | 13 => 1101 |
| 10 | 101 => 1100101 |
| 11 | from the above examples we can observe that |
| 12 | for all the odd integers there is always 1 set bit at the end |
| 13 | also, 1 in binary can be represented as 001, 00001, or 0000001 |
| 14 | so for any odd integer n => n&1 is always equals 1 else the integer is even |
| 15 | |
| 16 | >>> is_even(1) |
| 17 | False |
| 18 | >>> is_even(4) |
| 19 | True |
| 20 | >>> is_even(9) |
| 21 | False |
| 22 | >>> is_even(15) |
| 23 | False |
| 24 | >>> is_even(40) |
| 25 | True |
| 26 | >>> is_even(100) |
| 27 | True |
| 28 | >>> is_even(101) |
| 29 | False |
| 30 | """ |
| 31 | return number & 1 == 0 |
| 32 | |
| 33 | |
| 34 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected