Skip to content

Commit ccdc7d7

Browse files
committed
take back get_quadrant but teach it to handle poins on axises
1 parent 04a3445 commit ccdc7d7

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ A SICP'ish Points implemented in Python using hexlet-pairs.
1414
100
1515
>>> points.get_y(p)
1616
200
17+
>>> points.get_quadrant(p)
18+
1

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "hexlet-points"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
description = "A SICP'ish Points implemented in Python using hexlet-pairs"
55
authors = ["Hexlet Team <info@hexlet.io>"]
66
license = "ISC"

src/hexlet/points/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
"""SICP'ish Points implemented in Python using hexlet.pairs."""
44

5+
from typing import Optional
6+
57
from hexlet import pairs
68

79
__all__ = ( # noqa: WPS317
810
'make',
9-
'get_x', 'get_y',
11+
'get_x', 'get_y', 'get_quadrant',
1012
'to_string',
1113
)
1214

@@ -29,3 +31,16 @@ def get_y(point: pairs.Pair) -> int:
2931
def to_string(point: pairs.Pair) -> str:
3032
"""Return a string representation of the point."""
3133
return repr((get_x(point), get_y(point)))
34+
35+
36+
def get_quadrant(point: pairs.Pair) -> Optional[int]:
37+
"""Return a quadrant number for the point."""
38+
x, y = get_x(point), get_y(point)
39+
if not x or not y:
40+
return None
41+
return {
42+
(True, True): 1,
43+
(False, True): 2,
44+
(False, False): 3,
45+
(True, False): 4,
46+
}[(x > 0, y > 0)]

test/test_points.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,19 @@ def test_to_string():
1818
assert points.to_string(p) == '(100, 200)', (
1919
'Should return a proper representation.'
2020
)
21+
22+
23+
def test_get_quadrant(): # noqa: WPS210
24+
"""Test to_string conversion."""
25+
p1 = points.make(1, 1)
26+
p2 = points.make(-1, 1)
27+
p3 = points.make(-1, -1)
28+
p4 = points.make(1, -1)
29+
px = points.make(0, 1)
30+
py = points.make(1, 0)
31+
assert points.get_quadrant(p1) == 1, 'Should detect a quadrant #1.'
32+
assert points.get_quadrant(p2) == 2, 'Should detect a quadrant #2.'
33+
assert points.get_quadrant(p3) == 3, 'Should detect a quadrant #3.'
34+
assert points.get_quadrant(p4) == 4, 'Should detect a quadrant #4.'
35+
assert points.get_quadrant(px) is None, 'Should handle points on x axis.'
36+
assert points.get_quadrant(py) is None, 'Should handle points on y axis.'

0 commit comments

Comments
 (0)