Implemented doctests for geometry-related classes (#12368)

* 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 <tianyizheng02@gmail.com>
This commit is contained in:
Matej 2024-12-30 16:04:28 +01:00 committed by GitHub
parent a2be5adf67
commit f24ddba5b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 2 deletions

View File

@ -23,7 +23,7 @@ unitlist = (
+ [cross(rs, cs) for rs in ("ABC", "DEF", "GHI") for cs in ("123", "456", "789")] + [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} 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(): def test():
@ -172,7 +172,8 @@ def solved(values):
def from_file(filename, sep="\n"): def from_file(filename, sep="\n"):
"Parse a file into a list of strings, separated by sep." "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): def random_puzzle(assignments=17):

View File

@ -48,6 +48,18 @@ class Side:
Side(length=5, angle=Angle(degrees=45.6), next_side=None) Side(length=5, angle=Angle(degrees=45.6), next_side=None)
>>> Side(5, Angle(45.6), Side(1, Angle(2))) # doctest: +ELLIPSIS >>> 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(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 length: float
@ -162,6 +174,19 @@ class Polygon:
>>> Polygon() >>> Polygon()
Polygon(sides=[]) 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) sides: list[Side] = field(default_factory=list)
@ -207,6 +232,10 @@ class Rectangle(Polygon):
30 30
>>> rectangle_one.area() >>> rectangle_one.area()
50 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: def __init__(self, short_side_length: float, long_side_length: float) -> None: