Solution 1

class InvalidAreaUnitException(Exception):
    pass


class Country(object):
    def population_density(self):
        return float(self.population) / self.area_in_km2

    def gdp_per_capita(self):
        return float(self.total_gdp) / self.population

    def area(self, unit='km2'):
        if unit == 'km2':
            return self.area_in_km2
        elif unit == 'mi2':
            return self.area_in_km2 * 0.3861
        elif unit == 'acres':
            return self.area_in_km2 * 247
        elif unit == 'hectares':
            return self.area_in_km2 * 100
        else:
            raise InvalidAreaUnitException()


# vv Don't change these objects vv
usa = Country()
usa.name = 'United States of America'
usa.population = 325719178  # 325,719,178
usa.area_in_km2 = 9833520  # 9,833,520 km2
usa.total_gdp = 20199000000000  # 20.199 trillion

canada = Country()
canada.name = 'Canada'
canada.population = 35151728  # 35,151,728
canada.area_in_km2 = 9984670  # 9,984,670 km2
canada.total_gdp = 1836000000000  # 1.836 trillion
# ^^ Don't change these objects ^^

Country Methods

In your main module (at the right) we have defined a class Country and we've already created two instances: usa and canada with three attributes for each instance:

  • population
  • area_in_km2 (in square kilometers)
  • gdp (Gross domestic product in dollars)

Your job is to implement the following methods for the Country class:

(Important: Tests are rounding numbers to avoid decimal issues)

Population density
population_density() should return the number of inhabitants per square km. (population / area)

usa.population_density()  # 33.12
canada.population_density()  # 3.52

Dynamic area method

Area can be expressed in different units. These instances have area expressed in square kilometers but you can also use square miles or even Acres or Hectares.

The method area() of the class country should receive an optional parameter unit that can be either: km2, mi2, acres, hectares. By default, units is km2. If the unit passed is invalid (not one in the previous list) it should raise an InvalidAreaUnitException (that you must define). Examples:

usa.area(unit='km2')  # 9833520
usa.area()            # 9833520 (same result, km2 is default)
usa.area(unit='mi2')  #
usa.area(unit='acres')  #
usa.area(unit='hectares')  #

usa.area(unit='INVALID UNIT')  # will raise InvalidAreaUnitException

For simplicity, use the following conversion table (based in km2):

1km2 => 0.3861 mi2
1km2 => 247 acres
1km2 => 100 hectares

GDP per capita
It's not our intention to talk politics, but we want to know how evenly distributed wealth is in each country. Define a method gdp_per_capita() that returns the GDP per capita (gdp / population).

usa.gdp_per_capita()
canada.gdp_per_capita()

Test Cases

test area valid values - Run Test

def test_area_valid_values():
    assert round(usa.area(unit='km2')) == 9833520
    assert round(usa.area()) == 9833520
    assert round(usa.area(unit='mi2')) == 3796722
    assert round(usa.area(unit='acres')) == 2428879440
    assert round(usa.area(unit='hectares')) == 983352000

    assert round(canada.area(unit='km2')) == 9984670
    assert round(canada.area()) == 9984670
    assert round(canada.area(unit='mi2')) == 3855081
    assert round(canada.area(unit='acres')) == 2466213490
    assert round(canada.area(unit='hectares')) == 998467000

test population density - Run Test

def test_population_density():
    assert round(usa.population_density(), 2) == 33.12
    assert round(canada.population_density(), 2) == 3.52

test gdp per capita - Run Test

def test_gdp_per_capita():
    assert round(usa.gdp_per_capita()) == 62014
    assert round(canada.gdp_per_capita()) == 52231

test area invalid unit - Run Test

import pytest

def test_area_invalid_unit():
    with pytest.raises(InvalidAreaUnitException):
        usa.area(unit='INVALID')
class Country(object): # your methods here pass # vv Don't change these objects vv usa = Country() usa.name = 'United States of America' usa.population = 325719178 # 325,719,178 usa.area_in_km2 = 9833520 # 9,833,520 km2 usa.total_gdp = 20199000000000 # 20.199 trillion canada = Country() canada.name = 'Canada' canada.population = 35151728 # 35,151,728 canada.area_in_km2 = 9984670 # 9,984,670 km2 canada.total_gdp = 1836000000000 # 1.836 trillion # ^^ Don't change these objects ^^