Last active
July 26, 2016 20:44
-
-
Save reeddunkle/84cdd2dd2306be975b594d61cb677dae to your computer and use it in GitHub Desktop.
Reed and Leo: Almost Purely Functional Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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