Skip to content

Commit 809ef68

Browse files
committed
add geographic distance and estimated travel time calculator
1 parent d49deeb commit 809ef68

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from geopy.distance import geodesic
2+
3+
4+
def calculate_distance_and_time(coord1, coord2, avg_speed):
5+
"""
6+
Calculate the distance between two coordinates and estimate travel time.
7+
:param coord1: Tuple containing the latitude and longitude of the first location (lat1, lon1)
8+
:param coord2: Tuple containing the latitude and longitude of the second location (lat2, lon2)
9+
:param avg_speed: Average speed in km/h for estimating travel time
10+
:return: Distance in kilometers and estimated travel time in hours
11+
"""
12+
if not (isinstance(coord1, tuple) and isinstance(coord2, tuple)):
13+
raise ValueError("Coordinates must be in the format 'lat, lon'")
14+
15+
# Calculate geodesic distance
16+
distance = geodesic(coord1, coord2).kilometers
17+
# Estimate travel time (distance / speed)
18+
if avg_speed > 0:
19+
travel_time = distance / avg_speed
20+
else:
21+
raise ValueError("Average speed must be greater than 0.")
22+
return distance, travel_time
23+
24+
25+
def main():
26+
# Coordinates (latitude, longitude)
27+
coord1 = input("Enter the latitude and longitude of the first location (lat1, lon1) Example: 40.7128, -74.006: ")
28+
coord2 = input("Enter the latitude and longitude of the second location (lat2, lon2) Example: 37.7749, -122.4194: ")
29+
30+
# Speed in km/h (e.g., driving speed)
31+
avg_speed = float(input("Enter the average speed in km/h Example: 60: "))
32+
33+
# Calculate the distance and travel time
34+
distance, travel_time = calculate_distance_and_time(coord1, coord2, avg_speed)
35+
36+
print(f"Distance between the two coordinates: {distance:.2f} km")
37+
print(f"Estimated travel time: {travel_time:.2f} hours")
38+
39+
40+
if __name__ == "__main__":
41+
main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
geopy==2.4.1
2+
pytest==8.3.3

Geographic Distance/runtime.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-3.12.7
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
from geographic_distance import calculate_distance_and_time
3+
4+
5+
def test_calculate_distance_and_time():
6+
# Test with valid coordinates and speed
7+
coord1 = (40.7128, -74.006)
8+
coord2 = (37.7749, -122.4194)
9+
avg_speed = 60
10+
distance, travel_time = calculate_distance_and_time(coord1, coord2, avg_speed)
11+
assert distance > 0
12+
assert travel_time > 0
13+
14+
15+
def test_calculate_distance_and_time_invalid_speed():
16+
# Test with invalid speed (zero)
17+
coord1 = (40.7128, -74.006)
18+
coord2 = (37.7749, -122.4194)
19+
avg_speed = 0
20+
with pytest.raises(ValueError):
21+
calculate_distance_and_time(coord1, coord2, avg_speed)
22+
23+
24+
def test_calculate_distance_and_time_invalid_coordinates():
25+
# Test with invalid coordinates (not tuples)
26+
coord1 = "40.7128, -74.006"
27+
coord2 = "37.7749, -122.4194"
28+
avg_speed = 60
29+
with pytest.raises(ValueError):
30+
calculate_distance_and_time(coord1, coord2, avg_speed)
31+
32+
33+
def test_calculate_distance_and_time_same_coordinates():
34+
# Test with same coordinates
35+
coord1 = (40.7128, -74.006)
36+
coord2 = (40.7128, -74.006)
37+
avg_speed = 60
38+
distance, travel_time = calculate_distance_and_time(coord1, coord2, avg_speed)
39+
assert distance == 0
40+
assert travel_time == 0

0 commit comments

Comments
 (0)