! CSC148H1 S Pages Assignment 0

Assignment 0

Assignment 0: The Gym

Due date: Tuesday, January 28, 2020 before noon sharp (not 12:10)

You must complete this assignment individually. Partners will be permitted only on Assignments 1 and 2.

Learning goals

This assignment is called “Assignment 0” because it is considerably smaller than Assignments 1 and 2. Its purpose

is to make sure that you have understood the basics of object-oriented programming in Python and are ready to

move on with the more advanced topics in the course. By the end of this assignment you should be able to:

Implement a class in Python from a provided interface, including:

Using instance attributes that enable the class methods to provide their services to client code,

Enforcing representation invariants that record important facts about implementation decisions, and

Implementing initializers and other methods

Choose unit tests that can convincingly demonstrate that a method works according to its docstring for all

possible inputs

Implement those test cases in pytest

Interpret the results of doctests and unit tests to assess the correctness of methods

You should also have developed these habits:

Implementing tests for a method before implementing the method

Running your test suite throughout the development of your code

The domain

In this assignment, you will work with three Python classes that manage the schedule of a gym. A Gym consists of

many rooms and each room has a maximum capacity. The gym follows a Schedule where, at different times in the

day, a room may have a Workout Class scheduled in it. A workout class is taught by an Instructor provided they

have the proper qualifications (in the form of certificates) to teach it. A gym offering is a workout class taking

place in a specific room, at a specific time, and taught by a specific instructor. Clients can register for these

offerings provided there is enough space in the room.

We will assume that all offerings begin on the hour and last for 1 hour. We will also assume that all rooms have a fixed

capacity (i.e., the room capacity does not change for different workout classes).

The program

The code consists of three Python classes:

An instance of WorkoutClass represents a type of workout class that can be offered at a gym, such as

advanced pilates . It is worthy of being a Python class rather than just a string because it stores information

about the qualifications required for teaching that type of workout class.

An instance of Instructor represents an instructor who may teach classes at a gym.

An instance of Gym represents a gym, with all of its instructors and its schedule of workout classes. It contains

instances of the other classes.

The code for WorkoutClass has been completed for you. But many methods for the Instructor and Gym classes

are missing their implementation.

Representing the data

We have already decided how data will be represented for you in this assignment. For example, clients are

represented as strings and instructors are represented as a custom Python class. You should read the provided

starter code carefully to see how all the data is stored.

The most complex data structure is referred to by the _schedule attribute in the Gym class. The diagram below

should help you visualize the data structure:

Input to the program

Input to the program comes from a file. We have provided a function called load_data that reads and parses an

input file, and returns a Gym object that represents all the information in that file. The load_data function calls a

number of helper functions, and uses the three Python classes to build up the instance of Gym to be returned.

The starter code contains an example input file. It contains multi-line chunks of data separated by an extra

newline character (which give the appearance of a blank line). Each chunk begins with one of five strings

( Instructor , Class , Room , Offerings , or Registrations ) and is followed by information defining the described

thing. Each chunk is parsed differently depending on how it begins. The code handles any input file that follows

this format.

Although we have provided all the code for reading a gym data file, we recommend that you review this code

(especially if you are not confident about reading from files in Python).

Note that some sections of the input file include date and time values, such as ‘2020-01-14 09:00’ . The code we

have provided sends both a specific date and time string (such as ‘2020-01-14 09:00’ ) and a string specifying its

format (our code expects the input file to follow the format ‘%Y-%m-%d %H:%M’ ) to the function

datetime.strptime . The function returns an instance of class datetime that represents the given date and time.

See the Python documentation for more details.

This format is case-sensitive (e.g., the datetime library represents a month with %m and a minute with %M ).

Your task

Save a0.zip to your computer and extract its contents. Copy all the extracted files and folders into your

csc148/assignments/a0 folder. Specifically, a0.zip includes:

gym.py : Starter code that you will complete and submit.

a0_sample_test.py : Some basic tests cases that you will add to.

athletic-centre.txt : An example data file that you can use to run your program.

For reference: A document describing the class design recipe that we use in this course, and the code example

that it uses.

Your tasks are to:

1. Implement the methods defined in class Instructor . There are many ways we could have chosen to

represent the data, and you may find it interesting to consider alternatives. But you must use the attributes

that we defined in the class docstring, and must ensure that the representation invariants hold at all times in

between method calls. Do not add any new attributes to this class.

2. Implement the methods defined in class Gym . Again, you must use the attributes that we defined in the class

docstring, and must ensure that the representation invariants hold at all times in between method calls. Do

not add any new attributes to this class. Class Gym will use class Instructor and WorkoutClass heavily, but it

must not use any of their private attributes.

If a docstring does not specify what to do in a particular scenario, you may decide what the method will do (we

won’t test that scenario). You are welcome, and encouraged, to add private helper methods as you see fit.

However, you must not add any public methods.

Helpful hints

The trickiest part of this assignment is the nested structures in class Gym . It will help immensely if you can keep

straight which data (and type) you are referring to at each moment in the code. A local variable inside your

methods doesn’t have a type contract to help, so use a carefully chosen name to remind you what kind of object it


You may find the following useful. When you are iterating over a compound object (e.g., a list of tuples, a

dictionary), you can extract its pieces in the for statement. Here are some examples:

# Example 1 – List of Tuples

world = [(‘AFG’, ‘Afghanistan’, 22720000),

(‘CAN’, ‘Canada’, 31147000),

(‘ARG’, ‘Argentina’, 37032000),

(‘GHA’, ‘Ghana’, 20212000)]

for code, countryName, population in world:

# Loop body omitted.

# Example 2 – Dictionary

code_to_continent = {‘AFG’: ‘Asia’,

‘CAN’: ‘North America’,

‘ARG’: ‘South America’,

‘GHA’: ‘Africa’


for code, continent in code_to_continent.items():

# Loop body omitted.

A list of tuples can be sorted. If you call the sort method on a list of tuples it will, by default, sort based on the

first element of the tuple. For example,

>>> world = [(‘AFG’, ‘Afghanistan’, 22720000),

(‘CAN’, ‘Canada’, 31147000),

(‘ARG’, ‘Argentina’, 37032000),

(‘GHA’, ‘Ghana’, 20212000)]

>>> world.sort()

>>> print(world)

[(‘AFG’, ‘Afghanistan’, 22720000), (‘ARG’, ‘Argentina’, 37032000),

(‘CAN’, ‘Canada’, 31147000), (‘GHA’, ‘Ghana’, 20212000)]

About testing

Be sure to test your code thoroughly. For methods that we outline in the starter code, you may use our doctests

and our tests in a0_sample_test.py as a starting point, but you should augment these with much more thorough

testing of your own. For any helper methods that you define, you will be starting from scratch with the testing.

Your assignment grade will be based on the autotesting that we do, and you can be sure we’ll try to break your code

as hard as we can, so you should also!

The most efficient way to produce code that works is to create and run your test cases as early as possible, and to

re-run them after every change to your code.


Take some time to polish up. This step will improve your mark, but it also feels so good. Here are some things you

can do:

Pay attention to any violations of the Python style guidelines that PyCharm points out. Fix them!

In each module, run the provided python_ta.check_all() code to check for errors. Fix them!

Check your docstrings for any helper methods you wrote to make sure they are precise and complete, and

that they follow the conventions of the Function Design Recipe and the Class Design Recipe.

Read through and polish your internal comments.

Remove any code you added just for debugging, such as calls to the print function.

Remove any pass statement where you have added the necessary code.

Remove the word “TODO” wherever/whenever you have completed the task.

Take pride in your gorgeous code!

Submission instructions

1. Login to MarkUs.

2. DOES YOUR CODE RUN PROPERLY on the Teaching Lab machines?! Your code will be tested on the Teaching

Lab machines, so it must run in that environment. Check again before submitting.

3. Submit the file gym.py .

4. On a Teaching Lab machine, download the file you submitted into a brand-new folder, and test your code

thoroughly again to be sure that you submitted the correct version of the correct file. If you accidentally

submit the starter code, for example, you will get 0 for the assignment. 🙁

5. Congratulations, you are finished with your first assignment in CSC148! Go have some chocolate or do a

cartwheel. 🙂

For course-related questions, please contact csc148-2020-01 at cs.toronto.edu.

jQuery(‘.content’).click(function() { jQuery(‘.dropdown-list’).hide(); }); jQuery(‘nav’).click(function(event){

event.stopPropagation(); }); jQuery(‘.dropdown-toggle’).click(function(event){

jQuery(event.target).siblings(‘.dropdown-list’).toggle(); });



2020 Winter




Office Hours

Discussion Board

Weekly Preps

Weekly Labs

Course Team

Assignment Materials

Midterm and Final


Software Guide

CS Teaching Labs

Ramp-up Session



Leave a Reply

Your email address will not be published. Required fields are marked *