Python代写:CSCA08 Airports and Routes




In Assignment 3, you will be implementing several functions that explore the airport and flight data on OpenFlights - “a tool that lets you map your flights around the world, search and filter them in all sorts of interesting ways, calculate statistics automatically, and share your flights and trips with friends and the entire world (if you wish).” OpenFlights on GitHub is an open source project. This handout explains the problem being solved and the tasks to complete for the assignment. Please read it carefully and in its entirety.

Goals of this Assignment

The purpose of this assignment is to give you practice with understanding a complicated real-world problem domain. The programming concepts you will practice are:

  • You will be able to write functions that involve dealing with data read from files.
  • You will be able to build, use and manipulate dictionaries.
  • You will learn how to use a new data structure called ‘sets’.
  • You will design algorithms and functions using the Function Design Recipe.
  • You will use helper functions to help organize your program and reduce repetitive code.
  • You will debug your code and use unit tests for testing.


OpenFlights ( is “a tool that lets you map your flights around the world, search and filter them in all sorts of interesting ways, calculate statistics automatically, and share your flights and trips with friends and the entire world (if you wish).” This assignment utilizes airport and flight data from OpenFlights, so we recommend that you go try it out for 10 minutes. It should make understanding the rest of this assignment much easier. You don’t need to create an account, just try these things:

Find the PUQ airplane icon at the bottom of South America. It represents an airport. Click it. A chat bubble appears with information in it, including the name of the airport, the abbreviation (PUQ), the city and country, and the number of flights.

Click the blue splat icon (looks like a paw with 4 fingers) in the bottom right of the chat bubble. This zooms you in on the airport, displays the city information at the top of the window, and shows you direct flights connected to PUQ.

Click on the icon next to “7 routes” in the city information. This displays a table of flights from PUQ to various cities. What do the columns all mean?

Continue to explore the website. What happens when you click on the various pieces of information in the table?

In this assignment, you will be implementing several functions that explore the flight data.
OpenFlights on GitHub ( is an open source project.

Preliminary Knowledge

Airport Codes

Airports have two types of codes: IATA and ICAO, issued by two different organizations. We will use the IATA code throughout. IATA codes are three-letter codes used to efficiently identify airports (e.g. the IATA code for Toronto Pearson International Airport is YYZ).

Python sets

So far in this course, you’ve seen lists, tuples, and dictionaries. Lists and tuples are ordered, but dictionaries are not. For example, {1: 2, 3: 4} and {3: 4, 1: 2} are equal.

Another related type is set. A set is an unordered collection of unique items. We use curly braces: {1, 2, 3, 4} is a set. As with dictionaries, the order of elements does not matter: {1, 2, 3, 4} == {3, 4, 2, 1}.

To add and remove items from sets, you use methods sets using set.add and set.remove. You can iterate over sets using for item in set. You can check the number of items using function len. To test for set membership, use item in set.

Each set item is unique:

>>> set1 = {1, 2, 3}
>>> set1.add(1)
>>> set1
{1, 2, 3}

You can also do math with sets!

>>> set1 = {1, 2, 3}
>>> set2 = {2, 1, 4}
>>> set1 - set2

Those are all the methods and functions that you need for this assignment, but you can see the full list by typing help(set) in the Python shell.

Our Custom Data Types

You will be working with the following custom data types for this assignment. These types describe how you will store the airport, route and flight information. They have been imported for you in the starter code, and you should use them in your type contracts.

Starter Files

Please download the Assignment 3 Files and extract the zip archive. After you extract the zip file, you should see a similar directory structure.

The following paragraphs briefly explain the files you have been given; more in-depth descriptions appear later in this handout.

Data files

We have provided airports.dat and routes.dat, which were downloaded from the OpenFlights website though the format has been modified for our assignment. These files will be discussed in more detail in the “Understanding the OpenFlights Data Files” section.

Python files you will modify,

The starter code for your part of this assignment is in these files. These are the files you will be writing all your assignment code in, and each of these files will be discussed in more detail in the “What to do > Part 1” and “What to do > Part 2” sections respectively.

Python files you will NOT modify

This file should NOT be modified. This file includes the code for creating new types, data to use in docstrings and the checker, and some constants that will be helpful as you write your code.

Types, constants, and test data from this file have already been imported for you in the starter code. You can use this data in your docstring examples and other tests, if you’d like.
This file should NOT be modified. As with previous assignments, we are providing some program code that uses your completed functions so you can see your program in action. More information about this is in the “The main program” section.
As with the previous assignments, we have provided a checker. Hopefully you’ve gotten in the habit of running it regularly!

Writing Functions

For each function you write in this assignment, remember to follow the Function Design Recipe we have been using in class to write the functions for this assignment.


All functions should have docstrings - you will need to write your own for the functions we have not provided them for. We will mark some of your docstrings in addition to your code, so we expect the docstrings to contain a type contract, a description, and two examples, where appropriate. Include preconditions when you think they are necessary.

Function input

You can assume all the functions will receive input that satisfies their type contracts. The files that you will need to read will be properly formed, as described in this handout. The dictionaries and other inputs to the other functions will be valid inputs however your functions should deal with receiving IATA codes for airports that do not appear in your dictionaries as specified below.

Top Down Design

The top-down design is an approach to designing and implementing the body of a function. Top-down design has you write down, in English, what the major steps are to solving the function.

After that, repeatedly select one of the steps. If you know how to directly translate the English into Python, do so. Otherwise, write a call on a function (that doesn’t exist yet!) that will do what the English says to do. Then use the Function Design Recipe to design and write that new function. When it’s time to write the body of the new function, use top-down design!


We will be testing and marking each of these functions individually. So, even if you can’t complete them all, you can earn marks for correctly implementing some of the functions.

Testing your Code

It is strongly recommended that you test each function as you write it. This will make your life easier if you write 2-3 lines of code, then test, you know where to look for your bug! If you wait until you’ve written a whole lot of code, it’s harder to figure out where to look.

As usual, follow the Function Design Recipe. Once you’ve implemented a function, run it on the examples in your docstring. Here are a few tips:

Can you think of any special cases for your functions? Will each function always work, or are there special cases to consider? Test each function carefully.

Once you are happy with the behaviour of a function, move to the next function, implement it, and test it.