/fill
Created
November 19, 2014 15:39
Revisions
-
JosephSalisbury created this gist
Nov 19, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ """ 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()