在Python中生成唯一数字,不使用列表、集合等数据结构

3
这是一道家庭作业测试题(我刚刚发邮件给教授Gruhn,如果他正在残酷地搜索stackoverflow的话)。这是第一门计算机科学课程,使用Python进行介绍。使用书籍“Starting Out With Python 2nd Ed.”测试基本上是关于创建我们自己的模块库、读写文件和尝试/除外逻辑。
第一部分要求创建一个彩票号码模拟器。一个生成非唯一号码,另一个生成唯一不重复号码。我在这里看到的每个答案都使用了列表,而遗憾的是,它们是下一章,我们明确禁止使用它们。
这部分的我的代码:
import random

def ballPickerOne():
    a = random.randint(1, 59)
    b = random.randint(1, 59)
    c = random.randint(1, 59)
    d = random.randint(1, 59)
    e = random.randint(1, 59)
    f = random.randint(1, 35)

    showNumbers(a,b,c,d,e,f)


def ballPickerTwo():
   a = random.randrange(1,59,2)
   b = random.randrange(1,59,3)
   c = random.randrange(1,59,5)
   d = random.randrange(1,59,7)
   e = random.randrange(1,59,11)
   f = random.randint(1,35)

   showNumbers(a,b,c,d,e,f)


def showNumbers(a,b,c,d,e,f):

   print("Your Numbers ...")
   print()
   print("Ball 1: ", a)
   print("Ball 2: ", b)
   print("Ball 3: ", c)
   print("Ball 4: ", d)
   print("Ball 5: ", e)
   print("Red Ball: ", f)
   print()
   print("Good Luck")

我们需要使用showNumbers函数来显示结果,并以此格式显示。ballPickerTwo是“独特”的,我在尝试使用质数间隔来实现独特性时失败了。我曾经尝试使用循环,但无法想出如何使用showNumbers显示生成的数字。
4个回答

1

这是一种非常繁琐的方式,但它不使用列表。它将选择随机且唯一的值。

def ballPickerTwo():

    a = random.randint(1, 59)

    b = a        
    while b == a:
        b = random.randint(1, 59)

    c = b
    while c == b or c == a:
        c = random.randint(1, 59)

    d = c
    while d == c or d == b or d == a:
        d = random.randint(1, 59)

    ...

啊,我本来打算这么做,但我的教授通常不喜欢繁琐的东西,所以我相信一定有办法用一个范围来完成。这原本是一场3小时的课堂测试,但他意识到时间太长了。也许这也是其中的一部分。 - Zachary Matthew Perry

0

只需返回您生成的值 - 在函数中使用return。例如:

def ballPickerOne():
    a = random.randint(1, 59)
    b = random.randint(1, 59)
    c = random.randint(1, 59)
    d = random.randint(1, 59)
    e = random.randint(1, 59)
    f = random.randint(1, 35)
    return a,b,c,d,e,f

showNumbers(a,b,c,d,e,f)

如果:

from random import sample, randint

def ballPickerOne():
    a,b,c,d,e = sample(range(1,59), 5) 
    f = randint(1,35)
    while f!=a and f!=b and f!=c and f!=d and f!=e:
        f = randint(1,35)
    return a,b,c,d,e,f

我认为问题可能是缩进问题,而不是真正的问题。我怀疑对showNumbers的调用在ballPicker函数内部。 - Blckknght
@Blckknght那他为什么提到他在使用showNumbers时遇到了问题呢? - Artsiom Rudzenka
我把showNumbers的缩进弄错了,它应该用于显示ballPickerOne和Two中的数字,因此导致了冲突。 - Zachary Matthew Perry

0

你觉得用整数作为位图来检查是否唯一怎么样?

import random

def showNumbers(a,b,c,d,e,f):
   print("Your Numbers ...")
   print()
   print("Ball 1: ", a)
   print("Ball 2: ", b)
   print("Ball 3: ", c)
   print("Ball 4: ", d)
   print("Ball 5: ", e)
   print("Red Ball: ", f)
   print()
   print("Good Luck")

def ballPickerTwo():
    while True:
        a = random.randint(1, 59)
        b = random.randint(1, 59)
        c = random.randint(1, 59)
        d = random.randint(1, 59)
        e = random.randint(1, 59)
        f = random.randint(1, 35)
        m = 2**a + 2**b + 2**c + 2**d + 2**e + 2**f
        if bin(m).count("1") == 6:
            break
    showNumbers(a,b,c,d,e,f)

这肯定有效,但我不知道 bin 是什么。特别是这一章只是介绍了 random 库中的 .random、.randint 和 .uniform。我猜他是在回调布尔逻辑和第一个回答的人就是我们得到的结果。 - Zachary Matthew Perry

0

这与HYRY的答案类似,它使用数字中的位来记住已选择的数字。这有效是因为Python可以处理任意大的数字。

import random

def showNumbers(a, b, c, d, e, f):
   print("Your Numbers ...")
   print()
   print("Ball 1: ", a)
   print("Ball 2: ", b)
   print("Ball 3: ", c)
   print("Ball 4: ", d)
   print("Ball 5: ", e)
   print("Red Ball: ", f)
   print()
   print("Good Luck")

def pick(cur_val):
    while True:
        v = random.randint(1, 59)
        m = 2**v
        if (cur_val & m) == 0: # bit not set, v never picked before
            return (cur_val | m), v  # return updated cur_val and bit number now set in it

def ballPickerTwo():
    cur_val = 0
    cur_val, a = pick(cur_val)
    cur_val, b = pick(cur_val)
    cur_val, c = pick(cur_val)
    cur_val, d = pick(cur_val)
    cur_val, e = pick(cur_val)
    cur_val, f = pick(cur_val)

    showNumbers(a, b, c, d, e, f)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接