Introduction to Computer Science
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CSC148H1 : Introduction to Computer Science
Assignment 2
Assignment 2: Martian Robot Society
You may work alone or with one other person on this assignment. If you form a partnership, you
must declare it on MarkUs (one person must invite the other and the other must accept) as soon
as you begin working together.
Note: The files society_ui.py and a2_starter_tests.py will be released early next week
(Week of March 21). An announcement will be made when they are available. For
now, you may ignore any reference made to them in this handout.
Learning Goals
After completing this assignment, you will be able to:
model different kinds of real-world hierarchical data using trees
implement recursive operations on trees (both non-mutating and mutating)
You will also continue to practice writing test suites for the code you are developing.
General coding guidelines
These guidelines are designed to help you write well-designed code that adheres to the interfaces
we have defined (and thus will be able to pass our test cases). Read these rules carefully.
DOs:
Throughout the starter code, watch for the note: This method must call itself recursively. Such
methods will earn credit only if they are recursive.
All code you write must be consistent with the provided code, docstrings, and the provided UI
and client code.
While writing your code, assume that all arguments passed to the methods and functions
you have been given in the starter code will respect the preconditions and type annotations
outlined in the starter code.
All code that you write should follow the function design recipe and the class design recipe.
You may create new private helper methods for the classes you have been given. If you do
create new private methods, you must provide type annotations for every parameter and return
value. You must also write a full docstring for such methods, as described in the function
design recipe. Doctests are not required, unless helpful to you. (Ideally, you should also write a
set of pytest cases for each method you write to help you test them.)
You may create new private attributes for the classes you have been given. If you do create
new private attributes you must give them a type annotation and include a description of them
in the class’s docstring as described in the class design recipe.
You may remove unused imports from the Typing module
DON'Ts:
You must not use list.sort or sorted in your assignment. Instead, the helper function called
merge has been written for you. You will find it useful when you're merging sorted lists
together.
Do NOT add public attributes or methods to any class. All added attributes and methods must
be private.
Do NOT change any function and method interfaces that we provided in the starter code. In
particular, do NOT:
change the interface (parameters, parameter type annotations, or return types) to any of
the methods or functions you have been given in the starter code.
change the type annotations of any public or private attributes you have been given in the
starter code.
Do NOT add any more import statements to your code, except for imports from the typing
module
Do NOT mutate an object in a method or a function if the docstring doesn’t say that it will be
mutated.
Never have a method return an alias to a list; if you need to return an existing list within a
method, make sure it is a copy of the original.
Introduction
It is the year 3142. Robots have finally overtaken the world, and their society involves a very strict
hierarchy where every robot knows their place in society. (Also, it should be noted that humans
had obviously re-located to Mars by this time, so the planet which the robots have overtaken is
Mars, not Earth.) As we've discussed in class, trees are a fundamental data structure used to
model all sorts of hierarchical data. In this assignment, you will be modelling the organization of
the Martian Robot Society using trees.
Every robot in the Martian Robot Society is considered a citizen of Mars. The nodes in our tree will
each represent one citizen. Citizens all have subordinate-superior relationships, where one citizen
may work under another. Additionally, some citizens are leaders of a specific district within the
society. All citizens that work under a leader are considered part of that district.
Note that a district could be a geographical area, or just a domain of responsibility, like Finance.
Setup and starter code
The code is in three layers:
society_hierarchy.py : Defines classes that keep track of information about the Martian robot
society. This is the only file that you will modify. All classes, methods, and functions that
you need to write are in this file.
society_ui.py : Defines a graphical user interface for interacting with information about the
Martian robot society. Run this module to interact with the user interface. You do not have to
read or understand the code in this file. Do not modify this file.
client_code.py : A layer of code that is between the user interface and the "back end" defined
in society_hierarchy.py. It uses the code you will be writing in society_hierarchy.py to make
the UI work. You may look through this file to see example usage of the methods and functions
you will implement. Do not modify this file.
In addition, we are providing:
citizens.csv : A sample file describing a robot society hierarchy. You can use it to create a
society for testing by: (a) uncommenting the soc = create_from_file_demo() line at the end of
society_hierarchy.py , or (b) using the "Load society from file" button in the UI and choosing
this file.
a2_starter_tests.py : Some basic tests cases that you should add to in order to test your own
code.
You should create tests of your own and add them to a2_starter_tests.py to thoroughly test your
code. There are many methods for you to implement in this assignment: manually testing them all
will prove difficult. The society_ui.py has only very basic functionality is not designed for
thorough testing. (It's also not too fleshed out or well-made and will fail silently on bad input), so
you shouldn't rely on it for testing. The UI is just meant as a tool to help you visualize the tree
when experimenting with your code.
Problem description
The code you'll be working on for this assignment, in file society_hierarchy.py , consists of three
main classes:
1. Citizen : A class representing a citizen in a Martian Robot Society.
2. Society : A class representing the entire Martian Robot Society.
3. DistrictLeader : A class representing a district leader, a special type of
citizen.
Please read through the following sections carefully to understand what is required for this
program. Afterwards, we'll provide a breakdown of the tasks.
Citizen class
As mentioned before, each node of the Martian Robot Society tree represents a citizen of this
society. Each citizen will have its own set of characteristics:their citizen ID number, manufacturer
(the name of which company manufactured this particular robot), model year, job, and their rating
(kind of like a credit score; basically a measure of how good of a citizen they are, represented as
an integer from 0 to 100).
Each citizen may have one superior and any number of subordinates. For example, consider the
following tree of citizens (attributes are labelled by name only for the root Citizen, to avoid clutter):
Notice that ID numbers are unique to citizens: the numbers used don't matter, but they will always
be positive integers and there cannot be any duplicates within the hierarchy.
There are 2 types of subordinate:
1. Direct subordinates: These are subordinates that work directly under another citizen. For
example, Citizen ID: 2 is a direct subordinate of Citizen ID: 6.
2. Indirect subordinates: These are subordinates that do not work directly under another citizen.
For example, being the subordinate of a subordinate. In our example above, Citizen ID: 7 is an
indirect subordinate of Citizen ID: 6.
DistrictLeader class
DistrictLeader is a subclass of Citizen. While district leaders are fairly similar to a regular citizen,
they also keep track of the district that they lead. All subordinates (both direct and indirect) are
said to be part of (or "belong to") the district. For example, consider the following hierarchy (district
leaders are highlighted in blue; attributes are labelled by name only for the root Citizen, to avoid
clutter)