Define a class Rectangle
which stores the bottom-left coordinates (x0
, y0
)
and top-right coordinates (x1
, y1
) for each instance. The class should have
the following methods:
def __init__(self, x0: float, y0: float, x1: float, y1: float):
...
def area(self) -> float:
"""Returns the area of the rectangle."""
...
def contains(self, x: float, y: float) -> bool:
"""Returns true iff the point (x, y) falls within the rectangle"""
...
- A class definition
Rectangle
defined as above - A few basic
assert
tests of thearea
andcontains
methods - A brief (paragraph or so) description of your approach and any challenges you ran into
- Look up the formula for the area of a rectangle, if you need to
- Remember that you can't write
a < x < b
in Python; instead, you have to writea < x and x < b
Modify the class so that instead of storing the bottom-left and top-right coordinates, you simply store the bottom-left coordinates, the height, and the width, of the rectangle.
Define a class Employee
which stores an employee's name, title, and employee
ID, as follows.
def __init__(self, name: str, title: str, employee_id: int):
...
Then, define a function or generator which reads Employee
data from a text
file and yields fully-populated Employee
instances.
from typing import Iterator
def read_employees(path: str) -> Iterator[Employee]:
...
This function should assume the data is formatted similarly to the attached
employees.txt
.
- A class definition
Employee
defined as above - A function or generator
read_employees
defined as above - A few basic
assert
tests ofread_employees
- A brief (paragraph or so) description of your approach and any challenges you ran into
- Use the
with
construction inread_employees
- An example of reading four lines at a time appears in Lecture 8