# Netease Youdao 2017 internal programming shuffle (Python)

Time：2019-10-20

The example of this article shares Netease Youdao 2017 internal programming problem: shuffle, for your reference. The specific content is as follows

”’
[programming question] shuffle
Time limit: 1 second
Space limit: 32768k
Shuffling is very common in our life. Now we need to write a program to simulate the process of shuffling. Now we need to shuffle 2n cards.
From top to bottom is the first, the second, the third all the way to 2n. First of all, let’s divide these 2n cards into two piles.
The left hand holds sheets 1 to n (upper half pile), and the right hand holds sheets n + 1 to 2n (lower half pile).
Then we start the process of shuffling. First, we put down the last card of the right hand, and then the last card of the left hand.
Then put down the second card from the bottom of the right hand, and then the second card from the bottom of the left hand, until finally the first card from the left hand.
Then combine the cards. For example, there are six cards. The first card sequence is 1, 2, 3, 4, 5, 6. First, they were divided into two groups.
The left hand holds 1,2,3; the right hand holds 4,5,6. 6, 3, 5, 2, 4, 1 are put down in sequence during shuffling.
After the six cards are combined into a group of cards again, we look at the group of cards from the top down, and it becomes the sequence 1, 4, 2, 5, 3, 6.
Now given an original deck, please output the sequence from top to bottom after this deck shuffles K times.

Enter a description:

The number T (t ≤ 100) in the first row indicates the number of data groups. For each group of data, the first row has two numbers n, K (1 ≤ n, K ≤ 100).
The next line has 2n numbers A1, A2,…, A2N (1 ≤ AI ≤ 1000000000). Represents the top-down sequence of the original deck.

Output Description:

For each set of data, output a row, the final sequence. Separate numbers with spaces. Do not output extra spaces at the end of the line.

Input example 1:

3 3 1 1 2 3 4 5 6 3 2 1 2 3 4 5 6 2 2 1 1 1 1

Output example 1:

1 4 2 5 3 6 1 5 4 3 2 6 1 1 1 1

”’

”’
Solutions:Hash table
This question is an internal extrapolation question, so the test point is related to the dictionary data set. This question is a hash table. If you put the new card into array 1,
If the shuffled cards are put into array 2, there is a certain mapping relationship between the positions of each element in array 2. Namely:
The position of the element at position J in array I in array II is index = (2J% len (array II)),
If the location already has elements, index will automatically add one to search for the next location until an empty location is found.
The data of this topic is relatively simple. In fact, there is no need to do it in a hash table. You can do it by finding rules or slicing.
Many people use slicing. Use [:: 2] and [1:: 2] to find out all even and odd digits in sequence 2. Put in the first N and last n digits of sequence 1.
But I personally think the efficiency of Python slicing is not high, so I changed the way to find the rule. If 2J < 2n, then index = 2J, otherwise index = index% (2 * n) + 1.
Cycle K times like this. Using this method, the position of a card after K shuffles is directly found each time, avoiding a large number of slicing operations.
It also avoids a large number of operations to find and assign elements in the array.
(PS, input and output comparison of this inverse problem)
”’

”’
Code run result:
Correct answer: Congratulations! The program you submitted passed all the test cases
”’

``````
T = int(input())

array_list = []
n, k = [int(each) for each in input().split()]
for x in range(T):
digs = [int(each) for each in input().split()]
array = digs[0:2*n]
results = [None] * (2 * n)
for j in range(2*n):
index = j
for i in range(k):
index = 2 * index
if index >= 2 * n:
index = index % (2*n) + 1
results[index] = array[j]
n, k = digs[-2:]
results = map(str, results)
print(' '.join(results))``````