For this assignment, we are giving you some files, including Python starter code files. Please download the Assignment 1 Files and extract the zip archive.
There are two starter code Python files and a Python program that helps you check (not fully test!) your solutions for Python style violations:
tickets.py
This file contains some code that sets up your use of constants
from constants.py, a helper function for you to use (see the table of
functions to implement below), solutions
for several required functions for you to use as examples, as well as headers and
docstrings (but not bodies) for several functions you are to write, to help you
get started. Your job is to complete this file.
constants.py
This file contains some useful constants that you should use in your solution. You will not modify this file.
a1_checker.py
This is a checker program that you should use to check your
code. See below for more information
about a1_checker.py.
The typical airplane boarding pass contains many different pieces of information, including passenger information, seat, flight number, flight time, and so on. In this assignment, we'll work with our own version of information you might find on a boarding pass.
We'll refer to the data we will work with just as a "ticket". A ticket is a string, where different pieces of the string represent different kinds of information. Our ticket will include the following information:
A valid ticket will have either 17 or 21 characters. Below, we describe what the different index ranges mean in a ticket.
YR to YR+3, inclusive, represent
the year the ticket is for. In a valid year, these four characters must be digits that
represent a positive number. We do not place any other restrictions on the validity of
the year field.
MON and MON+1 represent the month the
ticket is for. In a valid month, these two characters must be digits that represent a
number between 1 and 12, inclusive.
DAY and DAY+1 represent the day the
ticket is for. In a valid day, these two characters must be digits that represent a
number between 1 and the maximum number of days for the given month and year. Recall
that months January, March, May, July, August, October and December have 31 days, months
April, June, September, and November have 30 days, and the month of February has 28 days
in a non-leap year and 29 days in a leap year. A leap year is a positive integer
multiple of 4 (except for years evenly divisible by 100, but not by 400).
DEP to DEP+2, inclusive, represent the
code of the departure airport. In a valid code, these three characters must be letters.
ARR to ARR+2, inclusive, represent the
code of the arrival airport. In a valid code, these three characters must be letters. In a
valid ticket, departure and arrival airports must be different.
ROW and ROW+1 represent the row number of
the ticket. In a valid row number, these two characters must be digits.
SEAT represents the seat in a given row. A valid seat is
the value of one of the
constants SA, SB, SC, SD, SE,
or SF, which for us represent seats "A", "B", "C", "D", "E", and "F", but can
be translated into a different language by changing the values of these constants. Our
aircraft all have six seats in each row, with three seats on each side of the
aisle: SA,
SB, SC on one side, and SD, SE,
and SF on the other. Seats SA and SF are window
seats, seats SB and SE are middle seats, and seats SC
and SD are aisle seats.
FFN are special and represent the frequent flyer
number for the passenger this ticket belongs to, if they have one. The value
of FFN must be 17 on a valid ticket, meaning the frequent flyer number always
appears as the last record in the ticket string. Some tickets do not have a record for
frequent flyer number, meaning the corresponding string is the empty
string ''. Some tickets have a record for frequent flyer number, in which case
this number appears between indices 17 and 20, inclusive. The empty string is a valid
frequent flyer number. A valid non-empty frequent flyer number must contain exactly four
digits, and the sum of the first three digits modulo 10 must be the same as the last
(fourth) digit.
YR = 0, MON =
4, DAY = 6, DEP = 8, ARR = 11, ROW =
14, SEAT = 16, and FFN = 17. The ticket below is for
December 21, 2023 for a flight from YYZ to YEG in seat 25F. The passenger's flyer number
is 4442. (Note that 4 + 4 + 4 mod 10 is 2).
20231221YYZYEG25F4442
YR = 10, MON =
8, DAY = 6, DEP = 0, ARR = 3, ROW =
14, SEAT = 16, and FFN = 17. The ticket below is for
September 20, 2024 for a flight from YYZ to LAS in seat 16D. The passenger's flyer number
is 4420. (Note that 4 + 4 + 2 mod 10 is 0).
YYZLAS2009202416D4420
Constants are special variables whose values should not change once assigned. A different
naming convention (uppercase pothole) is used for constants, so that programmers know to not
change their values. For example, in the starter code, the constant YR (for
"year") is assigned the value 0 at the beginning of the module and the value
of YR should never change in your code. When writing your code, if you need to
use the index at which a ticket contains the year, you should use YR and not the
value 1. The same goes for the other constant values.
Using constants simplifies code modifications and improves readability and changeability. For
example, if we later decide to use French instead of English, we would only have to make the
changes in one place (the WINDOW, AISLE, and MIDDLE
assignment statements in the file constants.py), rather than throughout the
program. This also makes our code more readable — whether we use 1 or any
other index to store the year on the ticket, we write our code using the
constant YR so it is clear to the reader what we mean.
You should read the file constants.py carefully to understand
the purpose of each defined constant. You must use these constants in your code and not the
literal values. For example, you must use YR instead of 0 to earn
full marks.
In the starter code file tickets.py, complete the following function
definitions. Note that some are already provided for you to use as examples!
Use the Function Design Recipe that you have been learning in this course. We have included
the type contracts in the following table; please read through the table to understand how the
functions will be used.
We will be evaluating your docstrings in addition to your code. Please include at least two examples in each docstring. You will need to paraphrase the full descriptions of the functions to get an appropriate docstring description. Your docstring examples should be valid doctests — we will run them as part of evaluation of your program.
Advice: Start working on these functions as soon as the assignment is released. You have already learned all material needed to complete them.
|
Function name: (Parameter types) -> Return type |
Full Description (paraphrase to get a proper docstring description) |
|---|---|
get_date:(str) -> str
|
The parameter represents the ticket. The function should return the date on the given
ticket in the format
For example, if the constants have values
As another example, if the constants have values |
get_year:(str) -> str
|
The parameter represents the ticket. The function should return the year on the given
ticket in the format
See function |
get_month:(str) -> str
|
The parameter represents the ticket. The function should return the month on the given
ticket in the format
See function |
get_day:(str) -> str
|
The parameter represents the ticket. The function should return the day on the given
ticket in the format
See function |
get_departure:(str) -> str
|
The parameter represents the ticket. The function should return the departure airport code on the given ticket. You may assume the input ticket is valid.
For example, if the constant
As another example, if the constant |
get_arrival:(str) -> str
|
The parameter represents the ticket. The function should return the arrival airport code on the given ticket. You may assume the input ticket is valid.
For example, if the constant
As another example, if the constant |
get_row:(str) -> int
|
The parameter represents the ticket. The function should return the row number on the given ticket. You may assume the input ticket is valid.
For example, if the constant |
get_seat:(str) -> str
|
The parameter represents the ticket. The function should return the seat on the given ticket. You may assume the input ticket is valid.
For example, if the constant |
get_ffn:(str) -> str
|
The parameter represents the ticket. The function should return the frequent flyer number on the given ticket. You may assume the input ticket is valid.
For example, if the input ticket string is |
Advice: Start working on these functions as soon as the assignment is released. You can complete the headers and docstrings for all of them now, and then complete the implementations after the week of Sep 25 classes are done.
|
Function name: (Parameter types) -> Return type |
Full Description (paraphrase to get a proper docstring description) |
|---|---|
is_valid_seat:(str, int, int) -> bool
|
The first parameter represents the ticket. The second parameter represents the number of
the first row in the plane. The third parameter represents the number of the last row in
the plane. The function should return True if and only if the seat, including the row
number and the seat within that row, of this ticket is
valid. See above for explanations of ticket validity. You may
assume that the format of the ticket string is valid: see provided
helper function
For example, if the values of the constants are
If, however, the input ticket string is
Similarly, if the input ticket string is |
is_valid_ffn:(str) -> bool
|
The parameter represents the ticket. The function should return True if and only if the
frequent flyer number of this ticket is valid. See above for
explanations of ticket validity. You may assume that the format of the
ticket string is valid: see provided helper
function |
is_valid_date:(str) -> bool
|
The parameter represents the ticket. The function should return True if and only if the
date of this ticket is valid. See above for explanations of ticket
validity. You may assume that the format of the ticket string is valid:
see provided helper function Hint: this is possibly the most challenging function in this assignment. You may want to implement it last. |
is_valid_ticket:(str, int, int) -> bool
|
The first parameter represents the ticket. The second parameter represents the number of
the first row in the plane. The third parameter represents the number of the last row in
the plane. The function should return True if and only if the ticket is in valid format
and all of the ticket information on this ticket is
valid. See above for explanations of ticket validity. Hint:
use the provided helper function |
Advice: Start working on these functions as soon as the assignment is released. You can complete the headers and docstrings for all of them now, and then complete the implementations after the week of Sep 25 classes are done.
|
Function name: (Parameter types) -> Return type |
Full Description (paraphrase to get a proper docstring description) |
|---|---|
visits_airport:(str, str) -> bool
|
The first parameter represents the ticket. The second parameter represents the airport
code. The function should return |
connecting:(str, str) -> bool
|
The parameters represent two tickets. The function should return |
adjacent:(str, str) -> bool
|
The parameters represent two tickets. The function should return |
behind:(str, str) -> bool
|
The parameters represent two tickets. The function should return |
get_seat_type:(str) -> str
|
The parameter represents the ticket. The function should return the type of the seat on
the given ticket: |
|
Function name: (Parameter types) -> Return type |
Full Description (paraphrase to get a proper docstring description) |
|---|---|
change_seat:(str, str, str) -> str
|
Note: this function is not required; completing it will earn you bonus marks. The first parameter represents the ticket. The second parameter represents the row number. The third parameter represents the seat within that row. The function should return a new ticket that is in the same format as the input ticket, has the same departure, arrival, date, and frequent flyer number as the input ticket, and has a new seat information with the given row and seat. You may assume the ticket and the new seat information are valid.
For example, if the values of the constants are |
change_date:(str, str, str, str) -> str
|
Note: this function is not required; completing it will earn you bonus marks. The first parameter represents the ticket. The second parameter represents the day. The third parameter represents the month. The last parameter represents the year. The function should return a new ticket that is in the same format as the input ticket, has the same departure, arrival, seat information, and frequent flyer number as the input ticket, and has a new date. You may assume the ticket and the new date information are valid.
For example, if the values of the constants are |
As we discuss in section Constants above, your code should make use of the provided constants. If the value of one of those constants were changed, and your program rerun, your functions should work with those new values.
Your docstring examples should reflect the given values of the constants in the provided starter code, and do not need to change.
Your tickets.py file should contain the starter code, plus the function
definitions specified above.
tickets.py must not include any calls to the print
and input functions. Do not add any import statements.
Also, do not include any function calls or other code outside of the function
definitions.
We are providing a checker module (a1_checker.py) that tests two things:
To run the checker, open a1_checker.py and run it. Note: the checker file should
be in the same directory as your tickets.py, as provided in the
starter code zip file. Be sure to scroll up to the top and read all messages!
If the checker passes for both style and types:
If the checker fails, carefully read the message provided:
Read the error messages to identify the problematic functions, review the function specifications in the handout, and fix your code.
Make sure the checker passes before submitting.
These are the aspects of your work that may be marked for A1:
Your functions should perform as specified. Correctness, as measured by our tests, will count for the largest single portion of your marks. Once your assignment is submitted, we will run additional tests not provided in the checker. Passing the checker does not mean that your code will earn full marks for correctness.
Note that for full marks all docstring examples you provide should run without producing errors.
The very last thing you do before submitting should be to run the checker program one last time.
Otherwise, you could make a small error in your final changes before submitting that causes your code to receive zero for correctness.
Submit tickets.py on MarkUs by following the instructions on the course
website. Remember that spelling of filenames, including case, counts: your file must be named
exactly as above.