|
| 1 | +# In this exercise we will write some functions for working on a file containing location data from the stations for city bikes in Helsinki. |
| 2 | + |
| 3 | +# Each file will follow this format: |
| 4 | + |
| 5 | +# Longitude;Latitude;FID;name;total_slot;operative;id |
| 6 | +# 24.950292890004903;60.155444793742276;1;Kaivopuisto;30;Yes;001 |
| 7 | +# 24.956347471358754;60.160959093887129;2;Laivasillankatu;12;Yes;002 |
| 8 | +# 24.944927399779715;60.158189199971673;3;Kapteeninpuistikko;16;Yes;003 |
| 9 | +# Each station has a single line in the file. The line contains the coordinates, name, and other identifying information for the station. |
| 10 | + |
| 11 | +# Distance between stations |
| 12 | +# First, write a function named get_station_data(filename: str). |
| 13 | +# This function should read the names and locations of all the stations in the file, and return them in a dictionary with the following format: |
| 14 | + |
| 15 | +# Sample output |
| 16 | +# { |
| 17 | +# "Kaivopuisto: (24.950292890004903, 60.155444793742276), |
| 18 | +# "Laivasillankatu: (24.956347471358754, 60.160959093887129), |
| 19 | +# "Kapteeninpuistikko: (24.944927399779715, 60.158189199971673) |
| 20 | +# } |
| 21 | +# Dictionary keys are the names of the stations, and the value attached is a tuple containing the location coordinates of the station. |
| 22 | +# The first element in the tuple is the Longitude field, and the second is the Latitude field. |
| 23 | + |
| 24 | +# Next, write a function named distance(stations: dict, station1: str, station2: str), which returns the distance between the two stations given as arguments. |
| 25 | + |
| 26 | +# The distance is calculated using the Pythagorean theorem. |
| 27 | +# The multiplication factors below are approximate values for converting latitudes and longitudes to distances in kilometres in the Helsinki region. |
| 28 | + |
| 29 | +# # we will need the function sqrt from the math module |
| 30 | +# import math |
| 31 | + |
| 32 | +# x_km = (longitude1 - longitude2) * 55.26 |
| 33 | +# y_km = (latitude1 - latitude2) * 111.2 |
| 34 | +# distance_km = math.sqrt(x_km**2 + y_km**2) |
| 35 | +# Some examples of the function in action: |
| 36 | + |
| 37 | +# stations = get_station_data('stations1.csv') |
| 38 | +# d = distance(stations, "Designmuseo", "Hietalahdentori") |
| 39 | +# print(d) |
| 40 | +# d = distance(stations, "Viiskulma", "Kaivopuisto") |
| 41 | +# print(d) |
| 42 | +# Sample output |
| 43 | +# 0.9032737292463177 |
| 44 | +# 0.7753594392019532 |
| 45 | + |
| 46 | +# The greatest distance |
| 47 | +# Please write a function named greatest_distance(stations: dict), which works out the two stations on the list with the greatest distance from each other. |
| 48 | +# The function should return a tuple, where the first two elements are the names of the two stations, and the third element is the distance between the two. |
| 49 | + |
| 50 | +# stations = get_station_data('stations1.csv') |
| 51 | +# station1, station2, greatest = greatest_distance(stations) |
| 52 | +# print(station1, station2, greatest) |
| 53 | +# Sample output |
| 54 | +# Laivasillankatu Hietalahdentori 1.478708873076181 |
| 55 | + |
| 56 | +import math |
| 57 | + |
| 58 | +def get_station_data(filename: str): |
| 59 | + with open(filename) as new_file: |
| 60 | + station_locations = {} |
| 61 | + for line in new_file: |
| 62 | + line = line.split(";") |
| 63 | + if line[0] == "Longitude": |
| 64 | + continue |
| 65 | + station_locations[line[3]] = (float(line[0]), float(line[1])) |
| 66 | + return station_locations |
| 67 | + |
| 68 | +def distance(stations: dict, station1: str, station2: str): |
| 69 | + longitude1 = stations[station1][0] |
| 70 | + longitude2 = stations[station2][0] |
| 71 | + latitude1 = stations[station1][1] |
| 72 | + latitude2 = stations[station2][1] |
| 73 | + |
| 74 | + x_km = (longitude1 - longitude2) * 55.26 |
| 75 | + y_km = (latitude1 - latitude2) * 111.2 |
| 76 | + distance_km = math.sqrt(x_km**2 + y_km**2) |
| 77 | + |
| 78 | + return distance_km |
| 79 | + |
| 80 | + |
| 81 | +def greatest_distance(stations: dict): |
| 82 | + station1 = "" |
| 83 | + station2 = "" |
| 84 | + greatest = 0 |
| 85 | + for x in stations: |
| 86 | + for y in stations: |
| 87 | + if distance(stations, x, y) > greatest: |
| 88 | + station1 = x |
| 89 | + station2 = y |
| 90 | + greatest = distance(stations, x, y) |
| 91 | + return station1, station2, greatest |
| 92 | + |
0 commit comments