Pythonで誕生日のパラドックスを解決しようとしています。私は近くにいますが、最後の部分は私を途方に暮れさせます。ランダムを使用して、作成するアイテムの範囲と数を指定して番号のリストを生成しています。それはうまくいきます。
次に、リスト(上記で生成)に重複がないかどうかを確認します。それはうまくいきます。
次に、指定された(n)個のリストを生成しようとします。ここで私は問題にぶつかります。1つのリストを生成し、「NoneType」を返します。反復可能ではありません。私が困惑しているのは、リストは生成されますが、Pythonはそれをリストとして認識していません。
コードは次のとおりです。
def make_bd(n, r):
"""Generates (r) numbers of birthdays in a range (n)."""
import random
t = [random.randrange(n) for i in range(r)]
print (t)
def has_dupe(test):
"""Here I test to see if I can detect a duplicate birthday.
This is based on problem #4."""
d = []
count = 0
for number in test:
if number in d:
count = count + 1
d.append(number)
if count >= 1:
return True
return False
def dupebd(n,r,t):
count_dupe = 0
for i in range(n):
if has_dupe(make_bd(r,t)):
count_dupe = count_dupe + 1
print (float(count)/n)
dupebd(50,365,23)
結果は次のとおりです。
>>> has_dupe(make_bd(50,6))
[13, 3, 8, 29, 34, 44]
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
has_dupe(make_bd(50,6))
File "<pyshell#44>", line 7, in has_dupe
for number in test:
TypeError: 'NoneType' object is not iterable