From ff961fa04acdf6018dfdb38955ee82e9ae32f7b9 Mon Sep 17 00:00:00 2001 From: Cfengsu2002 Date: Sun, 16 Feb 2025 03:57:21 -0500 Subject: [PATCH 01/11] Update minimum_coin_change.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix: Improve minimum coin change logic & add doctest - Removed unnecessary int() conversions to improve efficiency. - Updated function parameter type (value: str → value: int) to avoid redundant type casting. - Added try-except handling to prevent ValueError when user input is not a number. - Implemented testmod() to enable automated doctest verification. - Fixed issue where input denominations were not sorted in descending order, which previously led to incorrect results. --- greedy_methods/minimum_coin_change.py | 74 ++++++++++++++++----------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/greedy_methods/minimum_coin_change.py b/greedy_methods/minimum_coin_change.py index db2c381bc84a..30bdc72a998c 100644 --- a/greedy_methods/minimum_coin_change.py +++ b/greedy_methods/minimum_coin_change.py @@ -1,3 +1,4 @@ +from doctest import testmod """ Test cases: Do you want to enter your denominations ? (Y/N) :N @@ -39,12 +40,19 @@ Following is minimal change for 456 : 100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1 """ +def find_minimum_change(denominations: list[int], value: int) -> list[int]: + """ + Find the minimum change from the given denominations and value. + Args: + denominations (list[int]): List of available denominations. + value (int): The amount of money to be changed. -def find_minimum_change(denominations: list[int], value: str) -> list[int]: - """ - Find the minimum change from the given denominations and value - >>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000,2000], 18745) + Returns: + list[int]: List of denominations representing the minimal change. + + Examples: + >>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000], 18745) [2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5] >>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 987) [500, 100, 100, 100, 100, 50, 20, 10, 5, 2] @@ -55,46 +63,54 @@ def find_minimum_change(denominations: list[int], value: str) -> list[int]: >>> find_minimum_change([1, 5, 100, 500, 1000], 456) [100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1] """ - total_value = int(value) + + # Sort denominations in descending order (biggest first) + denominations.sort(reverse=True) # Initialize Result answer = [] - - # Traverse through all denomination - for denomination in reversed(denominations): - # Find denominations - while int(total_value) >= int(denomination): - total_value -= int(denomination) - answer.append(denomination) # Append the "answers" array + + # Find minimal change using largest denominations first + for denomination in denominations: + while value >= denomination: + value -= denomination + answer.append(denomination) return answer # Driver Code if __name__ == "__main__": - denominations = [] - value = "0" + # Run doctest + testmod() - if ( - input("Do you want to enter your denominations ? (yY/n): ").strip().lower() - == "y" - ): - n = int(input("Enter the number of denominations you want to add: ").strip()) + denominations = [] + value = 0 - for i in range(n): - denominations.append(int(input(f"Denomination {i}: ").strip())) - value = input("Enter the change you want to make in Indian Currency: ").strip() + if input("Do you want to enter your denominations ? (y/n): ").strip().lower() == "y": + try: + n = int(input("Enter the number of denominations you want to add: ").strip()) + for i in range(n): + denominations.append(int(input(f"Denomination {i+1}: ").strip())) + value = int(input("Enter the change you want to make in Indian Currency: ").strip()) + except ValueError: + print("Invalid input. Please enter valid numbers.") + exit(1) else: - # All denominations of Indian Currency if user does not enter + # Default denominations for Indian Currency denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000] - value = input("Enter the change you want to make: ").strip() + try: + value = int(input("Enter the change you want to make: ").strip()) + except ValueError: + print("Invalid input. Please enter a valid number.") + exit(1) - if int(value) == 0 or int(value) < 0: - print("The total value cannot be zero or negative.") + # Ensure denominations are sorted in descending order + denominations.sort(reverse=True) + if value <= 0: + print("The total value cannot be zero or negative.") else: print(f"Following is minimal change for {value}: ") answer = find_minimum_change(denominations, value) - # Print result - for i in range(len(answer)): - print(answer[i], end=" ") + print(" ".join(map(str, answer))) # Optimized printing format From 72868008ad3195d2ce3d4170d626364ed69c83eb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Feb 2025 09:00:32 +0000 Subject: [PATCH 02/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/minimum_coin_change.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/greedy_methods/minimum_coin_change.py b/greedy_methods/minimum_coin_change.py index 30bdc72a998c..c18cf074c9ce 100644 --- a/greedy_methods/minimum_coin_change.py +++ b/greedy_methods/minimum_coin_change.py @@ -1,4 +1,5 @@ from doctest import testmod + """ Test cases: Do you want to enter your denominations ? (Y/N) :N @@ -40,6 +41,8 @@ Following is minimal change for 456 : 100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1 """ + + def find_minimum_change(denominations: list[int], value: int) -> list[int]: """ Find the minimum change from the given denominations and value. @@ -69,7 +72,7 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: # Initialize Result answer = [] - + # Find minimal change using largest denominations first for denomination in denominations: while value >= denomination: @@ -87,12 +90,19 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: denominations = [] value = 0 - if input("Do you want to enter your denominations ? (y/n): ").strip().lower() == "y": + if ( + input("Do you want to enter your denominations ? (y/n): ").strip().lower() + == "y" + ): try: - n = int(input("Enter the number of denominations you want to add: ").strip()) + n = int( + input("Enter the number of denominations you want to add: ").strip() + ) for i in range(n): - denominations.append(int(input(f"Denomination {i+1}: ").strip())) - value = int(input("Enter the change you want to make in Indian Currency: ").strip()) + denominations.append(int(input(f"Denomination {i + 1}: ").strip())) + value = int( + input("Enter the change you want to make in Indian Currency: ").strip() + ) except ValueError: print("Invalid input. Please enter valid numbers.") exit(1) From ec2723f296baf23d023e02378f0d81833f61e6c9 Mon Sep 17 00:00:00 2001 From: Cfengsu2002 Date: Sun, 16 Feb 2025 04:33:47 -0500 Subject: [PATCH 03/11] Update minimum_coin_change.py Change with format --- greedy_methods/minimum_coin_change.py | 62 ++------------------------- 1 file changed, 3 insertions(+), 59 deletions(-) diff --git a/greedy_methods/minimum_coin_change.py b/greedy_methods/minimum_coin_change.py index c18cf074c9ce..62bf10fdfd7d 100644 --- a/greedy_methods/minimum_coin_change.py +++ b/greedy_methods/minimum_coin_change.py @@ -1,59 +1,13 @@ +import sys from doctest import testmod - -""" -Test cases: -Do you want to enter your denominations ? (Y/N) :N -Enter the change you want to make in Indian Currency: 987 -Following is minimal change for 987 : -500 100 100 100 100 50 20 10 5 2 - -Do you want to enter your denominations ? (Y/N) :Y -Enter number of denomination:10 -1 -5 -10 -20 -50 -100 -200 -500 -1000 -2000 -Enter the change you want to make: 18745 -Following is minimal change for 18745 : -2000 2000 2000 2000 2000 2000 2000 2000 2000 500 200 20 20 5 - -Do you want to enter your denominations ? (Y/N) :N -Enter the change you want to make: 0 -The total value cannot be zero or negative. -Do you want to enter your denominations ? (Y/N) :N -Enter the change you want to make: -98 -The total value cannot be zero or negative. - -Do you want to enter your denominations ? (Y/N) :Y -Enter number of denomination:5 -1 -5 -100 -500 -1000 -Enter the change you want to make: 456 -Following is minimal change for 456 : -100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1 -""" - - def find_minimum_change(denominations: list[int], value: int) -> list[int]: """ Find the minimum change from the given denominations and value. - Args: denominations (list[int]): List of available denominations. value (int): The amount of money to be changed. - Returns: list[int]: List of denominations representing the minimal change. - Examples: >>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000], 18745) [2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5] @@ -66,30 +20,22 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: >>> find_minimum_change([1, 5, 100, 500, 1000], 456) [100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1] """ - # Sort denominations in descending order (biggest first) denominations.sort(reverse=True) - # Initialize Result answer = [] - # Find minimal change using largest denominations first for denomination in denominations: while value >= denomination: value -= denomination answer.append(denomination) - return answer - - # Driver Code if __name__ == "__main__": # Run doctest testmod() - denominations = [] value = 0 - if ( input("Do you want to enter your denominations ? (y/n): ").strip().lower() == "y" @@ -105,7 +51,7 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: ) except ValueError: print("Invalid input. Please enter valid numbers.") - exit(1) + sys.exit(1) else: # Default denominations for Indian Currency denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000] @@ -113,11 +59,9 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: value = int(input("Enter the change you want to make: ").strip()) except ValueError: print("Invalid input. Please enter a valid number.") - exit(1) - + sys.exit(1) # Ensure denominations are sorted in descending order denominations.sort(reverse=True) - if value <= 0: print("The total value cannot be zero or negative.") else: From 8112c055eb6d397c114fc1c9e0aae29a2049f9b6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Feb 2025 09:34:10 +0000 Subject: [PATCH 04/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/minimum_coin_change.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/greedy_methods/minimum_coin_change.py b/greedy_methods/minimum_coin_change.py index 62bf10fdfd7d..f0fff4de6551 100644 --- a/greedy_methods/minimum_coin_change.py +++ b/greedy_methods/minimum_coin_change.py @@ -1,5 +1,7 @@ import sys from doctest import testmod + + def find_minimum_change(denominations: list[int], value: int) -> list[int]: """ Find the minimum change from the given denominations and value. @@ -30,6 +32,8 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: value -= denomination answer.append(denomination) return answer + + # Driver Code if __name__ == "__main__": # Run doctest From d34b0866d59267272e19cb0e2c86283b066e9cea Mon Sep 17 00:00:00 2001 From: Cfengsu2002 Date: Sun, 16 Feb 2025 04:36:25 -0500 Subject: [PATCH 05/11] Update minimum_coin_change.py Finished and formatted --- greedy_methods/minimum_coin_change.py | 64 ++------------------------- 1 file changed, 4 insertions(+), 60 deletions(-) diff --git a/greedy_methods/minimum_coin_change.py b/greedy_methods/minimum_coin_change.py index c18cf074c9ce..b523da7e8c14 100644 --- a/greedy_methods/minimum_coin_change.py +++ b/greedy_methods/minimum_coin_change.py @@ -1,59 +1,13 @@ +import sys from doctest import testmod - -""" -Test cases: -Do you want to enter your denominations ? (Y/N) :N -Enter the change you want to make in Indian Currency: 987 -Following is minimal change for 987 : -500 100 100 100 100 50 20 10 5 2 - -Do you want to enter your denominations ? (Y/N) :Y -Enter number of denomination:10 -1 -5 -10 -20 -50 -100 -200 -500 -1000 -2000 -Enter the change you want to make: 18745 -Following is minimal change for 18745 : -2000 2000 2000 2000 2000 2000 2000 2000 2000 500 200 20 20 5 - -Do you want to enter your denominations ? (Y/N) :N -Enter the change you want to make: 0 -The total value cannot be zero or negative. -Do you want to enter your denominations ? (Y/N) :N -Enter the change you want to make: -98 -The total value cannot be zero or negative. - -Do you want to enter your denominations ? (Y/N) :Y -Enter number of denomination:5 -1 -5 -100 -500 -1000 -Enter the change you want to make: 456 -Following is minimal change for 456 : -100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1 -""" - - def find_minimum_change(denominations: list[int], value: int) -> list[int]: """ Find the minimum change from the given denominations and value. - Args: denominations (list[int]): List of available denominations. value (int): The amount of money to be changed. - Returns: list[int]: List of denominations representing the minimal change. - Examples: >>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000, 2000], 18745) [2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5] @@ -66,30 +20,22 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: >>> find_minimum_change([1, 5, 100, 500, 1000], 456) [100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1] """ - # Sort denominations in descending order (biggest first) denominations.sort(reverse=True) - # Initialize Result answer = [] - # Find minimal change using largest denominations first for denomination in denominations: while value >= denomination: value -= denomination answer.append(denomination) - return answer - - # Driver Code if __name__ == "__main__": # Run doctest testmod() - denominations = [] value = 0 - if ( input("Do you want to enter your denominations ? (y/n): ").strip().lower() == "y" @@ -105,7 +51,7 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: ) except ValueError: print("Invalid input. Please enter valid numbers.") - exit(1) + sys.exit(1) else: # Default denominations for Indian Currency denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000] @@ -113,14 +59,12 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: value = int(input("Enter the change you want to make: ").strip()) except ValueError: print("Invalid input. Please enter a valid number.") - exit(1) - + sys.exit(1) # Ensure denominations are sorted in descending order denominations.sort(reverse=True) - if value <= 0: print("The total value cannot be zero or negative.") else: print(f"Following is minimal change for {value}: ") answer = find_minimum_change(denominations, value) - print(" ".join(map(str, answer))) # Optimized printing format + print(" ".join(map(str, answer))) # Optimized printing format \ No newline at end of file From 0bbf0ea76e478706b1fa9ea377465c52f5195c6f Mon Sep 17 00:00:00 2001 From: Cfengsu2002 Date: Sun, 16 Feb 2025 04:43:41 -0500 Subject: [PATCH 06/11] Update minimum_coin_change.py Add some input and output examples on top --- greedy_methods/minimum_coin_change.py | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/greedy_methods/minimum_coin_change.py b/greedy_methods/minimum_coin_change.py index f0fff4de6551..c6e4a28e8459 100644 --- a/greedy_methods/minimum_coin_change.py +++ b/greedy_methods/minimum_coin_change.py @@ -1,7 +1,45 @@ import sys from doctest import testmod +""" +Test cases: +Do you want to enter your denominations ? (Y/N) :N +Enter the change you want to make in Indian Currency: 987 +Following is minimal change for 987 : +500 100 100 100 100 50 20 10 5 2 +Do you want to enter your denominations ? (Y/N) :Y +Enter number of denomination:10 +1 +5 +10 +20 +50 +100 +200 +500 +1000 +2000 +Enter the change you want to make: 18745 +Following is minimal change for 18745 : +2000 2000 2000 2000 2000 2000 2000 2000 2000 500 200 20 20 5 +Do you want to enter your denominations ? (Y/N) :N +Enter the change you want to make: 0 +The total value cannot be zero or negative. +Do you want to enter your denominations ? (Y/N) :N +Enter the change you want to make: -98 +The total value cannot be zero or negative. +Do you want to enter your denominations ? (Y/N) :Y +Enter number of denomination:5 +1 +5 +100 +500 +1000 +Enter the change you want to make: 456 +Following is minimal change for 456 : +100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1 +""" def find_minimum_change(denominations: list[int], value: int) -> list[int]: """ Find the minimum change from the given denominations and value. From d4d24a610a66bec4f2232a6c1e6460054c46463e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Feb 2025 09:44:03 +0000 Subject: [PATCH 07/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/minimum_coin_change.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/greedy_methods/minimum_coin_change.py b/greedy_methods/minimum_coin_change.py index c6e4a28e8459..762f0c089036 100644 --- a/greedy_methods/minimum_coin_change.py +++ b/greedy_methods/minimum_coin_change.py @@ -1,5 +1,6 @@ import sys from doctest import testmod + """ Test cases: Do you want to enter your denominations ? (Y/N) :N @@ -40,6 +41,8 @@ Following is minimal change for 456 : 100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1 """ + + def find_minimum_change(denominations: list[int], value: int) -> list[int]: """ Find the minimum change from the given denominations and value. From af72388b49ab5ba593309f54fcf6f29c94a53531 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Feb 2025 09:45:10 +0000 Subject: [PATCH 08/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/minimum_coin_change.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_methods/minimum_coin_change.py b/greedy_methods/minimum_coin_change.py index 33a0f43c50c9..a4d779710a75 100644 --- a/greedy_methods/minimum_coin_change.py +++ b/greedy_methods/minimum_coin_change.py @@ -119,4 +119,4 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: else: print(f"Following is minimal change for {value}: ") answer = find_minimum_change(denominations, value) - print(" ".join(map(str, answer))) # Optimized printing format \ No newline at end of file + print(" ".join(map(str, answer))) # Optimized printing format From aa131539028764f97751c080d3613626aaafc602 Mon Sep 17 00:00:00 2001 From: Cfengsu2002 Date: Sun, 16 Feb 2025 05:00:20 -0500 Subject: [PATCH 09/11] Update minimum_coin_change.py The correct version with input and output explanation --- greedy_methods/minimum_coin_change.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/greedy_methods/minimum_coin_change.py b/greedy_methods/minimum_coin_change.py index a4d779710a75..f01458c61f3c 100644 --- a/greedy_methods/minimum_coin_change.py +++ b/greedy_methods/minimum_coin_change.py @@ -1,11 +1,5 @@ import sys from doctest import testmod -<<<<<<< HEAD -<<<<<<< HEAD -======= -======= - ->>>>>>> d4d24a610a66bec4f2232a6c1e6460054c46463e """ Test cases: Do you want to enter your denominations ? (Y/N) :N @@ -35,6 +29,7 @@ Do you want to enter your denominations ? (Y/N) :N Enter the change you want to make: -98 The total value cannot be zero or negative. + Do you want to enter your denominations ? (Y/N) :Y Enter number of denomination:5 1 @@ -46,12 +41,6 @@ Following is minimal change for 456 : 100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1 """ -<<<<<<< HEAD ->>>>>>> 0bbf0ea76e478706b1fa9ea377465c52f5195c6f -======= - - ->>>>>>> d4d24a610a66bec4f2232a6c1e6460054c46463e def find_minimum_change(denominations: list[int], value: int) -> list[int]: """ Find the minimum change from the given denominations and value. From 448f21964d817205645b0090b2835cbb64dd42a4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Feb 2025 10:00:57 +0000 Subject: [PATCH 10/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/minimum_coin_change.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/greedy_methods/minimum_coin_change.py b/greedy_methods/minimum_coin_change.py index f01458c61f3c..6bf895d964be 100644 --- a/greedy_methods/minimum_coin_change.py +++ b/greedy_methods/minimum_coin_change.py @@ -1,5 +1,6 @@ import sys from doctest import testmod + """ Test cases: Do you want to enter your denominations ? (Y/N) :N @@ -41,6 +42,8 @@ Following is minimal change for 456 : 100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1 """ + + def find_minimum_change(denominations: list[int], value: int) -> list[int]: """ Find the minimum change from the given denominations and value. @@ -71,6 +74,8 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: value -= denomination answer.append(denomination) return answer + + # Driver Code if __name__ == "__main__": # Run doctest From ffdd40cdf700b05affde97ed957fa4c07336b7e5 Mon Sep 17 00:00:00 2001 From: Cfengsu2002 Date: Sun, 16 Feb 2025 05:09:29 -0500 Subject: [PATCH 11/11] This is the final correct version