Skip to content

Commit 5dc6f03

Browse files
part 6.1 finished
1 parent 183e788 commit 5dc6f03

13 files changed

+2443
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Longitude;Latitude;FID;name;total_slot;operative;id
2+
24.950292890004903;60.155444793742276;1;Kaivopuisto;30;Yes;001
3+
24.956347471358754;60.160959093887129;2;Laivasillankatu;12;Yes;002
4+
24.944927399779715;60.158189199971673;3;Kapteeninpuistikko;16;Yes;003
5+
24.941775800312996;60.16098589997938;4;Viiskulma;14;Yes;004
6+
24.93628529982675;60.157948300373846;5;Sepankatu;32;Yes;005
7+
24.929709900391629;60.162225100108344;6;Hietalahdentori;24;Yes;006
8+
24.945959999554361;60.163103199952786;7;Designmuseo;14;Yes;007
9+
24.939149900447603;60.165288299815245;8;Vanha kirkkopuisto;24;Yes;008
10+
24.944134928883898;60.166911666939939;9;Erottajan aukio;1;Yes;009

part6/1. Reading files/city_bikes/stations2.csv

+239
Large diffs are not rendered by default.

part6/1. Reading files/city_bikes/stations3.csv

+346
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Longitude;Latitude;FID;name;total_slot;operative;id
2+
24.950292890004903;60.155444793742276;1;Kaivopuisto;30;Yes;001
3+
24.956347471358754;60.160959093887129;2;Laivasillankatu;12;Yes;002
4+
24.944927399779715;60.158189199971673;3;Kapteeninpuistikko;16;Yes;003
5+
24.941775800312996;60.16098589997938;4;Viiskulma;14;Yes;004
6+
24.93628529982675;60.157948300373846;5;Sepankatu;32;Yes;005
7+
24.929709900391629;60.162225100108344;6;Hietalahdentori;24;Yes;006
8+
24.945959999554361;60.163103199952786;7;Designmuseo;14;Yes;007
9+
24.939149900447603;60.165288299815245;8;Vanha kirkkopuisto;24;Yes;008
10+
24.944134928883898;60.166911666939939;9;Erottajan aukio;1;Yes;009
11+
24.949472879170116;60.165017180298662;10;Kasarmitori;28;Yes;010
12+
24.804734999820425;60.226378999990914;336;Leppavaaran uimahalli;18;Yes;747
13+
24.813871000246536;60.228462999867588;337;Vallikatu;24;Yes;749
14+
24.819613999786327;60.227827000215939;338;Vallipolku;20;Yes;751
15+
24.81395399957745;60.234401000305979;339;Linnuntie;24;Yes;753
16+
24.824561000389906;60.230452999906049;340;Kutsuntatie;16;Yes;755
17+
24.831396999971616;60.235088999657648;341;Painiitty;12;Yes;757
18+
24.839389999848663;60.220466999852192;342;Makkylan asema;32;Yes;761
19+
24.83353599968666;60.22055100012858;343;Kalkkipellonmaki;24;Yes;763
20+
24.820635000105199;60.223377000048572;344;Ruutikatu;10;Yes;767
21+
24.814793999643427;60.204582000259165;345;Tiurintie;12;Yes;769

0 commit comments

Comments
 (0)