-
Notifications
You must be signed in to change notification settings - Fork 0
/
star1.py
34 lines (28 loc) · 879 Bytes
/
star1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
sampleData = """start-A
start-b
A-c
A-b
b-d
A-end
b-end
"""
# data = open("data.txt", "r", encoding="utf-8").read()
data = sampleData
lst = [line.split("-") for line in data.splitlines()]
edges = [[a, b] for a, b in lst if a != "end" and b != "start"]
edges += [[b, a] for a, b in lst if a != "start" and b != "end"]
def find_path_r(current_path, found_paths):
current_node = current_path[-1]
if current_node == "end":
found_paths.append(current_path)
return
for edge in edges:
if edge[0] == current_node:
if edge[1].isupper() or edge[1] not in current_path:
find_path_r(current_path + [edge[1]], found_paths)
def find_paths():
"""Find the all the paths from start to the end node"""
found_paths = []
find_path_r(["start"], found_paths)
return found_paths
print(len(find_paths()))