Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. from functools import reduce
  2. import math
  3.  
  4. def listtotal(xs):
  5. if len(xs) == 0:
  6. return 0
  7. else:
  8. return reduce(lambda x,y: x + y, xs)
  9.  
  10. def reversecopy(xs):
  11. output = []
  12. for i in xs:
  13. output.insert(0,i)
  14. return output
  15.  
  16. def pilesort(n,p,s): #splits [1,n] into p subsets, each summing to s.
  17. output = []
  18. numbers = list(range(1,n+1))
  19. templist = []
  20. if (s*p) == int(n*(n+1)/2): #if it even makes sense to try doing this
  21. while len(output) < p:
  22. templist = []
  23. while not listtotal(templist) == s:
  24. for i in reversecopy(list(range(0,len(numbers)))):
  25. if listtotal(templist) + numbers[i] <= s:
  26. templist.append(numbers.pop(i))
  27. output.append(templist)
  28. else:
  29. return "Error!"
  30. return output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement