• Set and string

Question related to mission How to Find Friends

 

Hello everyone, I've just started learning python and I'm having some issues. I think I've solved the problem but I was testing it on a single-letter strings and I found out the hard way that when I enter a longer string my code doesn't work at all. Is there a way to fix it? I would appreciate any help.

def check_connection(network, first, second):
print("first:",first)
end = set(second)
visited = set()
explore = set(first)
while len(explore) > 0:
    print("visited:",visited)
    print("explore:",explore)
    explore_element = set(explore.pop())
    print("checking element:",explore_element)
    add_explore = set()
    for n in network:
        n = set(n.split('-'))
        if explore_element <= n and n-visited == n:
            add_explore = add_explore|(n-explore_element)
            print("adding to explore:",add_explore)
    if end <= add_explore:
        return True
    visited = visited|explore_element
    explore = explore|add_explore
    print()
print("visited:",visited)
return False

print(check_connection(("A-B","B-C","C-D","D-B","D-E","F-G"),"A","E"))
print("---------------")
print(check_connection(("dr101-mr99","mr99-out00","dr101-out00",
"scout1-scout2","scout3-scout1","scout1-scout4","scout4-sscout",
"sscout-super",),"scout2","scout3"))
33