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