Skip to content

Instantly share code, notes, and snippets.

@reeddunkle
Last active July 26, 2016 20:44
Show Gist options
  • Select an option

  • Save reeddunkle/84cdd2dd2306be975b594d61cb677dae to your computer and use it in GitHub Desktop.

Select an option

Save reeddunkle/84cdd2dd2306be975b594d61cb677dae to your computer and use it in GitHub Desktop.
Reed and Leo: Almost Purely Functional Python
def nest_while(f, value, condition):
'''
Given function, initial value, and break condition,
calls function with the initial value, and repeats calling
the function with the return value of the previous call,
until the condition returns false.
'''
if not condition(value):
return value
return nest_while(f, f(value), condition)
def cut_sticks(sticks):
'''
Given list of sticks, cut them by the shortest and yield
the remaining sticks.
'''
print(len(sticks))
return [stick - shortest for shortest in [min(sticks)] for stick in sticks if stick > shortest]
if __name__ == '__main__':
num_sticks = int(input())
sticks = [int(s) for s in input().split()]
nest_while(f=cut_sticks, value=sticks, condition=lambda c: len(c) > 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment