Книга: Practical Programming, Fourth Edition
Назад: Classifying What You’ve Learned
Дальше: Chapter 15: Testing and Debugging

Exercises

Here are some exercises for you to try on your own.

  1. In this exercise, you will implement the class Country, which represents a country with a name, a population, and an area.

    1. Here is a sample interaction from the Python shell:

      ​ ​>>>​​ ​​canada​​ ​​=​​ ​​Country(​​'Canada'​​,​​ ​​36991981,​​ ​​9984670)​
      ​ ​>>>​​ ​​canada.name​
      ​ 'Canada'
      ​ ​>>>​​ ​​canada.population​
      ​ 36991981
      ​ ​>>>​​ ​​canada.area​
      ​ 9984670

      This code cannot be executed yet because the class Country does not exist. Define Country with an initializer (method __init__) that has four parameters: a country, its name, its population, and its area.

    2. Consider this code:

      ​ ​>>>​​ ​​canada​​ ​​=​​ ​​Country(​​'Canada'​​,​​ ​​36991981,​​ ​​9984670)​
      ​ ​>>>​​ ​​usa​​ ​​=​​ ​​Country(​​'United States of America'​​,​​ ​​342034432,​​ ​​9826675)​
      ​ ​>>>​​ ​​canada.is_larger(usa)​
      ​ True

      In the class Country, define a method named is_larger that takes two Country objects and returns True if and only if the first has a larger area than the second.

    3. Consider this code:

      ​ ​>>>​​ ​​canada.population_density()​
      ​ 3.704877677479576

      In the class Country, define a method named population_density that returns the population density of the country (people per square kilometer).

    4. Consider this code:

      ​ ​>>>​​ ​​usa​​ ​​=​​ ​​Country(​​'United States of America'​​,​​ ​​342034432,​​ ​​9826675)​
      ​ ​>>>​​ ​​print(usa)​
      ​ United States of America has a population of 342034432 and is 9826675
      ​ square km.

      In class Country, define a method named __str__ that returns a string representation of the country in the format shown here.

    5. After you have written __str__, this session shows that a __repr__ method would be useful:

      ​ ​>>>​​ ​​canada​​ ​​=​​ ​​Country(​​'Canada'​​,​​ ​​36991981,​​ ​​9984670)​
      ​ ​>>>​​ ​​canada​
      ​ <exercise_country.Country object at 0x7f2aba30b550>
      ​ ​>>>​​ ​​print(canada)​
      ​ Canada has population 36991981 and is 9984670 square km.
      ​ ​>>>​​ ​​[canada]​
      ​ [<exercise_country.Country object at 0x7f2aba30b550>]
      ​ ​>>>​​ ​​print([canada])​
      ​ [<exercise_country.Country object at 0x7f2aba30b550>]

      Define the __repr__ method in Country to produce a string that behaves like this:

      ​ ​>>>​​ ​​canada​​ ​​=​​ ​​Country(​​'Canada'​​,​​ ​​36991981,​​ ​​9984670)​
      ​ ​>>>​​ ​​canada​
      ​ Country('Canada', 36991981, 9984670)
      ​ ​>>>​​ ​​[canada]​
      ​ [Country('Canada', 36991981, 9984670)]
  2. In this exercise, you will implement a Continent class, which represents a continent with a name and a list of countries. Class Continent will use class Country from the previous exercise. If Country is defined in another module, you’ll need to import it.

    1. Here is a sample interaction from the Python shell:

      ​ ​>>>​​ ​​canada​​ ​​=​​ ​​country.Country(​​'Canada'​​,​​ ​​36991981,​​ ​​9984670)​
      ​ ​>>>​​ ​​usa​​ ​​=​​ ​​country.Country(​​'United States of America'​​,​​ ​​342034432,​
      ​ ​...​​ ​​9826675)​
      ​ ​>>>​​ ​​mexico​​ ​​=​​ ​​country.Country(​​'Mexico'​​,​​ ​​132452034,​​ ​​1943950)​
      ​ ​>>>​​ ​​countries​​ ​​=​​ ​​[canada,​​ ​​usa,​​ ​​mexico]​
      ​ ​>>>​​ ​​north_america​​ ​​=​​ ​​Continent(​​'North America'​​,​​ ​​countries)​
      ​ ​>>>​​ ​​north_america.name​
      ​ 'North America'
      ​ ​>>>​​ ​​for​​ ​​country​​ ​​in​​ ​​north_america.countries:​
      ​  print(country)
      ​ 
      ​ Canada has a population of 36991981 and is 9984670 square km.
      ​ United States of America has a population of 342034432 and is 9826675
      ​ square km.
      ​ Mexico has a population of 132452034 and is 1943950 square km.
      ​ ​>>>​

      The code cannot be executed yet, because class Continent does not exist. Define Continent with an initializer (method __init__) that has three parameters: a continent, its name, and its list of Country objects.

    2. Consider this code:

      ​ ​>>>​​ ​​north_america.total_population()​
      ​ 511478447

      In class Continent, define a method named total_population that returns the sum of the populations of the countries on this continent.

    3. Consider this code:

      ​ ​>>>​​ ​​print(north_america)​
      ​ North America
      ​ Canada has a population of 36991981 and is 9984670 square km.
      ​ United States of America has a population of 342034432 and is 9826675
      ​ square km.
      ​ Mexico has a population of 132452034 and is 1943950 square km.

      In class Continent, define a method named __str__ that returns a string representation of the continent in the format shown here.

  3. In this exercise, you’ll write __str__ and __repr__ methods for several classes.

    1. In class Student, write a __str__ method that includes all the Member information and in addition includes the student number, the list of courses taken, and the list of current courses.

    2. Write __repr__ methods in classes Member, Student, and Faculty.

    Create a few Student and Faculty objects and call str and repr on them to verify that your code does what you want it to.

  4. Write a class called Nematode to keep track of information about C. elegans, including a variable for the body length (in millimeters; they are about 1 mm in length), gender (either hermaphrodite or male), and age (in days).

    Include methods __init__, __repr__, and __str__.

  5. Consider this code:

    ​ ​>>>​​ ​​segment​​ ​​=​​ ​​LineSegment(Point(1,​​ ​​1),​​ ​​Point(3,​​ ​​2))​
    ​ ​>>>​​ ​​segment.slope()​
    ​ 0.5
    ​ ​>>>​​ ​​segment.length()​
    ​ 2.23606797749979

    In this exercise, you will write two classes, Point and LineSegment, so that you can run this code and get the same results.

    1. Write a Point class with an __init__ method that takes two numbers as parameters.

    2. In the same file, write a LineSegment class whose initializer takes two Points as parameters. The first Point should be the start of the segment.

    3. Write a slope method in the class LineSegment that computes the slope of the segment. (Hint: The slope of a line is rise over run.)

    4. Write a length method in class LineSegment that computes the length of the segment. (Hint: Use x ** n to raise x to the nth power. To compute the square root, raise a number to the (1/2) power or use math.sqrt.)

Назад: Classifying What You’ve Learned
Дальше: Chapter 15: Testing and Debugging