Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. import copy
  2.  
  3. def partition(N,ar,index,current):
  4.     if N<0:
  5.         return 0
  6.         #dead end
  7.     elif N==0:
  8.         ans.append(current)
  9.     else:
  10.         if index>= len(ar):
  11.             return 0
  12.             #do nothing
  13.         else:
  14.             if ar[index]<=N:
  15.                 aa=copy.deepcopy(current)
  16.                 aa.append(ar[index])
  17.                 partition(N-ar[index],ar,index,aa)
  18.             bb=copy.deepcopy(current)
  19.             partition(N,ar,index+1,bb)
  20.  
  21.  
  22. #program
  23. ans=[[]]
  24. ar=[1,2,3,4,5,6,7]
  25. partition(4,ar,0,list())
  26. print(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement