From 4c6132f406f971533d717b7dbdb7a4316f245832 Mon Sep 17 00:00:00 2001 From: "renier.trenuela@freighttrackertms.com" Date: Fri, 3 Oct 2025 10:22:36 +0800 Subject: [PATCH 1/5] Python script that performs haversine calculations --- haversine_calculation/haversine.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 haversine_calculation/haversine.py diff --git a/haversine_calculation/haversine.py b/haversine_calculation/haversine.py new file mode 100644 index 000000000000..b3e06df2bf16 --- /dev/null +++ b/haversine_calculation/haversine.py @@ -0,0 +1,26 @@ +import math + +def haversine(lat1, lon1, lat2, lon2): + """ + Calculate the great circle distance in kilometers between two points + on the earth (specified in decimal degrees) + """ + # Convert decimal degrees to radians + lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2]) + + # Haversine formula + dlat = lat2 - lat1 + dlon = lon2 - lon1 + a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2 + c = 2 * math.asin(math.sqrt(a)) + r = 6371 # Radius of earth in kilometers. Use 3956 for miles. + return c * r + +# Example usage +if __name__ == "__main__": + # Coordinates of New York City and London + lat1, lon1 = 40.7128, -74.0060 # New York City + lat2, lon2 = 51.5074, -0.1278 # London + + distance = haversine(lat1, lon1, lat2, lon2) + print(f"Distance between New York City and London: {distance:.2f} km") From 48575adce27564e55b24001ef4b0127059d98200 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Oct 2025 02:23:55 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- haversine_calculation/haversine.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/haversine_calculation/haversine.py b/haversine_calculation/haversine.py index b3e06df2bf16..4c9696d15ed0 100644 --- a/haversine_calculation/haversine.py +++ b/haversine_calculation/haversine.py @@ -1,26 +1,31 @@ import math + def haversine(lat1, lon1, lat2, lon2): """ - Calculate the great circle distance in kilometers between two points + Calculate the great circle distance in kilometers between two points on the earth (specified in decimal degrees) """ - # Convert decimal degrees to radians + # Convert decimal degrees to radians lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2]) - - # Haversine formula - dlat = lat2 - lat1 - dlon = lon2 - lon1 - a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2 - c = 2 * math.asin(math.sqrt(a)) - r = 6371 # Radius of earth in kilometers. Use 3956 for miles. + + # Haversine formula + dlat = lat2 - lat1 + dlon = lon2 - lon1 + a = ( + math.sin(dlat / 2) ** 2 + + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2 + ) + c = 2 * math.asin(math.sqrt(a)) + r = 6371 # Radius of earth in kilometers. Use 3956 for miles. return c * r + # Example usage if __name__ == "__main__": # Coordinates of New York City and London lat1, lon1 = 40.7128, -74.0060 # New York City - lat2, lon2 = 51.5074, -0.1278 # London - + lat2, lon2 = 51.5074, -0.1278 # London + distance = haversine(lat1, lon1, lat2, lon2) print(f"Distance between New York City and London: {distance:.2f} km") From 3ccbc42f38b279f97a3c16a04035314ec7dc9911 Mon Sep 17 00:00:00 2001 From: "renier.trenuela@freighttrackertms.com" Date: Fri, 3 Oct 2025 10:25:03 +0800 Subject: [PATCH 3/5] I have successfully created a Python package for haversine calculations with proper package structure. The implementation includes: 1. A haversine_calculation directory with an __init__.py file to make it a proper Python package 2. A haversine.py file containing the haversine function that calculates the great circle distance between two points on Earth using their latitude and longitude coordinates 3. Example usage code that calculates the distance between New York City and London as approximately 5570.22 km --- haversine_calculation/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 haversine_calculation/__init__.py diff --git a/haversine_calculation/__init__.py b/haversine_calculation/__init__.py new file mode 100644 index 000000000000..d55199b6460b --- /dev/null +++ b/haversine_calculation/__init__.py @@ -0,0 +1 @@ +# This file makes haversine_calculation a Python package From 0bbe184a21d0184eb930a3428b70dc5e78a07193 Mon Sep 17 00:00:00 2001 From: "renier.trenuela@freighttrackertms.com" Date: Fri, 3 Oct 2025 10:26:39 +0800 Subject: [PATCH 4/5] Adding hints --- haversine_calculation/haversine.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/haversine_calculation/haversine.py b/haversine_calculation/haversine.py index 4c9696d15ed0..abac609a8121 100644 --- a/haversine_calculation/haversine.py +++ b/haversine_calculation/haversine.py @@ -1,10 +1,27 @@ import math -def haversine(lat1, lon1, lat2, lon2): +def haversine(lat1: float, lon1: float, lat2: float, lon2: float) -> float: """ - Calculate the great circle distance in kilometers between two points + Calculate the great circle distance in kilometers between two points on the earth (specified in decimal degrees) + + Args: + lat1 (float): Latitude of the first point in decimal degrees + lon1 (float): Longitude of the first point in decimal degrees + lat2 (float): Latitude of the second point in decimal degrees + lon2 (float): Longitude of the second point in decimal degrees + + Returns: + float: Distance between the two points in kilometers + + Examples: + >>> round(haversine(40.7128, -74.0060, 51.5074, -0.1278), 2) + 5570.22 + >>> haversine(0, 0, 0, 0) + 0.0 + >>> round(haversine(0, 0, 90, 0), 2) + 10007.54 """ # Convert decimal degrees to radians lat1, lon1, lat2, lon2 = map(math.radians, [lat1, lon1, lat2, lon2]) From 54c5504f5339c68c67cfc6dd7f8080857188b03d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Oct 2025 02:27:02 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- haversine_calculation/haversine.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/haversine_calculation/haversine.py b/haversine_calculation/haversine.py index abac609a8121..ad1411690704 100644 --- a/haversine_calculation/haversine.py +++ b/haversine_calculation/haversine.py @@ -3,18 +3,18 @@ def haversine(lat1: float, lon1: float, lat2: float, lon2: float) -> float: """ - Calculate the great circle distance in kilometers between two points + Calculate the great circle distance in kilometers between two points on the earth (specified in decimal degrees) - + Args: lat1 (float): Latitude of the first point in decimal degrees lon1 (float): Longitude of the first point in decimal degrees lat2 (float): Latitude of the second point in decimal degrees lon2 (float): Longitude of the second point in decimal degrees - + Returns: float: Distance between the two points in kilometers - + Examples: >>> round(haversine(40.7128, -74.0060, 51.5074, -0.1278), 2) 5570.22