Compare commits

...

4 Commits

Author SHA1 Message Date
Pushpak Raj
41cb59a4bf
Merge 7a2ee6eba2a8b3cb43df9ced3674f1e10b635c5f into 338cbafe0d5b07d57f83060ea0f9ba3a6c1155e7 2025-02-10 10:41:58 +08:00
lighting9999
338cbafe0d
Improve power.py (#12567)
* Fix And Add power.py

To fix the inaccuracies and allow handling of negative exponents and bases, the key issue lies in how negative numbers are handled in the power calculation, especially when dividing.
## Example Output:
```python
>>> power(4, 6)
4096
>>> power(2, 3)
8
>>> power(-2, 3)
-8
>>> power(2, -3)
0.125
>>> power(-2, -3)
-0.125
```

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update power.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
2025-02-09 20:51:18 +03:00
pre-commit-ci[bot]
7a2ee6eba2 [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
2025-01-20 06:13:25 +00:00
Pushpak2861
e3c6e81716 fixed sort 2025-01-20 11:42:36 +05:30
2 changed files with 27 additions and 25 deletions

View File

@ -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. Function using divide and conquer to calculate a^b.
It only works for integer a,b. It only works for integer a,b.
@ -19,10 +19,12 @@ def actual_power(a: int, b: int):
""" """
if b == 0: if b == 0:
return 1 return 1
half = actual_power(a, b // 2)
if (b % 2) == 0: if (b % 2) == 0:
return actual_power(a, int(b / 2)) * actual_power(a, int(b / 2)) return half * half
else: 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: def power(a: int, b: int) -> float:
@ -43,9 +45,9 @@ def power(a: int, b: int) -> float:
-0.125 -0.125
""" """
if b < 0: if b < 0:
return 1 / actual_power(a, b) return 1 / actual_power(a, -b)
return actual_power(a, b) return actual_power(a, b)
if __name__ == "__main__": if __name__ == "__main__":
print(power(-2, -3)) print(power(-2, -3)) # output -0.125

View File

@ -15,27 +15,27 @@ edges: dict[str, list[str]] = {
vertices: list[str] = ["a", "b", "c", "d", "e"] vertices: list[str] = ["a", "b", "c", "d", "e"]
def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]: class Topo:
"""Perform topological sort on a directed acyclic graph.""" def topo_sort(self):
current = start visited = set()
# add current to visited stack = []
visited.append(current)
neighbors = edges[current] def dfs(node):
for neighbor in neighbors: visited.add(node)
# if neighbor not in visited, visit
if neighbor not in visited: for neighbor in edges[node]:
sort = topological_sort(neighbor, visited, sort) if neighbor not in visited:
# if all neighbors visited add current to sort dfs(neighbor)
sort.append(current)
# if all vertices haven't been visited select a new one to visit stack.append(node)
if len(visited) != len(vertices):
for vertice in vertices: return stack
if vertice not in visited:
sort = topological_sort(vertice, visited, sort) result = dfs("a")
# return sort return result[::-1]
return sort
if __name__ == "__main__": if __name__ == "__main__":
sort = topological_sort("a", [], []) topo = Topo()
sort = topo.topo_sort()
print(sort) print(sort)