Last active
October 18, 2016 01:57
-
-
Save whitekid/68dd57b44d737043827b08ea955a0c4c 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
"""Just simple combination | |
todo recursive call s should be call by reference | |
""" | |
import itertools | |
import string | |
import unittest | |
def combination(s, n): | |
if n < 1: | |
yield () | |
elif n == 1: | |
for i in s: | |
yield i, | |
else: | |
for i in xrange(len(s)): | |
for j in combination(s[i+1:], n-1): | |
yield (s[i],) + j | |
class CombiTest(unittest.TestCase): | |
def setUp(self): | |
self.data = string.lowercase | |
def test_compare_with_itertools(self): | |
for x in range(5): | |
sol = tuple(combination(self.data, x)) | |
exp = tuple(itertools.combinations(self.data, x)) | |
self.assertEqual(exp, sol) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment