Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def noofSums(n,S,currDict):
  2.  
  3.   if n==0:
  4.     target=0
  5.     Pstring=""
  6.     for key in currDict:
  7.       if currDict[key]!=0:
  8.         Pstring = Pstring + str(key) + "*" + str(currDict[key]) + " + "
  9.         target = target+key*currDict[key]
  10.     Pstring = Pstring[:-3]
  11.     print Pstring, "=", target
  12.     return 1
  13.   elif n<0:
  14.     return 0
  15.  
  16.   if len(S)==0:
  17.     return 0
  18.  
  19.   Scopy = S.copy()
  20.   trial = Scopy.pop()
  21.   ret_value = 0
  22.  
  23.   for i in range(0,n/trial+1):
  24.     currDict[trial]=i
  25.     ret_value = ret_value + noofSums(n-i*trial,Scopy,currDict)
  26.     del currDict[trial]
  27.   return ret_value
  28.  
  29. if __name__ == '__main__':
  30.   S={1,2,3}
  31.   n=10
  32.   print n, S
  33.   result = noofSums(n,S,dict())
  34.   print "Total combination ", result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement