Skip to content
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

Add change_machine_package with greedy implementation #1

Merged
merged 16 commits into from
Jul 15, 2023
Merged

Conversation

atemate
Copy link
Owner

@atemate atemate commented Jul 15, 2023

With linters, unit tests and basic github actions.

In [1]: from change_machine_package.change_machine import return_coins

In [2]: return_coins(1.23, 500)
Out[2]:
[{'count': 249, 'value': 2, 'name': 'euro', 'type': 'coin'},
 {'count': 1, 'value': 50, 'name': 'cent', 'type': 'coin'},
 {'count': 1, 'value': 25, 'name': 'quarter', 'type': 'coin'},
 {'count': 1, 'value': 2, 'name': 'cent', 'type': 'coin'}]

In [3]: return_coins(1.23, 500, return_coins_only=False)
Out[3]:
[{'count': 2, 'value': 200, 'name': 'euro', 'type': 'banknote'},
 {'count': 1, 'value': 50, 'name': 'euro', 'type': 'banknote'},
 {'count': 2, 'value': 20, 'name': 'euro', 'type': 'banknote'},
 {'count': 1, 'value': 5, 'name': 'euro', 'type': 'banknote'},
 {'count': 1, 'value': 2, 'name': 'euro', 'type': 'coin'},
 {'count': 1, 'value': 1, 'name': 'euro', 'type': 'coin'},
 {'count': 1, 'value': 50, 'name': 'cent', 'type': 'coin'},
 {'count': 1, 'value': 25, 'name': 'quarter', 'type': 'coin'},
 {'count': 1, 'value': 2, 'name': 'cent', 'type': 'coin'}]

Greedy algorithm:

In [4]: from change_machine_package.algorithms import greedy_change

In [5]: greedy_change(3, [1, 2, 5, 10, 20, 50, 100])
Out[5]: [1, 1, 1]

In [6]: sorted([1, 2, 5, 10, 20, 50, 100], reverse=True)
Out[6]: [100, 50, 20, 10, 5, 2, 1]

In [7]: greedy_change(3, [100, 50, 20, 10, 5, 2, 1])
Out[7]: [2, 1]

In [8]: greedy_change(101, [100, 50, 20, 10, 5, 2, 1])
Out[8]: [100, 1]

In [9]: greedy_change(123, [100, 50, 20, 10, 5, 2, 1])
Out[9]: [100, 20, 2, 1]

In [10]: greedy_change(3, [1, 2, 5, 10, 20, 50, 100], force_reverse_coins=True)
Out[10]: [2, 1]

@atemate atemate merged commit b56705e into main Jul 15, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant