From 78adb6463f683c7f74c9f854a46bf371e005a25c Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Sat, 8 Feb 2025 17:48:46 +0800 Subject: [PATCH 1/9] 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 ``` --- divide_and_conquer/power.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/divide_and_conquer/power.py b/divide_and_conquer/power.py index faf6a3476d40..3370b08d2665 100644 --- a/divide_and_conquer/power.py +++ b/divide_and_conquer/power.py @@ -19,12 +19,15 @@ 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)) else: - return a * actual_power(a, int(b / 2)) * actual_power(a, int(b / 2)) - - + return half * half + else: + return a * half * half + def power(a: int, b: int) -> float: """ :param a: The base (integer). From c79cb95ddb791a26d0e6868d36b94a95c237b9d2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 8 Feb 2025 09:51:44 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- divide_and_conquer/power.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/divide_and_conquer/power.py b/divide_and_conquer/power.py index 3370b08d2665..b917b0f3b770 100644 --- a/divide_and_conquer/power.py +++ b/divide_and_conquer/power.py @@ -27,7 +27,7 @@ def actual_power(a: int, b: int): return half * half else: return a * half * half - + def power(a: int, b: int) -> float: """ :param a: The base (integer). From 456e4fbf3eb7967b295dbde01587a666fb03b8fc Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Sat, 8 Feb 2025 17:56:18 +0800 Subject: [PATCH 3/9] Update power.py --- divide_and_conquer/power.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/divide_and_conquer/power.py b/divide_and_conquer/power.py index b917b0f3b770..c5846db65d71 100644 --- a/divide_and_conquer/power.py +++ b/divide_and_conquer/power.py @@ -29,26 +29,14 @@ def actual_power(a: int, b: int): return a * half * half 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) - 8 - >>> power(-2,3) - -8 - >>> power(2,-3) - 0.125 - >>> power(-2,-3) - -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 From fe3d3d8593d9a66b16ac6275b6de1fe75059f6a3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 8 Feb 2025 09:56:30 +0000 Subject: [PATCH 4/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- divide_and_conquer/power.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/divide_and_conquer/power.py b/divide_and_conquer/power.py index c5846db65d71..e29932f9d472 100644 --- a/divide_and_conquer/power.py +++ b/divide_and_conquer/power.py @@ -29,7 +29,7 @@ def actual_power(a: int, b: int): return a * half * half 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. From bcd0b508c3e9abd3bb60bda93a137c3a97f18e16 Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Sat, 8 Feb 2025 18:02:22 +0800 Subject: [PATCH 5/9] Update power.py --- divide_and_conquer/power.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/divide_and_conquer/power.py b/divide_and_conquer/power.py index e29932f9d472..4f96bbb66bc6 100644 --- a/divide_and_conquer/power.py +++ b/divide_and_conquer/power.py @@ -25,8 +25,6 @@ def actual_power(a: int, b: int): return actual_power(a, int(b / 2)) * actual_power(a, int(b / 2)) else: return half * half - else: - return a * half * half def power(a: int, b: int) -> float: """ From 9e6c74cd9f4a376abe17e4f420520b5ad3743b81 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 8 Feb 2025 10:02:43 +0000 Subject: [PATCH 6/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- divide_and_conquer/power.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/divide_and_conquer/power.py b/divide_and_conquer/power.py index 4f96bbb66bc6..5a054b0742a8 100644 --- a/divide_and_conquer/power.py +++ b/divide_and_conquer/power.py @@ -26,6 +26,7 @@ def actual_power(a: int, b: int): else: return half * half + def power(a: int, b: int) -> float: """ :param a: The base (integer). @@ -33,8 +34,9 @@ def power(a: int, b: int) -> float: :return: The result of a^b, as a float for negative exponents. """ 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)) #output -0.125 + print(power(-2, -3)) # output -0.125 From fcf79183749ab2edd99f3c4491e4444c94ff2f84 Mon Sep 17 00:00:00 2001 From: lighting9999 <120090117+lighting9999@users.noreply.github.com> Date: Sat, 8 Feb 2025 18:16:12 +0800 Subject: [PATCH 7/9] Update power.py --- divide_and_conquer/power.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/divide_and_conquer/power.py b/divide_and_conquer/power.py index 5a054b0742a8..4cb3f41934dd 100644 --- a/divide_and_conquer/power.py +++ b/divide_and_conquer/power.py @@ -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. @@ -22,9 +22,9 @@ def actual_power(a: int, b: int): half = actual_power(a, b // 2) if (b % 2) == 0: - return actual_power(a, int(b / 2)) * actual_power(a, int(b / 2)) - else: return half * half + else: + return a * half * half def power(a: int, b: int) -> float: From d1f4ea46766e698defe2a8da681b077ecd3ddf22 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 8 Feb 2025 10:16:37 +0000 Subject: [PATCH 8/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- divide_and_conquer/power.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/divide_and_conquer/power.py b/divide_and_conquer/power.py index 4cb3f41934dd..deff6e3f19eb 100644 --- a/divide_and_conquer/power.py +++ b/divide_and_conquer/power.py @@ -1,4 +1,4 @@ -def actual_power(a: int, b: int)-> 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. From 53b5e81166643f9a5965cc1acb8f0207f00d2b59 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sun, 9 Feb 2025 20:45:56 +0300 Subject: [PATCH 9/9] Update power.py --- divide_and_conquer/power.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/divide_and_conquer/power.py b/divide_and_conquer/power.py index deff6e3f19eb..492ee6dd12f0 100644 --- a/divide_and_conquer/power.py +++ b/divide_and_conquer/power.py @@ -32,6 +32,17 @@ 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) + 8 + >>> power(-2,3) + -8 + >>> power(2,-3) + 0.125 + >>> power(-2,-3) + -0.125 """ if b < 0: return 1 / actual_power(a, -b)