610. Triangle Judgement
Description
Pandas Schema
data = [[13, 15, 30], [10, 20, 15]]
triangle = pd.DataFrame(data, columns=['x', 'y', 'z']).astype({'x':'Int64', 'y':'Int64', 'z':'Int64'})
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.
The result format is in the following example.
Example 1:
- 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 | +----+----+----+----------+
Submitted Code
import pandas as pd
def triangle_judgement(triangle: pd.DataFrame) -> pd.DataFrame:
triangle['triangle'] = (
(triangle['x'] + triangle['y'] > triangle['z']) &
(triangle['x'] + triangle['z'] > triangle['y']) &
(triangle['y'] + triangle['z'] > triangle['x'])).map({True: 'Yes', False: 'No'}
)
return triangle
Runtime: 255 ms | Beats 94.68%
Memory: 67.22 MB | Beats 5.72%
삼각형이 되려면 모든 변이 나머지 두 변의 합보다 짧아야 한다. map()을 사용하면 Series의 각 요소에 대해 딕셔너리로 매핑하여 변환 작업이 가능하다.
Other Solutions
1st
def triangle_judgement(triangle: pd.DataFrame) -> pd.DataFrame:
def check_triangle(x,y,z):
if (x+y>z) & (y+z>x) & (z+x>y):
return "Yes"
else:
return "No"
triangle['triangle'] = triangle.apply(lambda row: check_triangle(row['x'],row['y'],row['z']),axis=1)
return triangle
map() 대신 apply()를 사용하는 방법도 있다.
2nd
SELECT x, y, z,
CASE
WHEN (x+y) > z and (x+z) > y and (y+z) > x then 'Yes' else 'No'
END AS 'triangle'
FROM Triangle
CASE WHEN 사용
SELECT *,
IF(x+y>z and y+z>x and z+x>y, "Yes", "No") AS triangle
FROM Triangle
IF() 함수 구문 사용