如何概括澳大利亚洗牌?


def australian(deck,n):
i=0
s=deck
if len(deck) < 12:
while i < n :
i=i+1
s=s[0::2]+s[1::4]+s[3::4]
s=s[::-1]
return(s)
if len(deck) < 26:
while i<n:
i=i+1
s=s[0::2]+s[1::4]+s[3::8]+s[7::8]
s=s[::-1]
return(s)
if len (deck) < 48:
while i<n:
i=i+1
s=s[0::2]+s[1::4]+s[3::8]+s[7:8]+s[15:16]
s=s[::-1]
return(s)

这是我对澳大利亚洗牌的第一个代码。我想推广它,使它适用于更大的桥牌。

我写了第二段代码,其中一个方法应该可以工作,但是我不知道如何将它放入循环中,以便它可以处理所有组合。

def Down_Under_shufffle(lst):
basic=lst[0::2]#les impaires 
length = len(basic)
middle_index = length//2#list/4 :: middle index = 4
s=basic[:middle_index]
s1=s[:2]
s2=s1[:1]
s3=s2[:1]
a = [element * (2**1) for element in s]#multiplie les elemnts 
a1= [element * (2**2) for element in s1]
a2 = [element * (2**3) for element in s2]
a3= [element * (2**4) for element in s3]
toto=basic+a+a1+a2+a3
toto=toto[::-1]
return(toto)
print(Down_Under_shufffle([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]))

我认为整个事情可以简化。下面有两种方法

一个一个地处理arr中的项:

def down_under_shuffle_items( arr ):
a = arr.copy()   # arr would be destroyed without this step.
result = []
while len( a ) > 1:  
result.append( a.pop( 0 ) )  # Remove the top item and append to result
a.append( a.pop( 0 ) )       # Remove the new top item and append to the end of a
return result + a                # If a is empty this adds nothing

这处理所有偶数项,对我来说[0::2]是偶数,并从a中删除它们。然后,只要a中有不止一个元素,它就继续循环。如果a的长度小于dealt的长度,a中的第一张卡需要被删除,然后添加到a的末尾。当我测试它时,这更快。

def down_under_shuffle( arr ):
a = arr.copy()   # arr would be destroyed without this step 
result = []
while len( a ) > 1:              # Loop while a isn't empty
dealt = a[ :: 2 ]            # deal the even cards
a = a[ 1::2 ]                # Keep the odd cards
result += dealt              # add dealt cards to result
if len( a ) < len( dealt ):  # If the last card was dealt, move a[0] to a[-1], rotating a
a.append( a.pop(0) )
return result + a                # If a is empty this adds nothing

结果:

deck = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ]
down_under_shuffle_items( deck )                                                                    
# [0, 2, 4, 6, 8, 10, 1, 5, 9, 3, 11, 7]
down_under_shuffle( deck )                                                                            
# [0, 2, 4, 6, 8, 10, 1, 5, 9, 3, 11, 7]

最新更新