Python Forum
Print the frequency of each coin for the combinations that sum to the amount N
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print the frequency of each coin for the combinations that sum to the amount N
#3
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

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
Reply


Messages In This Thread
RE: Print the frequency of each coin for the combinations that sum to the amount N - by Pranav - May-17-2020, 03:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  frequency tables Tink 0 1,573 Dec-02-2020, 12:44 PM
Last Post: Tink
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 3,064 Sep-24-2020, 02:26 PM
Last Post: buran
  Unexpected change to a list in a small amount of self-contained code Johno 5 3,964 Mar-15-2020, 05:06 PM
Last Post: jefsummers
  Random module, coin flipping gus17 3 8,583 Jan-06-2020, 10:29 AM
Last Post: perfringo
  Need help with coin change program Gateux 2 7,704 Jun-25-2019, 02:32 PM
Last Post: perfringo
  Combinations mnnewick 1 3,420 Dec-17-2018, 02:35 PM
Last Post: ichabod801
  Program that displays the number with the greatest amount of factors ilusmd 3 3,692 Nov-01-2018, 08:28 PM
Last Post: ichabod801
  Adding and Removing coins to match Coin Bag Total infinite times Strayfe 8 6,268 Sep-11-2018, 07:30 PM
Last Post: gruntfutuk
  Looping over amount of attempts Beatenberg 4 8,640 Oct-17-2017, 07:47 AM
Last Post: Beatenberg
  Four digit combinations EHod 4 9,355 Aug-13-2017, 09:14 PM
Last Post: EHod

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020