May-17-2020, 03:57 PM
Thanks for your help. I am able to get the desired output now.
The below code was used
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