Skip to content

Instantly share code, notes, and snippets.

@jcollard
Last active January 12, 2018 17:14
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 jcollard/9afea9616f79b95f56370c35225200cb to your computer and use it in GitHub Desktop.
Save jcollard/9afea9616f79b95f56370c35225200cb to your computer and use it in GitHub Desktop.
def buildGroups(list, conditionChecker):
'''
Takes in a list and compiles a set of groups based on the condition checker
@param list - a list of items to be compiled into groups
@param conditionChecker - the condition checker which takes in the current group being built,
the list of groups built thus far and the current item being inspected.
If the item should be added to the current group, it returns true.
Otherwise, the current group is added to the list of groups and the item is skipped.
@return returns a list of compiled groups
'''
groups = []
group = []
for item in list:
if coniditionChecker(group, groups, item):
group.append(item)
else:
groups.append(group)
group = []
groups.append(group)
return groups
list = ['Harry', 'Sally', 'NEXT', 'Bob', 'Marie', 'Jason', 'NEXT', 'Joe']
# Searches for the word NEXT
def findNext(group, groups, item):
return item == 'NEXT'
groups = buildGroups(list, findNext)
# Creates groups of size 3
def groupsOfThree(group, groups, item):
return len(group) >= 3:
groups3 = buildGroups(group, groups, item):
# Each group is based on the size of the total number of previous groups created
def groupsNPlusOne(group, groups, item):
return len(group) >= len(groups) + 1
nPlusOne = groupsNPlusOne(group, groups, item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment