首页 > 解决方案 > Python图形算法问题,我的代码有什么问题?

问题描述

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

这是代码:

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")

标签: pythongraphcollectionshashmapqueue

解决方案


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

好吧,基本上我改变了 personIsSeller 的索引,它现在正在工作,我是社区的新成员,感谢@Andrew,他让我改变了解决问题的方法。


推荐阅读