Skip to content

Commit

Permalink
Add simulation script
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Oct 1, 2020
1 parent 89b69ef commit e65ba57
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ insert_final_newline = true

[*.rs]
indent_size = 4

[*.py]
indent_size = 4
36 changes: 36 additions & 0 deletions docs/simulate_riffle_shuffle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import functools

# Create a funtion that executed f recusively n times, i.e. f**n
def power(f, n):
functions = [f for _ in range(n)]
def compose2(f, g):
return lambda x: f(g(x))
return functools.reduce(compose2, functions, lambda x: x)

def riffle_shuffle(input):
left = input[0:len(input)//2]
right = input[len(input)//2:]
i = 0
out = ""
while i < len(input)//2:
out += right[i] + left[i]
i += 1
return out

values = [
"alice123----------------", # 0
"alice485----------------", # 1
"aliceimwunderland521----", # 2
"bob1--------------------", # 3
"bob123------------------", # 4
"bob485------------------", # 5
"bob511------------------", # 6
"creator-----------------", # 7
]

transform = power(riffle_shuffle, 18)
transformed = [transform(v) for v in values]

print("Original:\n" + "\n".join(sorted(values)))
print()
print("Shuffled:\n" + "\n".join(sorted(transformed)))

0 comments on commit e65ba57

Please sign in to comment.