We are looking for passionate developers to join our team.
Solve the challenge below and submit the form, and we will reach out to you!
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
Answers
Answer:
Here, the answer for the question is given below:
Explanation:
import requests
import csv
rows = []
eligible_countries = []
url = input('Webpage to grab source from: ')
html_output_name = input('Name for html file: ')
req = requests.get(url, 'html.parser')
with open(html_output_name, 'r') as f:
f.read(req.text)
for row in req.text:
rows.append(row)
input = int(input("Please enter the limit of population: "))
eligible_countries = []
for i in rows:
if source[i][2] < input:
eligible_countries.append(rows[i])
print(eligible_countries)
f.close()
source = []
The sum of the length of all lines (in km) that can be drawn between the coordinates of these countries.
SOURCE CODE:
import json
from math import pi, sin, cos, sqrt, asin
def find_dist(latlng_1, latlng_2):
'''
This function returns the rounded distance in kilometres between two points on Earth whose coordinates are given as two lists.
'''
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 = 65110000
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
//Calculating the total distance
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 the total distance
print(total_dist)
Explanation:
- The import keyword in Python is used to make code from one module accessible in another. Python imports are crucial for a successful code structure.
- The two libraries are imported the first one is json, and the other are pi, sin, cos, sqrt, and asin from math
- The function find_dist() returns the rounded distance in kilometres between two points on Earth whose coordinates are given as two lists.
- Reading the file using open () and checking whether the population of the country is within the limit or not. After that, the distance is calculated using the math function, and at last, the total distance is printed.
#SPJ3