mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Fix doctest power recursion (#10659)
* Added doctests to power_using_recursion.py * Added doctest to power_using_recursion.py * Update power_using_recursion.py * Update power_using_recursion.py --------- Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
9da6f48b46
commit
788e4ede9b
|
@ -15,18 +15,35 @@
|
|||
|
||||
def power(base: int, exponent: int) -> float:
|
||||
"""
|
||||
power(3, 4)
|
||||
>>> 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)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: can't multiply sequence by non-int of type 'str'
|
||||
>>> power('a', 'b')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: unsupported operand type(s) for -: 'str' and 'int'
|
||||
>>> power(2, -1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
RecursionError: maximum recursion depth exceeded
|
||||
"""
|
||||
return base * power(base, (exponent - 1)) if exponent else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from doctests import testmod
|
||||
|
||||
testmod()
|
||||
print("Raise base to the power of exponent using recursion...")
|
||||
base = int(input("Enter the base: ").strip())
|
||||
exponent = int(input("Enter the exponent: ").strip())
|
||||
|
|
Loading…
Reference in New Issue
Block a user