Created
September 6, 2015 06:43
-
-
Save kingoflolz/8465efa8a925ca58da35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
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)) | |
if c in paths: | |
for path in paths[c]: | |
if path not in visited: | |
todo.put(path) | |
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