NetEase Channel 2017 Push Programming Shuffle (python)

  • 2021-06-29 11:18:27
  • OfStack

The example in this paper shares with you the NetEase Introduced Programming Question of 2017: shuffling, for your reference, the specific content is as follows

'''
Shuffling
Time limit: 1 second
Space limit: 32768K
Shuffling is common in life with 10 points. Now you need to write a program to simulate the process of shuffling.Now you need to wash 2n cards.
From top to bottom are sheets 1, 2, 3 1 to 2n.First, we divide the 2n cards into two stacks.
Hold the first to n sheets in your left hand (top heap), the n+1 sheets in your right hand (bottom heap) to the second n sheets.
Then start the shuffling process, first drop the last card of your right hand, then the last card of your left hand.
Next put down the second last card of your right hand, then the second last card of your left hand until you finally put down the first card of your left hand.
Then merge the cards together.For example, there are six cards, and the sequence of the first cards is 1,2,3,4,5,6.First divided into two groups,
Hold 1,2,3 in your left hand;Hold 4,5,6 in your right hand.6,3,5,2,4,1 were dropped in sequence during the shuffle.
When these six cards are combined into a group again, we look at the group from top to bottom and it becomes a sequence of 1,4,2,5,3,6.
Now give a group of original cards. Output the sequence from top to bottom after the k shuffle.

Enter a description:

Line 1, number T (T < 100), represents the number of data groups.For each set of data, the first row contains two numbers n, k (1 < n, k < 100).
The next row has 2 n numbers a1, a2,..., a2n (1 < ai < 1000000000).Represents the top-down sequence of the original deck.

Output description:

For each set of data, output 1 row, the final sequence.Separate the numbers with spaces, and 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

'''

'''
Solution ideas: Hash list
This question is a fairly intrinsic one, so it's about dictionary datasets and hash lists. If you put a new card in Array 1,
When shuffled cards are placed in Array 2, there is a mapping relationship between the positions of each element in Array 1 and Array 2.That is:
The element in Array 1 at position j is index = (2j% len (Array 2)) in Array 2.
If the location already has elements, index automatically adds 1 and searches 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 looking for rules or slices.
Many people use slices, use [::2] and [1::2] to find all even and odd digits in column 2, and put in the first n and the last n of column 1.
But I personally think python slicing is not efficient, so I switched to a regular method, if 2j < 2n, index = 2j, otherwise index = index% (2*n) + 1,
This cycle k times.Use this method to find a card's position directly after the k shuffle each time, avoiding a lot of slicing operations.
It also avoids the large number of operations of finding and assigning elements to arrays.
(Ps, input and output comparison for this inversion)
'''

'''
Code run results:
The answer is correct: 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))

Related articles: