-
-
Notifications
You must be signed in to change notification settings - Fork 48.7k
Create inverse_coin_change.py #13294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
def inverse_coin_change(amount, 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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Please provide return type hint for the function: |
||
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() |
There was a problem hiding this comment.
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 functioninverse_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