Print the frequency of each coin for the combinations that sum to the amount N - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Print the frequency of each coin for the combinations that sum to the amount N (/thread-26873.html) |
Print the frequency of each coin for the combinations that sum to the amount N - Pranav - May-17-2020 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: DESIRED OUTPUT:
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) RE: Print the frequency of each coin for the combinations that sum to the amount N - jefsummers - May-17-2020 Comparing Output with Desired, it appears they are in the same order, but reversed. Since you are using recursion, you can't just reverse a loop. Suggest you have your recursive function return the string to be printed as a list element rather than actually print. Add the list to the arguments as well, and each time rather than print you append. Then, when allComb finally ends you can print the list in any order. Generally good practice not to have the output inside the same function that is doing the calculations. Can get you into problems like this. RE: Print the frequency of each coin for the combinations that sum to the amount N - Pranav - May-17-2020 Thanks for your help. I am able to get the desired output now. The below code was used n=4 arr=[1,2,4,3] arr1=[] """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] """Function for appending data to an array""" def Creat(a,string): a.append(string) return a """Recursive function that prints the combinations""" def allComb(n,s,start): if (n==0): Creat(arr1,s) for i in range(start,n+1): allComb(n-i,s+str(i)+"",i) #Recursion return arr1 new_str="" z=allComb(n,new_str,1) #Arguments are given for the Recursion function for i in range(len(z)-1,-1,-1): c1=c2=c3=c4=0 for j in z[i]: if str(j)=="1": c1+=1 elif str(j)=="2": c2+=1 elif str(j)=="3": c3+=1 elif str(j)=="4": c4+=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("") result=changes(n,arr) print("Total combinations is %d "%result)The output obtained is
RE: Print the frequency of each coin for the combinations that sum to the amount N - Pranav - May-19-2020 Please suggest if the above problem can be solved using iteration as well. If possible then please let me know the logic that can be applied. |