From ea140610aab7876098b1e523e71b58e07f76dc89 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 8 Oct 2023 21:06:02 +0530 Subject: [PATCH 1/4] to add min_coins program --- greedy_methods/minimum_coins.py | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 greedy_methods/minimum_coins.py diff --git a/greedy_methods/minimum_coins.py b/greedy_methods/minimum_coins.py new file mode 100644 index 000000000000..ea821a6162a9 --- /dev/null +++ b/greedy_methods/minimum_coins.py @@ -0,0 +1,43 @@ +""" +Given a list of coin denominations and an amount, +this program calculates the minimum number of coins needed to make up that amount. + +Example : +coins = [1, 2, 5, 10, 20, 50, 100, 500, 2000], +amount = 121, +The minimum number of coins would be 3 (100 + 20 + 1). + +This problem can be solved using the concept of "GREEDY ALGORITHM". + +We start with the largest denomination of coins and use as many of those, +as possible before moving to the next largest denomination. +This process continues until the entire amount has been made up of coins. +""" + +def min_coins(coins : list[int], amount : int = 0) -> tuple: + """ + >>> min_coins([1, 2, 5, 10, 20, 50, 100, 500, 2000],121) + (3, [100, 20, 1]) + >>> min_coins([1, 2, 5, 10, 20, 50, 100],343) + (7, [100, 100, 100, 20, 20, 2, 1]) + >>> min_coins([1,2,5,10],0) + (0, []) + """ + coins.sort(reverse=True) + count : int = 0 + coins_list : list[int] = [] + for coin in coins: + if coin <= amount: + while coin <= amount: + count += 1 + coins_list.append(coin) + amount -= coin + + return count, coins_list + +if __name__ == "__main__": + import doctest + doctest.testmod() + + print(f"{min_coins([1, 2, 5, 10, 20, 50, 100, 500, 2000],121)}") + From 78773d9a75e50a2b5282fc3af8129c9790121b15 Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Sun, 8 Oct 2023 21:09:17 +0530 Subject: [PATCH 2/4] to add min_coins program --- greedy_methods/minimum_coins.py | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 greedy_methods/minimum_coins.py diff --git a/greedy_methods/minimum_coins.py b/greedy_methods/minimum_coins.py new file mode 100644 index 000000000000..ea821a6162a9 --- /dev/null +++ b/greedy_methods/minimum_coins.py @@ -0,0 +1,43 @@ +""" +Given a list of coin denominations and an amount, +this program calculates the minimum number of coins needed to make up that amount. + +Example : +coins = [1, 2, 5, 10, 20, 50, 100, 500, 2000], +amount = 121, +The minimum number of coins would be 3 (100 + 20 + 1). + +This problem can be solved using the concept of "GREEDY ALGORITHM". + +We start with the largest denomination of coins and use as many of those, +as possible before moving to the next largest denomination. +This process continues until the entire amount has been made up of coins. +""" + +def min_coins(coins : list[int], amount : int = 0) -> tuple: + """ + >>> min_coins([1, 2, 5, 10, 20, 50, 100, 500, 2000],121) + (3, [100, 20, 1]) + >>> min_coins([1, 2, 5, 10, 20, 50, 100],343) + (7, [100, 100, 100, 20, 20, 2, 1]) + >>> min_coins([1,2,5,10],0) + (0, []) + """ + coins.sort(reverse=True) + count : int = 0 + coins_list : list[int] = [] + for coin in coins: + if coin <= amount: + while coin <= amount: + count += 1 + coins_list.append(coin) + amount -= coin + + return count, coins_list + +if __name__ == "__main__": + import doctest + doctest.testmod() + + print(f"{min_coins([1, 2, 5, 10, 20, 50, 100, 500, 2000],121)}") + From 4a81f0227c3202eb883b4850890c4d60fb1d85ad Mon Sep 17 00:00:00 2001 From: kosuri-indu Date: Fri, 13 Oct 2023 16:32:06 +0530 Subject: [PATCH 3/4] Adding min_coins.py --- greedy_methods/minimum_coins.py | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 greedy_methods/minimum_coins.py diff --git a/greedy_methods/minimum_coins.py b/greedy_methods/minimum_coins.py new file mode 100644 index 000000000000..ea821a6162a9 --- /dev/null +++ b/greedy_methods/minimum_coins.py @@ -0,0 +1,43 @@ +""" +Given a list of coin denominations and an amount, +this program calculates the minimum number of coins needed to make up that amount. + +Example : +coins = [1, 2, 5, 10, 20, 50, 100, 500, 2000], +amount = 121, +The minimum number of coins would be 3 (100 + 20 + 1). + +This problem can be solved using the concept of "GREEDY ALGORITHM". + +We start with the largest denomination of coins and use as many of those, +as possible before moving to the next largest denomination. +This process continues until the entire amount has been made up of coins. +""" + +def min_coins(coins : list[int], amount : int = 0) -> tuple: + """ + >>> min_coins([1, 2, 5, 10, 20, 50, 100, 500, 2000],121) + (3, [100, 20, 1]) + >>> min_coins([1, 2, 5, 10, 20, 50, 100],343) + (7, [100, 100, 100, 20, 20, 2, 1]) + >>> min_coins([1,2,5,10],0) + (0, []) + """ + coins.sort(reverse=True) + count : int = 0 + coins_list : list[int] = [] + for coin in coins: + if coin <= amount: + while coin <= amount: + count += 1 + coins_list.append(coin) + amount -= coin + + return count, coins_list + +if __name__ == "__main__": + import doctest + doctest.testmod() + + print(f"{min_coins([1, 2, 5, 10, 20, 50, 100, 500, 2000],121)}") + From 3aa1ec9e659f8e669cc99685770af92e2bcf83c7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 13 Oct 2023 11:11:59 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/minimum_coins.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/greedy_methods/minimum_coins.py b/greedy_methods/minimum_coins.py index ea821a6162a9..14ddf3a797e5 100644 --- a/greedy_methods/minimum_coins.py +++ b/greedy_methods/minimum_coins.py @@ -4,7 +4,7 @@ Example : coins = [1, 2, 5, 10, 20, 50, 100, 500, 2000], -amount = 121, +amount = 121, The minimum number of coins would be 3 (100 + 20 + 1). This problem can be solved using the concept of "GREEDY ALGORITHM". @@ -14,7 +14,8 @@ This process continues until the entire amount has been made up of coins. """ -def min_coins(coins : list[int], amount : int = 0) -> tuple: + +def min_coins(coins: list[int], amount: int = 0) -> tuple: """ >>> min_coins([1, 2, 5, 10, 20, 50, 100, 500, 2000],121) (3, [100, 20, 1]) @@ -24,8 +25,8 @@ def min_coins(coins : list[int], amount : int = 0) -> tuple: (0, []) """ coins.sort(reverse=True) - count : int = 0 - coins_list : list[int] = [] + count: int = 0 + coins_list: list[int] = [] for coin in coins: if coin <= amount: while coin <= amount: @@ -35,9 +36,10 @@ def min_coins(coins : list[int], amount : int = 0) -> tuple: return count, coins_list + if __name__ == "__main__": import doctest + doctest.testmod() - + print(f"{min_coins([1, 2, 5, 10, 20, 50, 100, 500, 2000],121)}") -