THINK FIRST·CODE LATER

← All labs

Great-circle distances and the nearest station

Problem

Input: a stations CSV name,lat,lon,bikes, a line ---, and a users CSV name,lat,lon.

Implement the haversine distance with R = 6371 km. For each user print Omar: nearest Riverside 0.74 km; if that nearest station has 0 bikes and another station has bikes, append (empty) -> nearest with bikes Library 1.11 km. Finally print at latitude 28.01: 1 deg latitude = 111.2 km, 1 deg longitude = 98.2 km for the mean latitude of the stations (distances from (lat, 0) to (lat + 1, 0) and to (lat, 1)).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v10-1-data-1.txt

name,lat,lon,bikes
North Gate,28.0200,120.6500,0
Library,28.0150,120.6600,7
Riverside,27.9900,120.6900,4
---
name,lat,lon
Mei,28.0210,120.6510
Omar,27.9950,120.6850
What your program must print for v10-1-data-1.txt
Mei: nearest North Gate 0.15 km (empty) -> nearest with bikes Library 1.11 km
Omar: nearest Riverside 0.74 km
at latitude 28.01: 1 deg latitude = 111.2 km, 1 deg longitude = 98.2 km

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v10-1-data-1.txt

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v10-1-data-1.txt | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v10-1-data-1.txt", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.