Answer to Question #298496 in Python for Ram

Question #298496



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.

1
Expert's answer
2022-02-16T15:02:08-0500
import json
from math import pi, sin, cos, sqrt, asin

def distance(latlng_1, latlng_2):
    lat1, lon1 = latlng_1[0]*pi/180, latlng_1[1]*pi/180
    lat2, lon2 = latlng_2[0]*pi/180, latlng_2[1]*pi/180
    d = 2*6371*asin(sqrt(sin((lat2-lat1)/2)**2 + cos(lat1)*cos(lat2)*sin((lon2-lon1)/2)**2))
    return round(d,2)

file = "path1.json"
pop_limit = 65110000

with open(file, 'r') as f:
    countries = json.load(f)
first = {}
for country in countries:
    if country['population'] >= pop_limit:
        first[country['alpha3Code']] = country['latlng']
        if len(first) == 20:
            break
            
total_distance = 0
keys = list(first.keys())
for i in range(len(keys)-1):
    for j in range(len(keys[i+1:])):
        total_distance += distance(first[keys[i]], first[keys[j]])
total_distance = round(total_distance, 2)

print(total_distance)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS