簡體   English   中英

彩票插入排序算法

[英]lottery insertion sort algorithm

在我的彩票游戲中,我需要創建 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

我能知道為什么以及如何解決這個問題嗎

您在pwnSort方法內的while循環的pwnSort有錯誤。

循環的條件是j >= 0 and key < players[j]但是你聲明j = i-1所以在第一次迭代時j的值將是0 在您的players字典中,沒有KeyError: 0錯誤指出的名為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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM