MCPcopy Create free account
hub / github.com/ActiveState/code / rotations

Function rotations

recipes/Python/577574_PythInfinite_Rotations/recipe-577574.py:6–11  ·  view source on GitHub ↗

rotations([0,1,2]) --> [[0, 1, 2], [1, 2, 0], [2, 0, 1]]

(it)

Source from the content-addressed store, hash-verified

4# First a naive approach. At each generation we pop the first element and append
5# it to the back. This is highly memmory deficient.
6def rotations(it):
7 """ rotations([0,1,2]) --> [[0, 1, 2], [1, 2, 0], [2, 0, 1]] """
8 l = list(it)
9 for i in range(len(l)):
10 yield iter(l)
11 l = l[1:]+[l[0]]
12
13# A much better approach would seam to be using a deque, which rotates in O(1),
14# However this does have the negative effect, that generating the next rotation

Callers

nothing calls this directly

Calls 6

listClass · 0.85
rangeFunction · 0.85
tailsFunction · 0.85
cycleFunction · 0.85
dequeClass · 0.50
rotateMethod · 0.45

Tested by

no test coverage detected