Skip to content

Instantly share code, notes, and snippets.

@r14c
Last active August 29, 2015 14:07
Show Gist options
  • Save r14c/769bdf10a1142ef1d517 to your computer and use it in GitHub Desktop.
Save r14c/769bdf10a1142ef1d517 to your computer and use it in GitHub Desktop.
Restricted Combinations
# -*- coding: utf-8 -*-
"""
rcombinations.py
~~~~~~~~~~~~~~~~
Implements different a restricted combination function.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
"""
__author__ = "Carlos Killpack"
__email__ = "carlos.killpack@rocketmail.com"
__license__ = "Unlicense <http://unlicense.org>"
from itertools import chain, izip
def rcombinations(values=None):
length = len(values)
for combination in set(chain(fill(values=values, length=length),
drain(values=values, length=length),
hit(values=values, length=length),
miss(values=values, length=length),
alt2(values=values, length=length))):
yield combination
def fill(values=None):
length = len(values)
for index in range(length):
yield tuple(chain(values[0:index], (None for _ in range(length - index))))
yield tuple(i for i in values)
def drain(values=None):
length = len(values)
for index in reversed(range(length)):
if length - index == length:
pass
else:
yield tuple(chain((None for _ in range(length - index)), values[-index:]))
yield tuple(None for _ in range(length))
def alt2(values=None):
length = len(values)
combination_set = set()
for i in range(length):
for j in range(length - 1):
combination = [None for _ in range(length)]
if i == j + 1:
pass
else:
combination[i] = values[i]
combination[j + 1] = values[j + 1]
combination_set.update([tuple(combination)])
for combination in combination_set:
yield tuple(combination)
def alt3(values=None):
length = len(values)
combination_set = set()
for i in range(length):
for j in range(length - 1):
combination = [None for _ in range(length)]
try:
combination[j] = values[j]
combination[j + 1] = values[j + 1]
combination[j + 2] = values[j + 2]
combination_set.update([tuple(combination)])
except IndexError:
pass
for combination in combination_set:
yield tuple(combination)
def cluster(values=None):
length = len(values)
def _cluster(size):
print size
for i, j in izip(range(length), range(size, length + size)):
if j > len(values):
group = list(values[:j - len(values)])
group.extend(values[i:len(values)])
else:
group = values[i:j]
print group
for combination in chain(*[_cluster(size + 1) for size in range(length - 1)]):
yield combination
def hit(values=None):
length = len(values)
for index in range(length):
combination = [None for _ in range(length)]
combination[index] = values[index]
yield tuple(combination)
def miss(values=None):
length = len(values)
for index in range(length):
combination = [value for value in values]
combination[index] = None
yield tuple(combination)
# -*- coding: utf-8 -*-
from setuptools import setup
setup(name="rcombinations",
version="0",
author="Carlos Killpack",
author_email="carlos.killpack@rocketmail.com",
license="Public Domain",
py_modules=("rcombinations", ),
classifiers=("Topic :: Utilities",
"License :: Public Domain", ))
# -*- coding: utf-8 -*-
from unittest import TestCase
from itertools import chain
from rcombinations import fill, drain, hit, miss
class TestRCombinations(TestCase):
def setUp(self):
self.values = ('a', 'b', 'c', 'd', 'e')
self.fill_result = [(None, None, None, None, None),
('a', None, None, None, None),
('a', 'b', None, None, None),
('a', 'b', 'c', None, None),
('a', 'b', 'c', 'd', None),
('a', 'b', 'c', 'd', 'e')]
self.drain_result = [(None, 'b', 'c', 'd', 'e'),
(None, None, 'c', 'd', 'e'),
(None, None, None, 'd', 'e'),
(None, None, None, None, 'e'),
(None, None, None, None, None)]
self.hit_result = [('a', None, None, None, None),
(None, 'b', None, None, None),
(None, None, 'c', None, None),
(None, None, None, 'd', None),
(None, None, None, None, 'e')]
self.miss_result = [(None, 'b', 'c', 'd', 'e'),
('a', None, 'c', 'd', 'e'),
('a', 'b', None, 'd', 'e'),
('a', 'b', 'c', None, 'e'),
('a', 'b', 'c', 'd', None)]
self.rcombinations_result = list(chain(self.fill_result,
self.drain_result,
self.hit_result,
self.miss_result))
def test_fill(self):
self.assertEqual(list(fill(values=self.values)), self.fill_result)
def test_drain(self):
self.assertEqual(list(drain(values=self.values)), self.drain_result)
def test_hit(self):
self.assertEqual(list(hit(values=self.values)), self.hit_result)
def test_miss(self):
self.assertEqual(list(miss(values=self.values)), self.miss_result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment