Skip to content
Closed
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
47 changes: 47 additions & 0 deletions backtracking/inverse_coin_change.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
def inverse_coin_change(amount, coins):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file backtracking/inverse_coin_change.py, please provide doctest for the function inverse_coin_change

Please provide return type hint for the function: inverse_coin_change. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: amount

Please provide type hint for the parameter: coins

"""
Finds all combinations of coins that sum up to the given amount.

Check failure on line 4 in backtracking/inverse_coin_change.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

backtracking/inverse_coin_change.py:4:1: W293 Blank line contains whitespace
:param amount: Target amount
:param coins: List of coin denominations
:return: List of combinations (each combination is a list of coins)
"""
results = []

def backtrack(remaining, combo, start):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file backtracking/inverse_coin_change.py, please provide doctest for the function backtrack

Please provide return type hint for the function: backtrack. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: remaining

Please provide type hint for the parameter: combo

Please provide type hint for the parameter: start

if remaining == 0:
results.append(list(combo))
return
for i in range(start, len(coins)):
coin = coins[i]
if coin <= remaining:
combo.append(coin)
backtrack(remaining - coin, combo, i) # allow reuse
combo.pop()

coins.sort()
backtrack(amount, [], 0)
return results


def main():
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file backtracking/inverse_coin_change.py, please provide doctest for the function main

Please provide return type hint for the function: main. If the function does not return a value, please provide the type hint as: def function() -> None:

import argparse

parser = argparse.ArgumentParser(description="Inverse Coin Change Solver")
parser.add_argument("--amount", type=int, required=True, help="Target amount to reach")

Check failure on line 31 in backtracking/inverse_coin_change.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/inverse_coin_change.py:31:89: E501 Line too long (91 > 88)
parser.add_argument("--coins", type=int, nargs="+", required=True, help="List of coin denominations")

Check failure on line 32 in backtracking/inverse_coin_change.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

backtracking/inverse_coin_change.py:32:89: E501 Line too long (105 > 88)

args = parser.parse_args()

solutions = inverse_coin_change(args.amount, args.coins)

if not solutions:
print(f"No combinations found to make {args.amount} using coins {args.coins}")
else:
print(f"Combinations to make {args.amount} using coins {args.coins}:")
for combo in solutions:
print(combo)


if __name__ == "__main__":
main()
Loading