Skip to content

Commit ebafef4

Browse files
committed
feat(pandas): add problem 610
1 parent 965a913 commit ebafef4

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Fiddling around with DataFusion, pandas, and PyArrow.
4848
|-------------:|:---------------------------------------------------------------------------------------------------------------------------------------------|:-------------|:-------------|:---------|:----------|
4949
| 1731 | [The Number of Employees Which Report to Each Employee](https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee) | Easy ||||
5050
| 1789 | [Primary Department for Each Employee](https://leetcode.com/problems/primary-department-for-each-employee) | Easy ||||
51-
| 610 | [Triangle Judgement](https://leetcode.com/problems/triangle-judgement) | Easy || ||
51+
| 610 | [Triangle Judgement](https://leetcode.com/problems/triangle-judgement) | Easy || ||
5252
| 180 | [Consecutive Numbers](https://leetcode.com/problems/consecutive-numbers) | Medium ||||
5353
| 1164 | [Product Price at a Given Date](https://leetcode.com/problems/product-price-at-a-given-date) | Medium ||||
5454
| 1204 | [Last Person to Fit in the Bus](https://leetcode.com/problems/last-person-to-fit-in-the-bus) | Medium ||||

problems/pandas.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,30 @@ def problem_596(courses: pd.DataFrame) -> pd.DataFrame:
258258
)
259259

260260

261+
def problem_610(triangle: pd.DataFrame) -> pd.DataFrame:
262+
"""Report for every three line segments whether they can form a triangle.
263+
264+
Return the result table in any order.
265+
266+
Parameters
267+
----------
268+
triangle : pd.DataFrame
269+
Table contains the lengths of three line segments (x, y, z).
270+
271+
Returns
272+
-------
273+
pd.DataFrame
274+
275+
"""
276+
triangle["triangle"] = (
277+
(triangle["x"] + triangle["y"] > triangle["z"])
278+
& (triangle["x"] + triangle["z"] > triangle["y"])
279+
& (triangle["y"] + triangle["z"] > triangle["x"])
280+
)
281+
triangle["triangle"] = triangle["triangle"].map({True: "Yes", False: "No"})
282+
return triangle
283+
284+
261285
def problem_1068(sales: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame:
262286
"""Report the product_name, year, and price for each sale_id in the Sales table.
263287

tests/test_pandas.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,50 @@ def test_problem_596(input_data, expected_data):
499499
)
500500

501501

502+
@pytest.mark.parametrize(
503+
"input_data, expected_data",
504+
[
505+
pytest.param(
506+
{"x": [3], "y": [4], "z": [5]},
507+
{"x": [3], "y": [4], "z": [5], "triangle": ["Yes"]},
508+
id="valid_triangle",
509+
),
510+
pytest.param(
511+
{"x": [5], "y": [5], "z": [5]},
512+
{"x": [5], "y": [5], "z": [5], "triangle": ["Yes"]},
513+
id="equilateral_triangle",
514+
),
515+
pytest.param(
516+
{"x": [2], "y": [2], "z": [3]},
517+
{"x": [2], "y": [2], "z": [3], "triangle": ["Yes"]},
518+
id="isosceles_triangle",
519+
),
520+
pytest.param(
521+
{"x": [1], "y": [1], "z": [2]},
522+
{"x": [1], "y": [1], "z": [2], "triangle": ["No"]},
523+
id="degenerate_triangle",
524+
),
525+
pytest.param(
526+
{"x": [0], "y": [0], "z": [0]},
527+
{"x": [0], "y": [0], "z": [0], "triangle": ["No"]},
528+
id="zero_length_sides",
529+
),
530+
pytest.param(
531+
{"x": [1], "y": [2], "z": [3]},
532+
{"x": [1], "y": [2], "z": [3], "triangle": ["No"]},
533+
id="non_triangle",
534+
),
535+
],
536+
)
537+
def test_problem_610(input_data, expected_data):
538+
table = pd.DataFrame(input_data)
539+
expected_table = pd.DataFrame(expected_data)
540+
result = problem_610(table).reset_index(drop=True)
541+
assert_frame_equal(
542+
result, expected_table, check_dtype=False, check_index_type=False
543+
)
544+
545+
502546
@pytest.mark.parametrize(
503547
"input_data, expected_data",
504548
[

0 commit comments

Comments
 (0)