Question

Language: Python Topic: API and JSON Function name: min_pop_countries Parameters: region (str), num (int) Return: list...

Language: Python

Topic: API and JSON

Function name: min_pop_countries

Parameters: region (str), num (int)

Return: list of tuples

Description: You are working on a project for your Demography class and you are tasked with finding the top num most populous countries in a given region . Instead of looking up on the Internet, you decide to apply your CS1301 knowledge of APIs and write a function to solve the problem for you. Develop a function that takes in a region (str) and a num (int), return a list of tuples of the form [(country_name, population), …] for the top num most populous countries in the region. The list of tuples you return must be s orted in descending order of population. If the given region is invalid, return “[region] is not a valid region.”

Test Cases:

>>> aList = min_pop_countries("Oceania", 5)

>>> print(aList)

[('Australia', 24117360), ('Papua New Guinea', 8083700), ('New Zeal and', 4697854), ('Fiji', 867000), ('Solomon Islands', 642000)]

API's and data can be found here:

https://restcountries.eu/

^ Data

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have implemented the code as per requirement. I have provided the code and the screenshot with output. Each step is explained with comments.

Code:

import requests

def bySecond(elem):
'''
Custom function, that returns the 2nd element of the input variable
'''
return elem[1]

def min_pop_countries(region, n):
'''
This function calculates most populous countries in a certain region.
@param: string region, integer n
@return: top n number of most populous countries in region
  
'''
#URL to get the data from
main_url = "https://restcountries.eu/rest/v2/region/"
#list of valid regions
regions = ['africa', 'americas', 'asia', 'europe', 'oceania']
#lowercase the region argument
region = region.lower()
#if argument region is part of valid regions
if region in regions:
full_url = main_url + region #construct full region
data = {} #create empty dictionary
response = requests.get(full_url) #get the data
data = response.json() #store in json format
lst = [] #create empty list
for items in data: #loop through the data
lst.append((items['name'], items['population'])) #get the country name and population
  
lst.sort(key=bySecond, reverse = True) #sort the list by population in reverse
  
return lst[0:5] #return top n countries and population
#if argument region is not part of valid regions print error
else:
errorMsg = region + 'is not a valid region'
return errorMsg
aList = min_pop_countries("Oceania",5)
print(aList)

Screenshot:

Add a comment
Know the answer?
Add Answer to:
Language: Python Topic: API and JSON Function name: min_pop_countries Parameters: region (str), num (int) Return: list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python String Product Function Name: string Multiply Parameters: sentence (str), num (int) Returns: product (int) Description:...

    Python String Product Function Name: string Multiply Parameters: sentence (str), num (int) Returns: product (int) Description: You're texting your friend when you notice that they replace many letters with numbers. Out of curiosity, you want to find the product of the numbers. Write a function that takes in a string sentence and an int num, and find the product of only the first num numbers in the sentence. If num is 0, return 0. If num > O but there...

  • Language: Python Topic: Tuples Function name : todo_tuple Parameters : todo (list of tuples of strings),...

    Language: Python Topic: Tuples Function name : todo_tuple Parameters : todo (list of tuples of strings), completed (list of strings) Returns: final_list (list) Description : Write a function that takes in a list of tuples of strings that represents the work you have to do in each class, and a list of strings that represent the work you have already completed. Each tuple in the todo list represents the work for a single class. For this function, go through the...

  • Language: PYTHON Function name : favorite_day Parameters : list of tuples (dates), int (weekday, 0-6, Mondays...

    Language: PYTHON Function name : favorite_day Parameters : list of tuples (dates), int (weekday, 0-6, Mondays are 0), int (day of the month 1 to 28) Returns: dates: list of tuples Description: Imagine that you have a favorite weekday, and want to see if certain days fall on that weekday. Using the calendar module from the Python standard library , write a function which takes in a list of tuples formatted like [(month, year), etc.], your favorite weekday, and a...

  • python Days of the Week Function Name: days_of_the_week() Parameters: list of birthdays ( list ), year...

    python Days of the Week Function Name: days_of_the_week() Parameters: list of birthdays ( list ), year (int ) Returns: list of names ( list ) Description: You and your friends all want to celebrate your birthdays together, but you don't want to stay up late on a school night. Write a function that takes in a list of tuples formatted as (day ( int ), month ( int ), name (string)), and a year ( int ). Use Python's calendar...

  • Write a ML language function stripmin of type int list -> int list that will return...

    Write a ML language function stripmin of type int list -> int list that will return a list from which every instance of the lowest value has been removed. Hints: • Only stripmin itself should be visible at the top level; •You should have two recursive helper functions tucked away inside a let … in … end; structure • Also include a val to avoid having to pass a value that never changes to one of these functions. • Use...

  • Language: Python Topic: Try/Except Function name : add_divide Parameters : list of ints, int Returns: float...

    Language: Python Topic: Try/Except Function name : add_divide Parameters : list of ints, int Returns: float Description: Given a list of integers and a number, you want to add the numbers in the list to a total value by iterating through the given list. However, when the index you are at is divisible by the integer passed in, you must instead divide the current total by the element at that index. You must be careful when dividing the total (you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT