rotations([0,1,2]) --> [[0, 1, 2], [1, 2, 0], [2, 0, 1]]
(it)
| 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. |
| 6 | def 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 |