Skip to content

Instantly share code, notes, and snippets.

@ethanagb
Created June 7, 2019 13:01
Show Gist options
  • Save ethanagb/a8bd7896b56e11ae9969be0cecf61da6 to your computer and use it in GitHub Desktop.
Save ethanagb/a8bd7896b56e11ae9969be0cecf61da6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import copy
def allCombos(scratch, data):
'''
Returns all possible combinations of objects in an array
Args:
----
scratch - an empty array
data - the array to choose from
Returns:
----
None
Yields:
------
A generator function for returning all combinations.
'''
for j in range(len(data)):
new_scratch = copy.copy(scratch)
new_data = copy.copy(data)
new_scratch.append(data[j])
new_data = data[j+1:]
yield new_scratch
yield from allCombos(new_scratch,new_data) #recursive call woah boy
scratch = []
data = [1,2,3,4,5]
for combo in allCombos(scratch,data):
print(combo)
comboList = [x for x in allCombos(scratch, data)]
print(comboList)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment