Calculate the power of a base raised to an exponent. >>> power(3, 4) 81 >>> power(2, 0) 1 >>> all(power(base, exponent) == pow(base, exponent) ... for base in range(-10, 10) for exponent in range(10)) True >>> power('a', 1) 'a' >>> power('a', 2)
(base: int, exponent: int)
| 14 | |
| 15 | |
| 16 | def power(base: int, exponent: int) -> float: |
| 17 | """ |
| 18 | Calculate the power of a base raised to an exponent. |
| 19 | |
| 20 | >>> power(3, 4) |
| 21 | 81 |
| 22 | >>> power(2, 0) |
| 23 | 1 |
| 24 | >>> all(power(base, exponent) == pow(base, exponent) |
| 25 | ... for base in range(-10, 10) for exponent in range(10)) |
| 26 | True |
| 27 | >>> power('a', 1) |
| 28 | 'a' |
| 29 | >>> power('a', 2) |
| 30 | Traceback (most recent call last): |
| 31 | ... |
| 32 | TypeError: can't multiply sequence by non-int of type 'str' |
| 33 | >>> power('a', 'b') |
| 34 | Traceback (most recent call last): |
| 35 | ... |
| 36 | TypeError: unsupported operand type(s) for -: 'str' and 'int' |
| 37 | >>> power(2, -1) |
| 38 | Traceback (most recent call last): |
| 39 | ... |
| 40 | RecursionError: maximum recursion depth exceeded |
| 41 | >>> power(0, 0) |
| 42 | 1 |
| 43 | >>> power(0, 1) |
| 44 | 0 |
| 45 | >>> power(5,6) |
| 46 | 15625 |
| 47 | >>> power(23, 12) |
| 48 | 21914624432020321 |
| 49 | """ |
| 50 | return base * power(base, (exponent - 1)) if exponent else 1 |
| 51 | |
| 52 | |
| 53 | if __name__ == "__main__": |
no outgoing calls
no test coverage detected