Created
January 22, 2016 16:35
-
-
Save mdellavo/46455bd4567c3e981169 to your computer and use it in GitHub Desktop.
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 divide(collection, decider): | |
""" | |
Divide the items of collection into A and B according to decider(item) | |
:param collection: a sequence of items | |
:param decider: a function that takes an item and returns true of false | |
:return: a pair of lists (A, B) | |
>>> divide(range(10), lambda x: x%2 == 0) | |
([0, 2, 4, 6, 8], [1, 3, 5, 7, 9]) | |
""" | |
a = [] | |
b = [] | |
for item in collection: | |
target = a if decider(item) else b | |
target.append(item) | |
return a, b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment