Find latitude and longitude of first 20 countries with a population greater than or equal to the population limit given below. Use the country details from this dataset.
Your task is to find the sum of the length of all lines (in kms) that can be drawn between co-ordinates of these countries.
Assume radius of earth: 6371 km
Round length of each line and final result to 2 decimal points
If co-ordinates are missing for any country use 0.000 N 0.000 E
Population limit: 300
Note: Population limit will change at random intervals.
Answer:
from math import radians, asin, sqrt, cos, sin
def distance(lt1, lt2, lg1, lg2):
lon1 = radians(lg1)
lg2 = radians(lg2)
lt1 = radians(lt1)
lt2 = radians(lt2)
dist_lon = lg2 - lg1
dist_lat = lt2 - lt1
res = sin(dist_lat / 2)**2 + cos(lt1) * cos(lt2) * sin(dist_lon / 2)**2
ct = 2 * asin(sqrt(res))
radius = 6371
return(ct * radius)
lt1 = eval(input("Enter first latitude: "))
lt2 = eval(input("Enter second latitude: "))
lg1 = eval(input("Enter first longitude: "))
lg2 = eval(input("Enter second longitude: "))
print(distance(lt1, lt2, lg1, lg2), "KM")
Comments
Leave a comment