Skip to content

Commit 7488285

Browse files
committed
solved(python): baekjoon 17387
1 parent e057f14 commit 7488285

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

baekjoon/python/17387/__init__.py

Whitespace-only changes.

baekjoon/python/17387/main.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
3+
read = lambda: sys.stdin.readline().rstrip()
4+
5+
6+
class Problem:
7+
def __init__(self):
8+
self.x1, self.y1, self.x2, self.y2 = map(int, read().split())
9+
self.x3, self.y3, self.x4, self.y4 = map(int, read().split())
10+
11+
def solve(self) -> None:
12+
a, b, c, d = (self.x1, self.y1), (self.x2, self.y2), (self.x3, self.y3), (self.x4, self.y4)
13+
ccw_ab, ccw_cd = self.ccw(a, b, c) * self.ccw(a, b, d), self.ccw(c, d, a) * self.ccw(c, d, b)
14+
15+
result = ccw_ab <= 0 and ccw_cd <= 0
16+
if ccw_ab == 0 and ccw_cd == 0:
17+
result = self.is_overlap(a, b, c, d)
18+
19+
print(int(result))
20+
21+
def ccw(self, point1: tuple[int, int], point2: tuple[int, int], point3: tuple[int, int]) -> int:
22+
result = (point2[0] - point1[0]) * (point3[1] - point1[1]) - (point2[1] - point1[1]) * (point3[0] - point1[0])
23+
return 0 if result == 0 else (result // abs(result))
24+
25+
def is_overlap(self, a: tuple[int, int], b: tuple[int, int], c: tuple[int, int], d: tuple[int, int]) -> bool:
26+
return (
27+
min(a[0], b[0]) <= max(c[0], d[0])
28+
and min(c[0], d[0]) <= max(a[0], b[0])
29+
and min(a[1], b[1]) <= max(c[1], d[1])
30+
and min(c[1], d[1]) <= max(a[1], b[1])
31+
)
32+
33+
34+
if __name__ == "__main__":
35+
Problem().solve()

baekjoon/python/17387/sample.json

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[
2+
{
3+
"input": [
4+
"1 1 5 5",
5+
"1 5 5 1"
6+
],
7+
"expected": [
8+
"1"
9+
]
10+
},
11+
{
12+
"input": [
13+
"1 1 5 5",
14+
"6 10 10 6"
15+
],
16+
"expected": [
17+
"0"
18+
]
19+
},
20+
{
21+
"input": [
22+
"1 1 5 5",
23+
"5 5 1 1"
24+
],
25+
"expected": [
26+
"1"
27+
]
28+
},
29+
{
30+
"input": [
31+
"1 1 5 5",
32+
"3 3 5 5"
33+
],
34+
"expected": [
35+
"1"
36+
]
37+
},
38+
{
39+
"input": [
40+
"1 1 5 5",
41+
"3 3 1 3"
42+
],
43+
"expected": [
44+
"1"
45+
]
46+
},
47+
{
48+
"input": [
49+
"1 1 5 5",
50+
"5 5 9 9"
51+
],
52+
"expected": [
53+
"1"
54+
]
55+
},
56+
{
57+
"input": [
58+
"1 1 5 5",
59+
"6 6 9 9"
60+
],
61+
"expected": [
62+
"0"
63+
]
64+
},
65+
{
66+
"input": [
67+
"1 1 5 5",
68+
"5 5 1 5"
69+
],
70+
"expected": [
71+
"1"
72+
]
73+
},
74+
{
75+
"input": [
76+
"1 1 5 5",
77+
"6 6 1 5"
78+
],
79+
"expected": [
80+
"0"
81+
]
82+
}
83+
]

baekjoon/python/17387/test_main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
import os.path
3+
import unittest
4+
from io import StringIO
5+
from unittest.mock import patch
6+
7+
from parameterized import parameterized
8+
9+
from main import Problem
10+
11+
12+
def load_sample(filename: str):
13+
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
14+
15+
with open(path, "r") as file:
16+
return [(case["input"], case["expected"]) for case in json.load(file)]
17+
18+
19+
class TestCase(unittest.TestCase):
20+
@parameterized.expand(load_sample("sample.json"))
21+
def test_case(self, case: str, expected: list[str]):
22+
# When
23+
with (
24+
patch("sys.stdin.readline", side_effect=case),
25+
patch("sys.stdout", new_callable=StringIO) as output,
26+
):
27+
Problem().solve()
28+
29+
result = output.getvalue().rstrip()
30+
31+
# Then
32+
self.assertEqual("\n".join(expected), result)
33+
34+
35+
if __name__ == "__main__":
36+
unittest.main()

0 commit comments

Comments
 (0)