File I/O, Searching, and Sorting.

Question 1 (20 marks)

Write a function called readCountries that reads a file and returns a list of countries. The countries should be read from this file (countries.txt), which contains an incomplete list of countries with their area and population. Each line in this file represents one country in the following format:

name, area(in km2), population

When opening the file your function should handle any exceptions that may occur. Your function should completely read in the file, and separate the data into a 2-dimensional list. You may need to split and strip the data as appropriate. Numbers should be converted to their correct types. Your function should return this list so that you can use it in the remaining questions.

E.g.:
>>> countryList = readCountries("countries.txt")
>>> countryList
[["Afghanistan",647500.0,25500100],["Albania",28748.0,2821977],...,["Zimbabwe",390580.0,12973808]]

Question 2 (10 marks)

Write a function called printCountry that takes a string representing a country name as a parameter. First call your answer from question 1 to get the list of countries, then do a binary search through the list and print the country’s information if found.

E.g.:
>>> printCountry("Canada")
Canada, Area: 9976140.0, Population: 35295770
>>> printCountry("Winterfell")
I'm sorry, could not find Winterfell in the country list.

Question 3 (10 marks)

Write a function called countryByPop that takes an integer as a parameter. This function should do two things:
First, get the list of countries, and sort it in descending order of population using the selection sort algorithm.
Second, use the integer parameter and return the nth most populace country. (e.g., if a 3 is passed in, return the 3rd most populated country). Your code should ensure that a valid number is given as parameter (e.g., countryByPop(218) and countryByPop(0) should both fail gracefully)

E.g.:
>>> c = countryByPop(18)
>>> c
["Turkey",780580.0,75627384]
>>> c = countryByPop(-1)
Invalid parameter: -1
>>> print(c)
None 

Question 4 (10 marks)

Write a function called countryByArea that behaves similarly to q3. In this case, you will sort using the bubble sort algorithm according to the areaof each country.

E.g.:
>>> c = countryByArea(7)
>>> c
["India",3287590.0,1239240000]

Question 5 (20 marks)

Write a function called coincidingCountries that determines which countries are in the same order for both population and area. That is, if a country is both the 7th most populated and 7th largest by area, it should be reported by your function along with its rank (i.e., 7th).

E.g.:
>>> coincidingCountries()
3: ['United States of America', 9826675.0, 317506000]
5: ['Brazil', 8547906.0, 201032714]
17: ['Iran', 1648000.0, 77151000]
74: ['Guinea', 245857.0 10824200]
122: ['Bosnia-Herzgovina', 51129.0, 3791622]
179: ['Guam', 549.0, 159358]

Place Similar Order Now!

  • Our Support Staff are online 24/7
  • Our Writers are available 24/7
  • Most Urgent order is delivered with 6 Hrs
  • 100% Original Assignment Plagiarism report can be sent to you upon request.
[order_calculator]