Skip to content

Instantly share code, notes, and snippets.

@jacksongabbard
Created May 28, 2016 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacksongabbard/aaec562105f75f5d2d0d3c4da35dae8e to your computer and use it in GitHub Desktop.
Save jacksongabbard/aaec562105f75f5d2d0d3c4da35dae8e to your computer and use it in GitHub Desktop.
import itertools
def findDinnerParty(friends, tableSize):
groups = combineFriends(friends, tableSize)
print "%d groups" % len(groups)
print groups
recursions = 0;
def combineFriends(friends, tableSize, groups=[], group=[], pos=0):
global recursions
recursions = recursions + 1
if len(group) == tableSize:
groups.append(group)
elif pos < len(friends):
# skip
combineFriends(friends, tableSize, groups, group, pos + 1)
# take
newGroup = list(group)
newGroup.append(friends[pos])
combineFriends(friends, tableSize, groups, newGroup, pos + 1)
return groups
friends = ['Fred', 'Paresh', 'Tom', 'Greenie', 'April', 'Dick', 'Harry', 'Lucifer']
findDinnerParty(friends, 8)
print "Btw, here's how much recurses, lol %d" % recursions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment