Skip to content

Instantly share code, notes, and snippets.

@theelous3
Last active July 31, 2018 15:07
Show Gist options
  • Select an option

  • Save theelous3/ebdfdd501ed494557829cc6d10297168 to your computer and use it in GitHub Desktop.

Select an option

Save theelous3/ebdfdd501ed494557829cc6d10297168 to your computer and use it in GitHub Desktop.
from itertools import cycle, islice
import pprint
# one cycle of the group
def increasing_cyclical(xs, period, max_width):
"""
Find the subcycles of an iterable given a depth and breadth.
period: the depth of the subcycle
max_width: the breadth of the subcycle
"""
for x in range(period):
g = list(islice(cycle(xs), period+x))[x:]
for y in range(max_width):
yield list(islice(iter(g), y+1))
# cycle the group forever
def increasing_cyclical(xs, period, max_width):
while True:
for x in range(period):
g = list(islice(cycle(xs), period+x))[x:]
for y in range(max_width):
yield list(islice(iter(g), y+1))
# Example output:
>>> pprint(list(increasing_cyclical('abcd', 3, 1)))
[['a'], ['b'], ['c']]
>>> pprint(list(increasing_cyclical('abcd', 4, 1)))
[['a'], ['b'], ['c'], ['d']]
>>> pprint(list(increasing_cyclical('abcd', 4, 2)))
[['a'], ['a', 'b'], ['b'], ['b', 'c'], ['c'], ['c', 'd'], ['d'], ['d', 'a']]
>>> pprint(list(increasing_cyclical('abcd', 4, 3)))
[['a'],
['a', 'b'],
['a', 'b', 'c'],
['b'],
['b', 'c'],
['b', 'c', 'd'],
['c'],
['c', 'd'],
['c', 'd', 'a'],
['d'],
['d', 'a'],
['d', 'a', 'b']]
>>> pprint(list(increasing_cyclical('abcd', 4, 4)))
[['a'],
['a', 'b'],
['a', 'b', 'c'],
['a', 'b', 'c', 'd'],
['b'],
['b', 'c'],
['b', 'c', 'd'],
['b', 'c', 'd', 'a'],
['c'],
['c', 'd'],
['c', 'd', 'a'],
['c', 'd', 'a', 'b'],
['d'],
['d', 'a'],
['d', 'a', 'b'],
['d', 'a', 'b', 'c']]
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment