Answer to Question #272357 in HTML/JavaScript Web Application for Deepak

Question #272357

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(https://cdn.jsdelivr.net/gh/apilayer/restcountries@3dc0fb110cd97bce9ddf27b3e8e1f7fbe115dc3c/src/main/resources/countriesV2.json).






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: 28875






Note: Population limit will change at random intervals. So please make sure answer is computed for the correct population limit

1
Expert's answer
2021-11-27T11:34:28-0500
import ​json
from ​math import pi, sin, cos, sqrt, asin




def find_dist(latlng_1, latlng_2):
	'''
	This function calculate distanse in km between two point on Earth
	whose coordinates are passed as two lists and returns rounded distanse
	'''
	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 = "countriesV2.json"


limit = 11750


with ​open(file, 'r') as f:
	contries = json.load(f)
first_20 = {}
for ​contry in contries:
	if contry['population'] >= limit:
		first_20[contry['alpha3Code']] = contry['latlng']
		if len(first_20) == 20:
			break


total_dist = 0
keys = list(first_20.keys())
for ​i in range(len(keys)-1):
	for j in range(len(keys[i+1:])):
		total_dist += find_dist(first_20[keys[i]], first_20[keys[j]])
total_dist = round(total_dist, 2)


print(total_)

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