如何防止python中的重复字符串(票据)?

2024-09-29 23:33:25 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我的目标是让程序运行,这样它就可以继续抽到票,直到找到一张与中奖票匹配的票。这将更好地工作,如果没有重复票,但我不知道我将如何实现这一点。你知道吗

from __future__ import print_function
from datetime import date, datetime, timedelta
from random import randint

count = 0

class Ticket:
#Powerball Lottery Ticket Class'

     def __init__(Ticket, ball1, ball2, ball3, ball4, ball5, pb):
          Ticket.ball1 = ball1
          Ticket.ball2 = ball2
          Ticket.ball3 = ball3
          Ticket.ball4 = ball4
          Ticket.ball5 = ball5
          Ticket.pb = pb

     def displayTicket(Ticket):
          print ("Ticket: ", Ticket.ball1, Ticket.ball2, Ticket.ball3,
                  Ticket.ball4, Ticket.ball5, Ticket.pb)

WinningTicket = Ticket(randint(0, 69), randint(0, 69), randint(0, 69),
                       randint(0, 69), randint(0, 69), randint(0, 26))
DrawnTicket = Ticket(randint(0, 60), randint(0, 60), randint(0, 60), 
                     randint(0, 60), randint(0, 60), randint(0, 26))

#Winning Ticket
print("Powerball Ticket")
WinningTicket.displayTicket()
print("----------------")

#Draws a random ticket
def randomTicket():
     DrawnTicket = Ticket(randint(0, 69), randint(0, 69), randint(0, 69),
                          randint(0, 69), randint(0, 69), randint(0, 27))
     DrawnTicket.displayTicket()
     return Ticket

#Draw Random Ticket Until DrawnTicket is = RandomTicket
     while WinningTicket != DrawnTicket:
     randomTicket()
     count += 1
     if WinningTicket == DrawnTicket:
          break

#Number of DrawnTickets
print("Number of Drawn Ticket: ", count)

Tags: fromimportdefcountticketprintrandintpb
1条回答
网友
1楼 · 发布于 2024-09-29 23:33:25

根据强力球的规则,你必须从1-69(含)的数字中随机抽取5个数字,不需要替换,你必须从1-26(含)的数字中抽取一个数字。要赢得头奖,你必须匹配红色的强力球号码,并以任何顺序匹配前5个号码,这意味着你看的是组合,而不是排列。你知道吗

因此,我对您的代码进行了调整,使其能够查看数字的组合,并定义了__eq__()魔术方法,以便比较运算符可以按预期工作。为了检查重复的数字,我基本上有一个存储数字列表的列表,每当我们生成一个新的数字列表时,我们只需在返回它之前检查它是否不在列表中,否则我们会清除它并重试。你知道吗

下面是反映这些调整的代码:

from __future__ import print_function
from datetime import date, datetime, timedelta
from random import randint

count = 0


class Ticket:
    # Powerball Lottery Ticket Class'

    def __init__(self, numbers):
        self.wb = numbers[:5]
        self.pb = numbers[5]

    def displayTicket(self):
        print("Ticket: ", self.wb[0], self.wb[1], self.wb[2], self.wb[3], self.wb[4], self.pb)

    def __eq__(self, other):
        if isinstance(other, Ticket):
            return set(self.wb) == set(other.wb) and self.pb == other.pb
        else:
            return False

checkedNumbers = []

# Draws a random ticket
def randomTicket():
    numbers = []
    while True:
        if len(numbers) < 5:
            numbers.append(randint(0,69))
        else:
            numbers.append(randint(0, 26))

        if numbers in checkedNumbers:
            numbers = []
        elif len(numbers) == 6:
            break

    DrawnTicket = Ticket(numbers)
    DrawnTicket.displayTicket()
    checkedNumbers.append(numbers)
    return DrawnTicket


WinningTicket = randomTicket()

checkedNumbers.remove([number for number in WinningTicket.wb] + [WinningTicket.pb])

# Winning Ticket
print("Powerball Ticket")
WinningTicket.displayTicket()
print("        ")

# Draw Random Ticket Until DrawnTicket is = RandomTicket
while True:
    DrawnTicket = randomTicket()
    count += 1
    if WinningTicket == DrawnTicket:
        break


# Number of DrawnTickets
print("Number of Drawn Ticket: ", count)

该计划将按预期进行。然而,就像TigerhawkT3试图暗示的那样,这个程序将需要非常长的时间。你正试图通过随机生成的数字来强行通过292201338个数字的组合,这将产生大量的重复,如果你要通过每一个单一的组合来增加你的方式。即使这样,也需要很长时间。你知道吗

相关问题 更多 >

    热门问题