Skip to content

Commit e65ba57

Browse files
committed
Add simulation script
1 parent 89b69ef commit e65ba57

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ insert_final_newline = true
99

1010
[*.rs]
1111
indent_size = 4
12+
13+
[*.py]
14+
indent_size = 4

docs/simulate_riffle_shuffle.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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

Comments
 (0)