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

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.

: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):

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():

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"
)
parser.add_argument(
"--coins", type=int, nargs="+", required=True, help="List of coin denominations"
)

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()