'How to run the function on this array until I get the particular result?
I have a code to shuffle an array. I want to keep running the code on the array until the resultant array is equal to input array.
def riffle(deck):
new_deck = []
deck_1 = deck[:len(deck)//2]
deck_2 = deck[len(deck)//2::]
for i in range(len(deck)//2):
new_deck.append(deck_1[i])
new_deck.append(deck_2[i])
if len(deck) % 2 == 1:
new_deck.append(deck[-1])
print(new_deck)
arr = [i for i in range(1,11)]
#t1 =time.time()
riffle(arr)
#t2 = time.time()
#
original_arr = arr[:]
k = 0
while 1:
riffle(arr)
if arr == original_arr:
break
k+= 1
print(k)
By running this code I am getting the output.
[1, 6, 2, 7, 3, 8, 4, 9, 5, 10]
[1, 6, 2, 7, 3, 8, 4, 9, 5, 10]
0
Is this some fault in the while loop or the function itself? Please help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
