Skip to content

Instantly share code, notes, and snippets.

@whitekid
Last active October 18, 2016 01:57
Show Gist options
  • Save whitekid/68dd57b44d737043827b08ea955a0c4c to your computer and use it in GitHub Desktop.
Save whitekid/68dd57b44d737043827b08ea955a0c4c to your computer and use it in GitHub Desktop.
"""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