Created
August 1, 2013 16:38
-
-
Save knzm/6133057 to your computer and use it in GitHub Desktop.
This file contains 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 _chunker(it): | |
while True: | |
yield (it.next(), it.next()) | |
def chunker(seq): | |
return map(list, _chunker(iter(seq))) | |
import unittest | |
class ChunkerTest(unittest.TestCase): | |
def test_balance(self): | |
result = chunker([1, 2, 3, 4, 5, 6]) | |
expected = [[1, 2], [3, 4], [5, 6]] | |
self.assertEqual(result, expected) | |
def test_unbalance(self): | |
result = chunker([1, 2, 3, 4, 5]) | |
expected = [[1, 2], [3, 4]] | |
self.assertEqual(result, expected) | |
def test_empty(self): | |
result = chunker([]) | |
expected = [] | |
self.assertEqual(result, expected) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment