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 max_area(L): | |
| L.sort() | |
| left, right = 0,0 | |
| while len(L) > 0: | |
| # if the left is shorter, put the twig over there | |
| if left < right: | |
| left += L.pop() | |
| # if the right is shorter, put it on the right |
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 memoize(F): | |
| # A hash table for storing previous states | |
| hashed_states = {} | |
| def memoized_F(*args): | |
| # if we haven't memoized this state, calculate | |
| # it. Then return it | |
| if args not in hashed_states: | |
| hashed_states[args] = F(*args) |
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
| class typecheck: | |
| def __init__(self, *args): | |
| self.types = args | |
| def __call__(self, F): | |
| # create a new function that checks | |
| # the function types first | |
| def typechecked_F(*args): | |
| for expected_type, arg in zip(self.types, args): |
NewerOlder