|
| 1 | +import functools |
| 2 | + |
| 3 | +# Create a funtion that executed f recusively n times, i.e. f**n |
| 4 | +def power(f, n): |
| 5 | + functions = [f for _ in range(n)] |
| 6 | + def compose2(f, g): |
| 7 | + return lambda x: f(g(x)) |
| 8 | + return functools.reduce(compose2, functions, lambda x: x) |
| 9 | + |
| 10 | +def riffle_shuffle(input): |
| 11 | + left = input[0:len(input)//2] |
| 12 | + right = input[len(input)//2:] |
| 13 | + i = 0 |
| 14 | + out = "" |
| 15 | + while i < len(input)//2: |
| 16 | + out += right[i] + left[i] |
| 17 | + i += 1 |
| 18 | + return out |
| 19 | + |
| 20 | +values = [ |
| 21 | + "alice123----------------", # 0 |
| 22 | + "alice485----------------", # 1 |
| 23 | + "aliceimwunderland521----", # 2 |
| 24 | + "bob1--------------------", # 3 |
| 25 | + "bob123------------------", # 4 |
| 26 | + "bob485------------------", # 5 |
| 27 | + "bob511------------------", # 6 |
| 28 | + "creator-----------------", # 7 |
| 29 | +] |
| 30 | + |
| 31 | +transform = power(riffle_shuffle, 18) |
| 32 | +transformed = [transform(v) for v in values] |
| 33 | + |
| 34 | +print("Original:\n" + "\n".join(sorted(values))) |
| 35 | +print() |
| 36 | +print("Shuffled:\n" + "\n".join(sorted(transformed))) |
0 commit comments