-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
56 lines (49 loc) · 1.53 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#dependencies
import numpy as np
import matplotlib.pyplot as plt
#functions
def split_deck(deck):
deck1 = deck[0:len(deck)//2]
deck2 = deck[len(deck)//2:]
return deck1, deck2
def riffle_shuffle(deck, deck1, deck2):
shuffled = np.zeros_like(deck)
for i in range(len(deck2)):
shuffled[2*i] = deck1[i]
shuffled[2*i+1] = deck2[i]
return shuffled
#main function
x = int(input("Enter max deck numbers you want to find up to: ")) # insert large number to visualize trends
deck_sizes = []
riffle_counts = []
for i in range(2,x+2,2):
deck_num = i
deck = np.arange(1,deck_num+1,1)
deck1, deck2 = split_deck(deck)
shuffled = riffle_shuffle(deck, deck1, deck2)
count = 1
if ((shuffled == deck).all()):
print(i, count)
continue
while(count > 0):
deck1, deck2 = split_deck(shuffled)
shuffled = riffle_shuffle(shuffled,deck1,deck2)
count += 1
if((shuffled==deck).all()):
break
print(i, count) #output corresponding deck size and number of riffle shuffles to return original position
deck_sizes.append(i)
riffle_counts.append(count)
# plot results
x = np.linspace(0, x)
y = x/2
y2 = x/3
plt.plot(x, x, label='x=y')
plt.plot(x, y, label='x=1/2*y')
plt.plot(x, y2, label='x=1/3*y')
plt.scatter(deck_sizes, riffle_counts, marker='o', label="Data points", s=5, c='r')
plt.xlabel('Deck Size')
plt.ylabel('Number of Riffle Shuffles')
plt.title('Number of Riffle Shuffles Required for Different Deck Sizes')
plt.legend(loc='best')
plt.show()