Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented doctests for geometry-related classes #12368

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
5 changes: 3 additions & 2 deletions data_structures/arrays/sudoku_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def cross(items_a, items_b):
+ [cross(rs, cs) for rs in ("ABC", "DEF", "GHI") for cs in ("123", "456", "789")]
)
units = {s: [u for u in unitlist if s in u] for s in squares}
peers = {s: set(sum(units[s], [])) - {s} for s in squares} # noqa: RUF017
peers = {s: {x for u in units[s] for x in u} - {s} for s in squares}


def test():
Expand Down Expand Up @@ -172,7 +172,8 @@ def unitsolved(unit):

def from_file(filename, sep="\n"):
"Parse a file into a list of strings, separated by sep."
return open(filename).read().strip().split(sep) # noqa: SIM115
with open(filename) as file:
return file.read().strip().split(sep)


def random_puzzle(assignments=17):
Expand Down
29 changes: 29 additions & 0 deletions geometry/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ class Side:
Side(length=5, angle=Angle(degrees=45.6), next_side=None)
>>> Side(5, Angle(45.6), Side(1, Angle(2))) # doctest: +ELLIPSIS
Side(length=5, angle=Angle(degrees=45.6), next_side=Side(length=1, angle=Angle(d...
>>> Side(-1)
Traceback (most recent call last):
...
TypeError: length must be a positive numeric value.
>>> Side(5, None)
Traceback (most recent call last):
...
TypeError: angle must be an Angle object.
>>> Side(5, Angle(90), "Invalid next_side")
Traceback (most recent call last):
...
TypeError: next_side must be a Side or None.
"""

length: float
Expand Down Expand Up @@ -162,6 +174,19 @@ class Polygon:

>>> Polygon()
Polygon(sides=[])
>>> polygon = Polygon()
>>> polygon.add_side(Side(5)).get_side(0)
Side(length=5, angle=Angle(degrees=90), next_side=None)
>>> polygon.get_side(1)
Traceback (most recent call last):
...
IndexError: list index out of range
>>> polygon.set_side(0, Side(10)).get_side(0)
Side(length=10, angle=Angle(degrees=90), next_side=None)
>>> polygon.set_side(1, Side(10))
Traceback (most recent call last):
...
IndexError: list assignment index out of range
"""

sides: list[Side] = field(default_factory=list)
Expand Down Expand Up @@ -207,6 +232,10 @@ class Rectangle(Polygon):
30
>>> rectangle_one.area()
50
>>> Rectangle(-5, 10)
Traceback (most recent call last):
...
TypeError: length must be a positive numeric value.
"""

def __init__(self, short_side_length: float, long_side_length: float) -> None:
Expand Down