Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import copy
- def partition(N,ar,index,current):
- if N<0:
- return 0
- #dead end
- elif N==0:
- ans.append(current)
- else:
- if index>= len(ar):
- return 0
- #do nothing
- else:
- if ar[index]<=N:
- aa=copy.deepcopy(current)
- aa.append(ar[index])
- partition(N-ar[index],ar,index,aa)
- bb=copy.deepcopy(current)
- partition(N,ar,index+1,bb)
- #program
- ans=[[]]
- ar=[1,2,3,4,5,6,7]
- partition(4,ar,0,list())
- print(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement