|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是一个流行的彩票。人们可以从1到30之间随机选择7个不同的数字。中奖号码将开放给公众,直到到期日期。匹配7个号码、6个号码和(>=)4个号码以上的人将分别获得3个类别的奖品。忽略数字的顺序。每个人只能有一个赌注。
给你一本字典,里面有买家的名字、他们的彩票号码和中奖号码,请详细说明他们能得到的奖金。
例如:
people ={
'Hermione':(7, 6, 5, 4, 3, 2, 1),
'Harry':(7, 2, 8, 3, 10, 6, 1),
'Ron' : (11, 2, 4, 22, 8, 16, 15),
'Voldemort' : (17, 4, 24, 14, 25, 3, 29),
'Dumbledore' : (1, 2, 3, 4, 14, 6, 30)}
winning_number = (1,2,3,4,5,6,7)
winprize(people, winning_number) == {'first_prize': {'Hermione'}, 'third_prize': {'Dumbledore', 'Harry'}}
(注意return dictionary的值已经设置。可以调用defaultdict来构造一个默认空集的字典)
def winprize(people, winning_number):
def winprize(people, winning_number):
result = {'first_prize':[],'second_prize':[],'third_prize':[]}
for each in people:
i = 0
for number in winning_number:
if number in people[each]:
i += 1
if i == 7:
result['first_prize'].append(each)
elif i == 6:
result['second_prize'].append(each)
elif i == 5:
result['third_prize'].append(each)
result2 = result.copy()
for each in result2:
if result2[each] == []:
del result[each]
return result
people ={
'Hermione':(7, 6, 5, 4, 3, 2, 1),
'Harry':(7, 2, 8, 3, 10, 6, 1),
'Ron':(11, 2, 4, 22, 8, 16, 15),
'Voldemort':(17, 4, 24, 14, 25, 3, 29),
'Dumbledore':(1, 2, 3, 4, 14, 6, 30)}
winning_number = (1,2,3,4,5,6,7)
result = winprize(people,winning_number)
print(result)
|
|