Skip to content

Instantly share code, notes, and snippets.

@kingoflolz
Created September 5, 2015 11:46
Show Gist options
  • Save kingoflolz/66d89798349a0e183a08 to your computer and use it in GitHub Desktop.
Save kingoflolz/66d89798349a0e183a08 to your computer and use it in GitHub Desktop.
from pprint import pprint
from queue import Queue
w = input("Enter configuration: ")
z = w.split(" ")
paths = {}
for t in z:
if "<->" in t:
v = t.split("<->")
if v[0] not in paths:
paths[v[0]] = {v[1], v[0]}
else:
paths[v[0]].add(v[1])
if v[1] not in paths:
paths[v[1]] = {v[1], v[0]}
else:
paths[v[1]].add(v[0])
elif "->" in t:
v = t.split("->")
if v[0] not in paths:
paths[v[0]] = {v[1], v[0]}
else:
paths[v[0]].add(v[1])
elif "<-" in t:
v = t.split("<-")
if v[1] not in paths:
paths[v[1]] = {v[1], v[0]}
else:
paths[v[1]].add(v[0])
#pprint(paths)
def search(start, end):
try:
if start == end and start not in paths:
return False
todo = Queue()
todo.put(start)
visited = set()
while not todo.empty():
c = todo.get()
if c == end:
return True
#print(c)
visited.add(str(c))
for path in paths[c]:
if path not in visited:
todo.put(path)
return False
except:
return False
v = (input("Query? "))
while v != "":
t = v.split("->")
if search(t[0], t[1]):
print("Connected!")
else:
print("No connection.")
v = input("Query? ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment