Skip to content

Commit

Permalink
Implemented doctests for geometry-related classes (#12368)
Browse files Browse the repository at this point in the history
* Implemented doctests for geometry-related classes

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Removed unused noqa directive

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* refactored sudoku_solver.py

* refactored sudoku_solver.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* context manager for file handling changed too in from_file function

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Tianyi Zheng <[email protected]>
  • Loading branch information
3 people authored Dec 30, 2024
1 parent a2be5ad commit f24ddba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
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)
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

0 comments on commit f24ddba

Please sign in to comment.