mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-11 17:19:48 +00:00
Compare commits
3 Commits
9e13543553
...
0e76901065
Author | SHA1 | Date | |
---|---|---|---|
|
0e76901065 | ||
|
338cbafe0d | ||
|
e59d819d09 |
@ -16,13 +16,13 @@ repos:
|
||||
- id: auto-walrus
|
||||
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: v0.9.3
|
||||
rev: v0.9.4
|
||||
hooks:
|
||||
- id: ruff
|
||||
- id: ruff-format
|
||||
|
||||
- repo: https://github.com/codespell-project/codespell
|
||||
rev: v2.4.0
|
||||
rev: v2.4.1
|
||||
hooks:
|
||||
- id: codespell
|
||||
additional_dependencies:
|
||||
|
@ -1,4 +1,4 @@
|
||||
def actual_power(a: int, b: int):
|
||||
def actual_power(a: int, b: int) -> int:
|
||||
"""
|
||||
Function using divide and conquer to calculate a^b.
|
||||
It only works for integer a,b.
|
||||
@ -19,10 +19,12 @@ def actual_power(a: int, b: int):
|
||||
"""
|
||||
if b == 0:
|
||||
return 1
|
||||
half = actual_power(a, b // 2)
|
||||
|
||||
if (b % 2) == 0:
|
||||
return actual_power(a, int(b / 2)) * actual_power(a, int(b / 2))
|
||||
return half * half
|
||||
else:
|
||||
return a * actual_power(a, int(b / 2)) * actual_power(a, int(b / 2))
|
||||
return a * half * half
|
||||
|
||||
|
||||
def power(a: int, b: int) -> float:
|
||||
@ -43,9 +45,9 @@ def power(a: int, b: int) -> float:
|
||||
-0.125
|
||||
"""
|
||||
if b < 0:
|
||||
return 1 / actual_power(a, b)
|
||||
return 1 / actual_power(a, -b)
|
||||
return actual_power(a, b)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(power(-2, -3))
|
||||
print(power(-2, -3)) # output -0.125
|
||||
|
Loading…
x
Reference in New Issue
Block a user