|
| 1 | +""" |
| 2 | +Test cases: |
| 3 | +Do you want to enter your denominations ? (Y/N) :N |
| 4 | +Enter the change you want to make in Indian Currency: 987 |
| 5 | +Following is minimal change for 987 : |
| 6 | +500 100 100 100 100 50 20 10 5 2 |
| 7 | +
|
| 8 | +Do you want to enter your denominations ? (Y/N) :Y |
| 9 | +Enter number of denomination:10 |
| 10 | +1 |
| 11 | +5 |
| 12 | +10 |
| 13 | +20 |
| 14 | +50 |
| 15 | +100 |
| 16 | +200 |
| 17 | +500 |
| 18 | +1000 |
| 19 | +2000 |
| 20 | +Enter the change you want to make: 18745 |
| 21 | +Following is minimal change for 18745 : |
| 22 | +2000 2000 2000 2000 2000 2000 2000 2000 2000 500 200 20 20 5 |
| 23 | +
|
| 24 | +Do you want to enter your denominations ? (Y/N) :N |
| 25 | +Enter the change you want to make: 0 |
| 26 | +The total value cannot be zero or negative. |
| 27 | +Do you want to enter your denominations ? (Y/N) :N |
| 28 | +Enter the change you want to make: -98 |
| 29 | +The total value cannot be zero or negative. |
| 30 | +
|
| 31 | +Do you want to enter your denominations ? (Y/N) :Y |
| 32 | +Enter number of denomination:5 |
| 33 | +1 |
| 34 | +5 |
| 35 | +100 |
| 36 | +500 |
| 37 | +1000 |
| 38 | +Enter the change you want to make: 456 |
| 39 | +Following is minimal change for 456 : |
| 40 | +100 100 100 100 5 5 5 5 5 5 5 5 5 5 5 1 |
| 41 | +""" |
| 42 | + |
| 43 | + |
| 44 | +def find_minimum_change(denominations: list[int], value: int) -> list[int]: |
| 45 | + """ |
| 46 | + Find the minimum change from the given denominations and value |
| 47 | + >>> find_minimum_change([1, 5, 10, 20, 50, 100, 200, 500, 1000,2000], 18745) |
| 48 | + [2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 500, 200, 20, 20, 5] |
| 49 | + >>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 987) |
| 50 | + [500, 100, 100, 100, 100, 50, 20, 10, 5, 2] |
| 51 | + >>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], 0) |
| 52 | + [] |
| 53 | + >>> find_minimum_change([1, 2, 5, 10, 20, 50, 100, 500, 2000], -98) |
| 54 | + [] |
| 55 | + >>> find_minimum_change([1, 5, 100, 500, 1000], 456) |
| 56 | + [100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1] |
| 57 | + """ |
| 58 | + total_value = int(value) |
| 59 | + |
| 60 | + # Initialize Result |
| 61 | + answer = [] |
| 62 | + |
| 63 | + # Traverse through all denomination |
| 64 | + for denomination in reversed(denominations): |
| 65 | + |
| 66 | + # Find denominations |
| 67 | + while int(total_value) >= int(denomination): |
| 68 | + total_value -= int(denomination) |
| 69 | + answer.append(denomination) # Append the "answers" array |
| 70 | + |
| 71 | + return answer |
| 72 | + |
| 73 | + |
| 74 | +# Driver Code |
| 75 | +if __name__ == "__main__": |
| 76 | + |
| 77 | + denominations = list() |
| 78 | + value = 0 |
| 79 | + |
| 80 | + if ( |
| 81 | + input("Do you want to enter your denominations ? (yY/n): ").strip().lower() |
| 82 | + == "y" |
| 83 | + ): |
| 84 | + n = int(input("Enter the number of denominations you want to add: ").strip()) |
| 85 | + |
| 86 | + for i in range(0, n): |
| 87 | + denominations.append(int(input(f"Denomination {i}: ").strip())) |
| 88 | + value = input("Enter the change you want to make in Indian Currency: ").strip() |
| 89 | + else: |
| 90 | + # All denominations of Indian Currency if user does not enter |
| 91 | + denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000] |
| 92 | + value = input("Enter the change you want to make: ").strip() |
| 93 | + |
| 94 | + if int(value) == 0 or int(value) < 0: |
| 95 | + print("The total value cannot be zero or negative.") |
| 96 | + |
| 97 | + else: |
| 98 | + print(f"Following is minimal change for {value}: ") |
| 99 | + answer = find_minimum_change(denominations, value) |
| 100 | + # Print result |
| 101 | + for i in range(len(answer)): |
| 102 | + print(answer[i], end=" ") |
0 commit comments