Return True if this number is power of 4 or False otherwise. >>> power_of_4(0) Traceback (most recent call last): ... ValueError: number must be positive >>> power_of_4(1) True >>> power_of_4(2) False >>> power_of_4(4) True >>> power_of_4(6)
(number: int)
| 16 | |
| 17 | |
| 18 | def power_of_4(number: int) -> bool: |
| 19 | """ |
| 20 | Return True if this number is power of 4 or False otherwise. |
| 21 | |
| 22 | >>> power_of_4(0) |
| 23 | Traceback (most recent call last): |
| 24 | ... |
| 25 | ValueError: number must be positive |
| 26 | >>> power_of_4(1) |
| 27 | True |
| 28 | >>> power_of_4(2) |
| 29 | False |
| 30 | >>> power_of_4(4) |
| 31 | True |
| 32 | >>> power_of_4(6) |
| 33 | False |
| 34 | >>> power_of_4(8) |
| 35 | False |
| 36 | >>> power_of_4(17) |
| 37 | False |
| 38 | >>> power_of_4(64) |
| 39 | True |
| 40 | >>> power_of_4(-1) |
| 41 | Traceback (most recent call last): |
| 42 | ... |
| 43 | ValueError: number must be positive |
| 44 | >>> power_of_4(1.2) |
| 45 | Traceback (most recent call last): |
| 46 | ... |
| 47 | TypeError: number must be an integer |
| 48 | |
| 49 | """ |
| 50 | if not isinstance(number, int): |
| 51 | raise TypeError("number must be an integer") |
| 52 | if number <= 0: |
| 53 | raise ValueError("number must be positive") |
| 54 | if number & (number - 1) == 0: |
| 55 | c = 0 |
| 56 | while number: |
| 57 | c += 1 |
| 58 | number >>= 1 |
| 59 | return c % 2 == 1 |
| 60 | else: |
| 61 | return False |
| 62 | |
| 63 | |
| 64 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected