Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 37cae3f

Browse files
Muhammadummerrpre-commit-ci[bot]tianyizheng02
authored
Updated test cases of power_sum.py (TheAlgorithms#9978)
* Updated test cases of power_sum.py * updated * updated. * remove extra comment and used ** instead of pow * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update backtracking/power_sum.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
1 parent 0b2c9fb commit 37cae3f

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

‎backtracking/power_sum.py‎

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
The only solution is 2^2+3^2. Constraints: 1<=X<=1000, 2<=N<=10.
77
"""
88

9-
from math import pow
10-
119

1210
def backtrack(
1311
needed_sum: int,
@@ -19,25 +17,25 @@ def backtrack(
1917
"""
2018
>>> backtrack(13, 2, 1, 0, 0)
2119
(0, 1)
22-
>>> backtrack(100, 2, 1, 0, 0)
23-
(0, 3)
24-
>>> backtrack(100, 3, 1, 0, 0)
20+
>>> backtrack(10, 2, 1, 0, 0)
21+
(0, 1)
22+
>>> backtrack(10, 3, 1, 0, 0)
23+
(0, 0)
24+
>>> backtrack(20, 2, 1, 0, 0)
2525
(0, 1)
26-
>>> backtrack(800, 2, 1, 0, 0)
27-
(0, 561)
28-
>>> backtrack(1000, 10, 1, 0, 0)
26+
>>> backtrack(15, 10, 1, 0, 0)
2927
(0, 0)
30-
>>> backtrack(400, 2, 1, 0, 0)
31-
(0, 55)
32-
>>> backtrack(50, 1, 1, 0, 0)
33-
(0, 3658)
28+
>>> backtrack(16, 2, 1, 0, 0)
29+
(0, 1)
30+
>>> backtrack(20, 1, 1, 0, 0)
31+
(0, 64)
3432
"""
3533
if current_sum == needed_sum:
3634
# If the sum of the powers is equal to needed_sum, then we have a solution.
3735
solutions_count += 1
3836
return current_sum, solutions_count
3937

40-
i_to_n = int(pow(current_number, power))
38+
i_to_n = current_number**power
4139
if current_sum + i_to_n <= needed_sum:
4240
# If the sum of the powers is less than needed_sum, then continue adding powers.
4341
current_sum += i_to_n
@@ -57,17 +55,17 @@ def solve(needed_sum: int, power: int) -> int:
5755
"""
5856
>>> solve(13, 2)
5957
1
60-
>>> solve(100, 2)
61-
3
62-
>>> solve(100, 3)
58+
>>> solve(10, 2)
6359
1
64-
>>> solve(800, 2)
65-
561
66-
>>> solve(1000, 10)
60+
>>> solve(10, 3)
6761
0
68-
>>> solve(400, 2)
69-
55
70-
>>> solve(50, 1)
62+
>>> solve(20, 2)
63+
1
64+
>>> solve(15, 10)
65+
0
66+
>>> solve(16, 2)
67+
1
68+
>>> solve(20, 1)
7169
Traceback (most recent call last):
7270
...
7371
ValueError: Invalid input

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /