-
Notifications
You must be signed in to change notification settings - Fork 0
/
borders.py
47 lines (36 loc) · 1.2 KB
/
borders.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import csv
from collections import Iterable
def flatten(l):
# flatten a nested list
# https://stackoverflow.com/a/2158532
for el in l:
if isinstance(el, Iterable) and not isinstance(el, (str, bytes)):
yield from flatten(el)
else:
yield el
def find_shortest_path(graph, start, end):
# breadth first search (BFS)
# adapted from https://www.python.org/doc/essays/graphs/
dist = {start: [start]}
q = [start]
while len(q):
at = q.pop(0)
for country in graph[at]:
if country not in dist:
dist[country] = [dist[at], country]
q.append(country)
raw_path = dist.get(end)
return list(flatten(dist.get(end)))
country_border_graph = {}
country_code_lookup = {}
with open('borders.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
next(reader, None) # skip the headers
for row in reader:
country_code_lookup[row[0]] = row[1]
country_a = row[0]
country_b = row[2]
if country_a in country_border_graph:
country_border_graph[country_a].append(country_b)
else:
country_border_graph[country_a] = [country_b]