-
-
Save kevinmel2000/b2c5a9ba2833019785a0 to your computer and use it in GitHub Desktop.
This file contains 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
START, END = 'A', 'E' | |
COORDINATE = { | |
'A' : [0,0], | |
'B' : [2,0], | |
'C' : [0,5], | |
'D' : [4,1], | |
'E' : [4,0] | |
} | |
EDGE = { | |
'A' : ['B','C','D'], | |
'B' : ['D','E','A'], | |
'C' : ['B','A'], | |
'D' : ['B','A'], | |
'E' : ['B'] | |
} | |
def distance(node_1, node_2): | |
if node_2 in EDGE[node_1]: | |
x1, y1 = COORDINATE[node_1] | |
x2, y2 = COORDINATE[node_2] | |
return ( ((x1-x2)**2) + ((y1-y2)**2) ) ** 0.5 | |
else: | |
return -1 | |
def path_distance(nodes): | |
total = 0 | |
for i in range(len(nodes)-1): | |
dist = distance(nodes[i],nodes[i+1]) | |
if dist == -1: | |
return -1 | |
total += dist | |
return total | |
def nonloop_path(previous_path = [START], end = END): | |
current_node = previous_path[-1] | |
new_path_list = [] | |
for next_node in EDGE[current_node]: | |
new_path = previous_path[:] | |
if next_node not in new_path: | |
new_path.append(next_node) | |
new_path_list.append(new_path) | |
next_new_path_list = nonloop_path(new_path, end) | |
for next_new_path in next_new_path_list: | |
new_path_list.append(next_new_path) | |
return new_path_list | |
def valid_nonloop_path(previous_path = [START], end = END): | |
return [x for x in nonloop_path(previous_path, end) if x[-1] == END] | |
print valid_nonloop_path() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment