-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.py
38 lines (33 loc) · 1.02 KB
/
common.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
from typing import List, NamedTuple
import sys
class Location(NamedTuple):
"""Represents a lat,long coordinate pair.
Latitude and longitude should be expressed in floating-point
degrees.
"""
latitude: float
longitude: float
class WeightedLine(NamedTuple):
start: Location
end: Location
weight: int
def read_segments() -> List[WeightedLine]:
"""Reads a list of weighted route segments from stdin.
There should be one segment per line, expressed in the format
"start_lat, start_lon, end_lat, end_lon, weight".
"""
segments: List[WeightedLine] = []
for line in sys.stdin:
# Each line represents a weighted path segment on the map.
try:
start_lat, start_lon, end_lat, end_lon, wt = line.split(',')
start = Location(latitude = float(start_lat),
longitude = float(start_lon))
end = Location(latitude = float(end_lat),
longitude = float(end_lon))
segment = WeightedLine(start, end, int(wt))
except ValueError:
pass
else:
segments.append(segment)
return segments