Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Y1 AUTUMN 2019
- # Basic Course in Programming Y1
- # Author: Joel Lahenius
- # Template for exercise 8.3 Journey Analyzer
- from math import pi, cos, sqrt
- EARTH_RADIUS = 6371 # km
- EARTH_CIRCUMFERENCE = EARTH_RADIUS * 2 * pi # km
- DEGREE_EQUATOR = EARTH_CIRCUMFERENCE / 360 # km
- def degrees_to_radians(deg):
- rad = deg * (pi / 180)
- return rad
- def calculate_longitude_degree_length(latitude_degrees):
- return cos(degrees_to_radians(latitude_degrees)) * DEGREE_EQUATOR # km
- def calculate_distance(lat1, lon1, lat2, lon2):
- latitude_degree_length = 111.2 # km, roughly constant for our purposes
- longitude_degree_length = calculate_longitude_degree_length((lat1 + lat2) / 2)
- # km, depends on latitude due to Earth being round
- # Write your distance formula here.
- # If you already did it in exercise 7.3 citybike stations,
- # You can use the same formula as there.
- distance = ((latitude_degree_length * (lat1 - lat2)) ** 2 + (longitude_degree_length * (lon1 - lon2)) ** 2) ** (
- 1 / 2)
- return distance
- def find_distance(line):
- data = line.strip().split(";")
- longitude = data[0]
- latitude = data[1]
- return longitude, latitude
- def find_time(line):
- data = line.strip().split(";")
- full_time_stamp = data[3]
- clock_time = full_time_stamp[-9:-1]
- return clock_time
- def calculate_time(starting_time, ending_time):
- t0 = starting_time.split(":")
- tn = ending_time.split(":")
- t0 = [int(i) for i in t0]
- tn = [int(i) for i in tn]
- dseconds = tn[2] - t0[2]
- dminutes = tn[1] - t0[1]
- dhours = tn[0] - t0[0]
- hours2minutes = dhours * 60
- seconds2minutes = dseconds / 60
- dt = dminutes + hours2minutes + seconds2minutes
- return round(dt, 1)
- def main():
- try:
- filename = input("Journey file name:\n")
- file = open(filename, "r")
- file.readline()
- file.readline()
- file.readline()
- rivi1 = file.readline()
- rivi2 = file.readline()
- start_time = find_time(rivi1)
- while rivi2 != "":
- rivi1 = rivi2
- rivi2 = file.readline()
- file.close()
- end_time = find_time(rivi1)
- time = calculate_time(start_time, end_time)
- print("Your journey took {} minutes.".format(time))
- except FileNotFoundError:
- print("Could not open the file {}, try again.".format(filename))
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement