Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize shuffle Mini Program with python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces python how to achieve shuffle Mini Program, the article is very detailed, has a certain reference value, interested friends must read it!

[programming title] shuffle

Time limit: 1 second

Space limit: 32768K

Shuffling is very common in life. Now we need to write a program to simulate the shuffling process. Now we need to shuffle 2n cards.

From top to bottom are the first, the second, and the third to the 2n. First of all, let's divide the 2n cards into two piles.

Hold the first to the nth sheet (the top half of the pile) in the left hand and the n + 1 to the 2n sheet (the bottom half) in the right hand.

Then begin the process of shuffling, first put down the last card in the right hand, and then put down the last card in the left hand

Then put down the penultimate card of the right hand, then put down the penultimate card of the left hand, until finally put down the first card of the 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, 4, 5 and 6. First, it is divided into two groups.

In the left hand, he holds 1, 2, and 3, and in the right, he holds 4, 5, and 6. In the process of shuffling the cards were put down in order: 6, 3, 5, 2, 4, 1.

After putting the six cards into a set of cards again, we look at the set of cards from top to bottom, and we turn them into a sequence 1, 4, 4, 2, 5, 5, 3, and 6.

Now give an original card group, please output the sequence from top to bottom after this deck is shuffled k times.

Enter a description:

The first row is a number T (T ≤ 100), indicating the number of data groups. For each set of data, there are two numbers in the first row, n ≤ k (1 ≤ n memory k ≤ 100)

There are 2n in the next line: a 1 ≤ ai ≤ 1000000000. Represents the sequence from top to bottom of the original brand group.

Output description:

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

Enter example 1:

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

Output example 1:

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

''

''

Problem-solving idea: hash table

This question is an internal extrapolation of youdao, so the test site is related to the dictionary data set, this question is a hash table, if you put the new card into array one

If the shuffled cards are put into array two, then there is a certain mapping relationship between the positions of each element of the array. That is:

The position of the element at the j sign in array one is index = (2J% len (array two)) in array two.

If there is already an element in that location, index automatically increments one 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 use a hash table, relying on finding rules or slicing can be done.

Many people use slicing, using [:: 2] and [1Groupe 2] to find out all the even and odd digits in sequence 2, and put the first n and the last n in sequence one.

But I personally think that the efficiency of python slicing is not high, so I changed to find a regular method, if 2J

< 2n , 则index=2j,否则index = index % (2*n) + 1, 如此循环k次。使用这种方法每次都直接找到某张牌在k次洗牌后的位置,避免了大量的切片操作, 也避免了大量在数组中寻找元素并赋值的操作。 (Ps,这倒题目的输入和输出比较坑爹) ''' ''' 代码运行结果: 答案正确:恭喜!您提交的程序通过了所有的测试用例 ''' 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 >

+ 1 results [index] = array [j] n, k = digs [- 2:] results = map (str, results) print ('. Join (results)) these are all the contents of the article "how python implements shuffle Mini Program". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report