파이썬 while문 질문
조회수 1679회
아래는 너비우선탐색 알고리즘 코드인데요
에러없이 돌아가서 정답은 "Thom"이 나옵니다.
그런데 저기 while문에서 while search_queue:
를
while search_queue == True:
이렇게 쓰면 안되던데
두 구문의 차이가 뭔가요 ㅜㅜ?
graph = {}
graph["you"] = ["alice", "bob", "claire"]
graph["alice"] = ["peggy"]
graph["bob"] = ["anuj", "peggy"]
graph["claire"] = ["thom", "jonny"]
graph["anuj"] = []
graph["peggy"] = []
graph["thom"] = []
graph["jonny"] = []
from collections import deque
def person_is_seller(name):
return name[-1] == "m"
def search(name):
search_queue = deque()
search_queue += graph["you"]
# print(search_queue)
searched = []
while search_queue:
person = search_queue.popleft()
if not person in searched:
if person_is_seller(person):
print(person + " is mango seller!")
return True
else:
search_queue += graph[person]
searched.append(person)
return False
search("you")
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
댓글 입력