Description:
The different combinations of the coins in the range of 1 to N that sum to the amount N needs to be printed. I have used recursive function for printing the different coin combinations.
n=4
The above variable is the amount.
arr=[1,2,3,4]
Above array contains the list of coins.
I am getting all the lines for the output but not in the same order.
In the problem its mandatory that the output should be printed in exactly the same order.
Please help me out in printing the combinations in exactly the same order.
OUTPUT OBTAINED:
The different combinations of the coins in the range of 1 to N that sum to the amount N needs to be printed. I have used recursive function for printing the different coin combinations.
n=4
The above variable is the amount.
arr=[1,2,3,4]
Above array contains the list of coins.
I am getting all the lines for the output but not in the same order.
In the problem its mandatory that the output should be printed in exactly the same order.
Please help me out in printing the combinations in exactly the same order.
OUTPUT OBTAINED:
Output:|| Coin 1 occurs: 4 times || Coin 2 occurs: 0 times || Coin 4 occurs: 0 times || Coin 3 occurs: 0 times
|| Coin 1 occurs: 2 times || Coin 2 occurs: 1 times || Coin 4 occurs: 0 times || Coin 3 occurs: 0 times
|| Coin 1 occurs: 1 times || Coin 2 occurs: 0 times || Coin 4 occurs: 0 times || Coin 3 occurs: 1 times
|| Coin 1 occurs: 0 times || Coin 2 occurs: 2 times || Coin 4 occurs: 0 times || Coin 3 occurs: 0 times
|| Coin 1 occurs: 0 times || Coin 2 occurs: 0 times || Coin 4 occurs: 1 times || Coin 3 occurs: 0 times
Total combinations is 5
DESIRED OUTPUT:Output:
|| Coin 1 occurs: 0 times || Coin 2 occurs: 0 times || Coin 4 occurs: 1 times || Coin 3 occurs: 0 times
|| Coin 1 occurs: 0 times || Coin 2 occurs: 2 times || Coin 4 occurs: 0 times || Coin 3 occurs: 0 times
|| Coin 1 occurs: 1 times || Coin 2 occurs: 0 times || Coin 4 occurs: 0 times || Coin 3 occurs: 1 times
|| Coin 1 occurs: 2 times || Coin 2 occurs: 1 times || Coin 4 occurs: 0 times || Coin 3 occurs: 0 times
|| Coin 1 occurs: 4 times || Coin 2 occurs: 0 times || Coin 4 occurs: 0 times || Coin 3 occurs: 0 times
Total combinations is 5
n=4 arr=[1,2,4,3] """Function to calculate the number of different combinations""" def changes(amount,c): ways=[0]*(amount+1) ways[0]=1 for x in c: for y in range(x,amount+1): ways[y]+=ways[y-x] return ways[amount] """Recursive function that prints the combinations""" def allComb(n,s,start): c1=c2=c3=c4=0 if (n==0): for z in range(len(s)): if (s[z]=="4"): c4+=1 elif(s[z]=="3"): c3+=1 elif (s[z]=="2"): c2+=1 elif(s[z]=="1"): c1+=1 print ("|| Coin 1 occurs: %d times"%c1,end=" ") print ("|| Coin 2 occurs: %d times"%c2,end=" ") print ("|| Coin 4 occurs: %d times"%c4,end=" ") print ("|| Coin 3 occurs: %d times"%c3,end=" ") print("") for i in range(start,n+1): allComb(n-i,s+str(i)+"",i) #Recursion new_str="" allComb(n,new_str,1) #Arguments are given for the Recursion function z=changes(n,arr) print("Total combinations is %d "%z)