![](/img/trans.png)
[英]Generating combinations of elements of 2 lists in sets of 3 in python
[英]Generating Unique Numbers in Python without Using Lists, Sets, Etc
這是一個帶回家的測試問題(如果Gruhn教授無情地搜索堆棧溢出,我會在20分鍾前郵寄)。 第一個計算機科學課程,使用Python的Intro。 使用“從Python第二版開始”這本書。 測試基本上是創建我們自己的模塊庫,讀取和寫入文件,以及嘗試/除邏輯。
第一部分要求創建一個彩票mumber模擬器。 生成非唯一數字的數字,其他唯一的非重復數字。 我在這里看到的每個答案都使用了列表,遺憾的是下一章,我們明確禁止使用它們。
我這部分的代碼:
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生成的數字。
這是一種非常繁瑣的方式,但它不使用列表。 它將選擇隨機和唯一的值。
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)
...
只需返回您生成的值 - 在函數中使用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
如何使用整數作為位圖來檢查唯一?
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)
這類似於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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.