"""
Who is the killer?
Some people have been killed!
You have managed to narrow the suspects down to just a few.
Luckily, you know every person who those suspects have seen on the day of the murders.
Task.
Given a dictionary with all the names of the suspects and everyone that they have seen on that day which may look like this:
{'James': ['Jacob', 'Bill', 'Lucas'],
'Johnny': ['David', 'Kyle', 'Lucas'],
'Peter': ['Lucy', 'Kyle']}
and also a list of the names of the dead people:
['Lucas', 'Bill']
return the name of the one killer, in our case 'James' because he is the only person that saw both 'Lucas' and 'Bill'
"""
# def killer(suspect_info, dead):
# lstkey = list(suspect_info.keys())
# lstvalue = list(suspect_info.values())
# count = 0
# for i in range(len(lstvalue)):
# for j in range(len(lstvalue[i])):
# for k in range(len(dead)):
# if dead[k] == lstvalue[i][j]:
# count += 1
# if count == len(dead):
# return lstkey[i]
# other anwser
# set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。
def killer(info, dead):
for name, seen in info.items():
if set(dead) <= set(seen):
return name
print(killer({'James': ['Jacob', 'Bill', 'Lucas'],
'Johnny': ['David', 'Kyle', 'Lucas'],
'Peter': ['Lucy', 'Kyle']},
['Lucas', 'Bill']))
print(killer({'Brad': [],
'Megan': ['Ben', 'Kevin'],
'Finn': []},
['Ben']))
Who is keller
最新推荐文章于 2024-05-02 17:57:23 发布