-
-
Save JosephSalisbury/ddaf64e240467db1775b to your computer and use it in GitHub Desktop.
Fill up a container, to some numerical limit, from another container dependent on some predicate between the item and the dependent container.
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
""" Fill up a container, to some numerical limit, from another container | |
dependent on some predicate between the item and the dependent | |
container. """ | |
from itertools import takewhile | |
import unittest | |
def fill(source, destination, limit, predicate): | |
iterator = source.__iter__() | |
while len(destination) != limit: | |
item = iterator.next() | |
if predicate(destination, item): | |
destination.append(item) | |
class TestUniqueFill(unittest.TestCase): | |
def setUp(self): | |
self.source = ['a', 'c', 'a', 'b', 'a', 'c', 'd'] | |
self.destination = [] | |
def test_fill(self): | |
fill(self.source, self.destination, 3, lambda d, i: i not in d) | |
self.assertEquals( | |
self.destination, | |
['a', 'c', 'b'] | |
) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment