39_golf: Python implementation

This commit is contained in:
Martin Thoma
2022-03-17 06:58:07 +01:00
parent bbd9332569
commit 030d61b85f
4 changed files with 1094 additions and 1 deletions

View File

@@ -22,7 +22,7 @@ jobs:
pip install -r 00_Utilities/python/ci-requirements.txt
- name: Test with pytest
run: |
pytest 01_Acey_Ducey/python 02_Amazing/python
pytest 01_Acey_Ducey/python 02_Amazing/python 39_Golf/python
- name: Test with mypy
run: |
mypy . --exclude 79_Slalom --exclude 27_Civil_War --exclude 38_Fur_Trader --exclude 81_Splat --exclude 09_Battle --exclude 40_Gomoko --exclude 36_Flip_Flop --exclude 43_Hammurabi --exclude 04_Awari --exclude 78_Sine_Wave --exclude 77_Salvo --exclude 34_Digits --exclude 17_Bullfight --exclude 16_Bug

1
.gitignore vendored
View File

@@ -29,6 +29,7 @@ out/
.python-version
Pipfile
venv/
.coverage
.DS_Store
.vs/

1004
39_Golf/python/golf.py Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,88 @@
import io
import math
import pytest
from golf import (
CircleGameObj,
GameObjType,
Golf,
Point,
RectGameObj,
get_distance,
is_in_rectangle,
odds,
to_degrees_360,
to_radians,
)
def test_odds():
n = 1000
p = sum(odds(50) for i in range(n)) / n
assert abs(p - 0.5) < 0.1
@pytest.mark.parametrize(
("p1", "p2", "expected"),
[
((0, 0), (0, 0), 0),
((0, 0), (1, 0), 1),
((0, 0), (0, 1), 1),
((0, 1), (0, 0), 1),
((1, 0), (0, 0), 1),
((0, 0), (2, 0), 2),
((0, 0), (0, 2), 2),
((0, 2), (0, 0), 2),
((2, 0), (0, 0), 2),
((0, 0), (1, 1), 2**0.5),
((2, 3), (4, 5), (2**2 + 2**2) ** 0.5),
],
)
def test_get_distance(p1, p2, expected):
assert get_distance(Point(*p1), Point(*p2)) == expected
@pytest.mark.parametrize(
("pt", "rect", "expected"),
[
(
CircleGameObj(1, 1, 1, GameObjType.BALL),
RectGameObj(0, 0, 2, 2, GameObjType.GREEN),
True,
),
(
CircleGameObj(1, 1, 1, GameObjType.BALL),
RectGameObj(0, 0, 1, 1, GameObjType.GREEN),
False,
),
],
)
def test_is_in_rectangle(pt, rect, expected):
assert is_in_rectangle(pt, rect) == expected
@pytest.mark.parametrize(
("angle", "radians"),
[
(0, 0),
(180, math.pi),
(360, 2 * math.pi),
],
)
def test_to_radians(angle, radians):
assert to_radians(angle) == radians
assert to_degrees_360(radians) == angle
def test_golf(monkeypatch, capsys):
handycap = 10
difficulty = 4
club = 10
swing = 10
monkeypatch.setattr(
"sys.stdin",
io.StringIO(f"10\na\n{handycap}\n{difficulty}\n{club}\n{swing}\nQUIT"),
)
Golf()
out, err = capsys.readouterr()
assert err == ""