728x90
문제
Table: Triangle
+-------------+------+
| Column Name | Type |
+-------------+------+
| x | int |
| y | int |
| z | int |
+-------------+------+
In SQL, (x, y, z) is the primary key column for this table.
Each row of this table contains the lengths of three line segments.
Report for every three line segments whether they can form a triangle.
Return the result table in any order.
https://leetcode.com/problems/triangle-judgement/description/?lang=pythondata
예시
Input:
Triangle table:
+----+----+----+
| x | y | z |
+----+----+----+
| 13 | 15 | 30 |
| 10 | 20 | 15 |
+----+----+----+
Output:
+----+----+----+----------+
| x | y | z | triangle |
+----+----+----+----------+
| 13 | 15 | 30 | No |
| 10 | 20 | 15 | Yes |
+----+----+----+----------+
문제 풀이
import pandas as pd
def triangle_judgement(triangle: pd.DataFrame) -> pd.DataFrame:
triangle['triangle'] = triangle.apply(lambda row: 'Yes'
if row['x'] + row['y'] > row['z']
and row['y'] + row['z'] > row['x']
and row['x'] + row['z'] > row['y'] else 'No', axis=1)
return triangle
파이썬을 독학하시는 분들에게 도움이 되길 바라며,
혹 더 좋은 방법이 있거나 오류가 있다면 편하게 말씀 부탁드립니다.
728x90