python - 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")
解决方案
def personIsSeller(name):
return name[0] == 'M
好吧,基本上我改变了 personIsSeller 的索引,它现在正在工作,我是社区的新成员,感谢@Andrew,他让我改变了解决问题的方法。
推荐阅读
- python - 应用程序版本控制:如何处理密钥演变?
- c - 检测到堆损坏:在正常块之后(#87)
- properties - “变量点属性”构造(var.property)只是访问器的语法糖吗?
- amazon-web-services - AppSync 与 TS 构建问题中的 ApolloClient - 模块“Apollo 客户端”没有导出成员“WatchQueryFetchPolicy”
- javascript - 如何将 gpx 轨道与传单地图 Javascript 对齐
- python - 创建带有标签和相应元组列表的字典的问题
- node.js - 无法连接到 *name_server* - getaddrinfo ENOTFOUND
- python - 通过列表字典迭代过滤火花数据帧
- jenkins - 无法访问电子邮件正文中的环境变量
- c# - 无法在 EF Core 5/6 中的 Subselect 上使用 GroupBy