Python图形算法问题,我的代码有什么问题?

时间:2020-10-19 17:59:19

标签: python graph collections hashmap queue

这是我在这里的第一个问题,我正在尝试学习算法。您能告诉我我的代码有什么问题吗? 谢谢您的答复。

代码如下:

from collections import deque

graph ={}
graph["you"] = ["Mark", "Victoria", "Lovie", "Chi","John"]

def personIsSeller(name):
    return name[-1] == 'M'

def search(name):
    searchQueue = deque()
    searchQueue += graph[name]
    searched = []
    while searchQueue:
        person = searchQueue.popleft()
        if not person in searched:
            if personIsSeller(person):
                print(person, "is a mango-seller")
                return True
            else:
                searchQueue += graph[person]
                searched.append(person)
    return False

search("you")

1 个答案:

答案 0 :(得分:0)

def personIsSeller(name):
    return name[0] == 'M

基本上我已经更改了personIsSeller的索引,并且现在正在运行,我是社区中的新成员,感谢@Andrew,他让我改变了解决问题的方法。