Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add doctest/document to actual_power and document to power (TheAlgori…
…thms#11187)

* Update power.py

* Update divide_and_conquer/power.py

---------

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
  • Loading branch information
pedram-mohajer and tianyizheng02 authored Jun 1, 2024
commit 70bd06db4642a2323ff397b041d40bc95ed6a5bf
18 changes: 18 additions & 0 deletions divide_and_conquer/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ def actual_power(a: int, b: int):
"""
Function using divide and conquer to calculate a^b.
It only works for integer a,b.

:param a: The base of the power operation, an integer.
:param b: The exponent of the power operation, a non-negative integer.
:return: The result of a^b.

Examples:
>>> actual_power(3, 2)
9
>>> actual_power(5, 3)
125
>>> actual_power(2, 5)
32
>>> actual_power(7, 0)
1
"""
if b == 0:
return 1
Expand All @@ -13,6 +27,10 @@ def actual_power(a: int, b: int):

def power(a: int, b: int) -> float:
"""
:param a: The base (integer).
:param b: The exponent (integer).
:return: The result of a^b, as a float for negative exponents.

>>> power(4,6)
4096
>>> power(2,3)
Expand Down