彩票插入排序算法
Posted
技术标签:
【中文标题】彩票插入排序算法【英文标题】:lottery insertion sort algorithm 【发布时间】:2020-09-24 16:16:55 【问题描述】:在我的彩票游戏中,我需要创建 1000 名玩家,每个玩家都有一个号码集,需要将其与中奖号码集进行比较。所有数字集都来自一桶 0-30 数字。这部分我已经完成了,但现在我需要将球员号码分为两组,分别称为 PWN 和 SWN。 PWN 是前 6 个号码,SWN 是后两个号码。我需要使用插入排序对 PWN 进行排序,使用选择排序对 SWN 进行排序。我还没有参加 SWN 部分,因为我仍然坚持试图弄清楚 PWN 排序。
for example my program create something like below
Winning number
19, 27, 23, 18, 1, 3, 2, 24
Player ID | Player Numbers
1 | 8, 14, 3, 4, 10, 21, 2, 22
2 | 13, 22, 9, 11, 23, 29, 16, 27
3 | 10, 25, 22, 26, 8, 5, 23, 18
but I need to sort them like
Winning number
PWN | SWN
19, 27, 23, 18, 1, 3 | 2, 24
Player ID | Player Numbers PWN | Player Numbers SWN
1 | 8, 14, 3, 4, 10, 21 | 2, 22
2 | 13, 22, 9, 11, 23, 29 | 16, 27
3 | 10, 25, 22, 26, 8, 5 | 23, 18
import random
#main code to generate data in the program starts here
def generateRandomNumber():
randomNumber = random.randint(0,30) #can return numbers between 0 and 30 including 0 and 30
return randomNumber#return Random numbers
def drawWinningLotteryNumber():#this function generates the winning numbers
lotteryDrawNumbers = []#list containner for the generated lottery numbers
for currentLotteryNumber in range(8):#for loop to keep generating for 8 counts
randomNumber = generateRandomNumber()# call funtion to generate random numbers
lotteryDrawNumbers.append(randomNumber)# add to the back of the list
return lotteryDrawNumbers#return the list
def printWinningLotteryNumber(lotteryDrawNumbers):#ths function prints the winning numbers
for currentLotteryNumberIndex in range (len(lotteryDrawNumbers)):#for loop to run the print funtion for the length of the list
print(lotteryDrawNumbers[currentLotteryNumberIndex], end = " ")#prints the numbers
def draw():# this funtion brings to gether the draw funtion and prints it
lotteryNumbers = drawWinningLotteryNumber()#call function to draw winning numbers
print ("The 8 lottery number for today are :")
printWinningLotteryNumber(lotteryNumbers)#call funtion to print the generated numbers
def generateID():#this function creates the lotto players and their numbers
players =
for player_id in range(1, 1001):
player_list = []
for i in range(8):
player_list.append(generateRandomNumber())
players[player_id] = player_list
for player_id in players:
print("Player has numbers ".format(player_id, players[player_id]))
pwnSort(players)
#data generating code ends here
def pwnSort(players):
for i in range(1, len(players)):
key = players[i]
j = i-1
while j >=0 and key < players[j] :
players[j+1] = players[j]
j -= 1
players[j+1] = key
generateID()
draw()
当我运行此代码时,它会创建数据并卡住并在 pwnSort() 处给出以下内容
Player 984 has numbers [14, 8, 16, 9, 29, 21, 3, 9]
Player 985 has numbers [28, 15, 1, 19, 30, 23, 29, 2]
Player 986 has numbers [6, 8, 13, 0, 19, 5, 13, 2]
Player 987 has numbers [29, 13, 9, 16, 28, 8, 15, 20]
Player 988 has numbers [6, 0, 13, 20, 18, 26, 22, 15]
Player 989 has numbers [29, 13, 17, 16, 18, 13, 14, 8]
Player 990 has numbers [12, 22, 29, 5, 13, 16, 16, 23]
Player 991 has numbers [18, 0, 12, 11, 11, 6, 19, 24]
Player 992 has numbers [4, 0, 19, 24, 17, 20, 19, 3]
Player 993 has numbers [24, 28, 10, 4, 8, 13, 19, 6]
Player 994 has numbers [19, 17, 10, 14, 7, 15, 14, 6]
Player 995 has numbers [1, 18, 24, 5, 29, 19, 20, 2]
Player 996 has numbers [26, 9, 0, 28, 7, 2, 18, 20]
Player 997 has numbers [19, 8, 8, 30, 14, 0, 5, 8]
Player 998 has numbers [19, 16, 7, 14, 16, 25, 0, 21]
Player 999 has numbers [15, 27, 29, 10, 4, 28, 12, 30]
Player 1000 has numbers [8, 18, 6, 6, 17, 25, 6, 16]
Traceback (most recent call last):
File "E:/User Profile/Documents/ACBT/Data Sturctures/assignment.py", line 83, in <module>
generateID()
File "E:/User Profile/Documents/ACBT/Data Sturctures/assignment.py", line 37, in generateID
pwnSort(players)
File "E:/User Profile/Documents/ACBT/Data Sturctures/assignment.py", line 49, in pwnSort
while j >=0 and key < players[j] :
KeyError: 0
我能知道为什么以及如何解决这个问题
【问题讨论】:
也许如果我的解决方案解决了问题,最好将其标记为已接受? 【参考方案1】:您在pwnSort
方法内的while
循环的条件中有错误。
循环的条件是j >= 0 and key < players[j]
,但您声明的是j = i-1
,所以在第一次迭代中j
的值将是0
。在您的 players
字典中,没有名为 0
的键,正如错误 KeyError: 0
所指出的那样。
要解决这个问题,只需将条件更改为:j > 0 and key < players[j]
最终的函数结果如下:
def pwnSort(players):
for i in range(1, len(players)):
key = players[i]
j = i-1
while j > 0 and key < players[j]:
players[j+1] = players[j]
j -= 1
players[j+1] = key
【讨论】:
以上是关于彩票插入排序算法的主要内容,如果未能解决你的问题,请参考以下文章