-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaro.py
More file actions
88 lines (73 loc) · 2.78 KB
/
Copy pathFaro.py
File metadata and controls
88 lines (73 loc) · 2.78 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from LoopChain import methodLoop
from LoopChain import methodLoopLen
"""
TODO: test
"""
def positionSort(initialList: list, sortList: list):
"""
Sorts a list using a second list with the same length. The second list indicates where entries in the first list go.
:param initialList: A list to be sorted.
:param sortList: A list consisting of the integers in range(len(initialList)) each appearing once.
:return: Sorted list, or None if the lengths do not match.
"""
if len(initialList) == len(sortList):
listLen = len(initialList)
retList = [initialList[i] for i in sortList]
return retList
else:
return None
def inFaroGen(deckSize: int):
"""
Generates a list for an inside Faro shuffle with a given deck size.
:param deckSize: int; must be greater than 2 and even.
:return: list for inside Faro shuffle, or None for an invalid deck size.
"""
if deckSize % 2 == 0 and deckSize > 2:
retList = []
halfDeckSize = int(deckSize / 2)
for i in range(halfDeckSize):
retList.append([i, i + halfDeckSize])
return retList
else:
return None
def outFaroGen(deckSize: int):
"""
Generates a list for an outside Faro shuffle with a given deck size.
:param deckSize: int; must be greater than 2 and even.
:return: list for outside Faro shuffle, or None for an invalid deck size.
"""
if deckSize % 2 == 0 and deckSize > 2:
retList = []
halfDeckSize = int(deckSize / 2)
for i in range(halfDeckSize):
retList.append([i + halfDeckSize, i])
return retList
else:
return None
def faroShuffleChain(initialList: list, shuffleSequence: list) -> list:
"""
Sorts a list based on repeated Faro shuffles using the sequence given.
:param initialList: List to shuffle.
:param shuffleSequence: Sequence to use; list with entries that are of the form 'I', 'O', 'In', or 'On',
where n is the number of shuffles in a row to do.
:return: shuffled list
"""
inSortList = inFaroGen(len(initialList))
outSortList = outFaroGen(len(initialList))
retList = initialList
for i in shuffleSequence:
faroNum = 1
faroType = i
if len(i) != 1:
faroNum = int(i[1:])
faroType = i[1]
for j in range(faroNum):
if faroType == 'i' or faroType == 'I':
retList = positionSort(retList, inSortList)
elif faroType == 'o' or faroType == 'O':
retList = positionSort(retList, outSortList)
return retList
def faroChainLen(deckSize: int, shuffleSequence: list):
initialList = [i for i in range(deckSize)]
retNum = methodLoopLen(initialList, lambda x: faroShuffleChain(x, shuffleSequence))
return retNum