Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import copy
  4.  
  5. def allCombos(scratch, data):
  6. '''
  7. Returns all possible combinations of objects in an array
  8.  
  9. Args:
  10. ----
  11. scratch - an empty array
  12. data - the array to choose from
  13.  
  14. Returns:
  15. ----
  16. None
  17.  
  18. Yields:
  19. ------
  20.  
  21. A generator function for returning all combinations.
  22.  
  23. '''
  24. for j in range(len(data)):
  25. new_scratch = copy.copy(scratch)
  26. new_data = copy.copy(data)
  27. new_scratch.append(data[j])
  28. new_data = data[j+1:]
  29. yield new_scratch
  30. yield from allCombos(new_scratch,new_data) #recursive call woah boy
  31.  
  32. scratch = []
  33. data = [1,2,3,4,5]
  34.  
  35. for combo in allCombos(scratch,data):
  36. print(combo)
  37.  
  38. comboList = [x for x in allCombos(scratch, data)]
  39. print(comboList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement