Find latitude and longitude of utmost 20 countries, ordered by population, with a population greater or equal to the population limit given below and have atleast one currency exclusively for themselves. (countries like Madagascar, Sri Lanka but not India, USA). 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: 65110000
Note: Population limit will change at random intervals. So please make sure answer is computed for the correct population limit before submitting.
from math import pi, sin, cos, sqrt, asin, atan2
populationLimit = 65110000;
r = 6371000;
allCountries = []
countries = []
for country in allCountries:
if country['population'] >= populationLimit and len(country['curencies']) > 1:
countries.append(country)
if len(countries) == 20:
break;
total = 0;
for i in range(len(countries)):
for j in range(1, len(countries)):
fi1 = countries[i]['latitude'] * pi / 180
fi2 = countries[j]['latitude'] * pi / 180;
dFi = (countries[j]['latitude'] - countries[i]['latitude']) * pi / 180;
dLam = (countries[j]['longitude'] - countries[i]['longitude']) * pi / 180;
a = sin(dFi / 2) * sin(dFi / 2) + cos(fi1) * cos(fi2) * sin(dLam / 2) * sin(dLam / 2);
c = 2 * atan2(sqrt(a), sqrt(1 - a))
total += r * c;
print("Total distance:", total / 1000);
Comments
Leave a comment