Skip to content

Commit d126f55

Browse files
authored
Merge pull request #81 from iamAntimPal/Branch-1
Branch 1
2 parents 4eb22e6 + 4bd6425 commit d126f55

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pandas as pd
2+
3+
def triangle_judgement(triangle: pd.DataFrame) -> pd.DataFrame:
4+
# Create a new column 'triangle' based on the triangle inequality conditions
5+
triangle['triangle'] = triangle.apply(
6+
lambda row: 'Yes' if (row['x'] + row['y'] > row['z'] and
7+
row['x'] + row['z'] > row['y'] and
8+
row['y'] + row['z'] > row['x']) else 'No',
9+
axis=1
10+
)
11+
return triangle
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
2+
# **610. Triangle Judgement**
3+
4+
## **Problem Statement**
5+
You are given a table `Triangle` that contains three integer values representing the lengths of three line segments.
6+
7+
### **Triangle Table**
8+
```
9+
+-------------+------+
10+
| Column Name | Type |
11+
+-------------+------+
12+
| x | int |
13+
| y | int |
14+
| z | int |
15+
+-------------+------+
16+
```
17+
- **(x, y, z)** is the **primary key**.
18+
- Each row represents the lengths of three line segments.
19+
20+
### **Task:**
21+
Report for each row whether the three line segments can form a triangle. A triangle can be formed if and only if the sum of any two sides is greater than the third side.
22+
23+
---
24+
25+
## **Example 1:**
26+
27+
### **Input:**
28+
#### **Triangle Table**
29+
```
30+
+----+----+----+
31+
| x | y | z |
32+
+----+----+----+
33+
| 13 | 15 | 30 |
34+
| 10 | 20 | 15 |
35+
+----+----+----+
36+
```
37+
38+
### **Output:**
39+
```
40+
+----+----+----+----------+
41+
| x | y | z | triangle |
42+
+----+----+----+----------+
43+
| 13 | 15 | 30 | No |
44+
| 10 | 20 | 15 | Yes |
45+
+----+----+----+----------+
46+
```
47+
48+
### **Explanation:**
49+
- For the first row: `13 + 15` is not greater than `30`, so the segments cannot form a triangle.
50+
- For the second row: All conditions are met (`10+20 > 15`, `10+15 > 20`, `20+15 > 10`), so they form a triangle.
51+
52+
---
53+
54+
## **Solution Approaches**
55+
56+
### **SQL Solution**
57+
```sql
58+
SELECT
59+
x,
60+
y,
61+
z,
62+
IF(x + y > z AND x + z > y AND y + z > x, 'Yes', 'No') AS triangle
63+
FROM Triangle;
64+
```
65+
**Explanation:**
66+
- The query checks if the sum of any two sides is greater than the third side.
67+
- If all conditions are true, it returns `'Yes'`; otherwise, it returns `'No'`.
68+
69+
---
70+
71+
### **Pandas Solution**
72+
```python
73+
import pandas as pd
74+
75+
def triangle_judgement(triangle: pd.DataFrame) -> pd.DataFrame:
76+
# Create a new column 'triangle' based on the triangle inequality conditions
77+
triangle['triangle'] = triangle.apply(
78+
lambda row: 'Yes' if (row['x'] + row['y'] > row['z'] and
79+
row['x'] + row['z'] > row['y'] and
80+
row['y'] + row['z'] > row['x']) else 'No',
81+
axis=1
82+
)
83+
return triangle
84+
85+
# Example usage:
86+
# df = pd.DataFrame({'x': [13, 10], 'y': [15, 20], 'z': [30, 15]})
87+
# print(triangle_judgement(df))
88+
```
89+
**Explanation:**
90+
- The Pandas solution uses `apply()` with a lambda function to evaluate the triangle inequality for each row.
91+
- It then creates a new column `triangle` with the result `'Yes'` or `'No'`.
92+
93+
---
94+
95+
## **File Structure**
96+
```
97+
LeetCode610/
98+
├── problem_statement.md # Contains the problem description and constraints.
99+
├── sql_solution.sql # Contains the SQL solution.
100+
├── pandas_solution.py # Contains the Pandas solution.
101+
├── README.md # Overview of the problem and available solutions.
102+
```
103+
104+
---
105+
106+
## **Useful Links**
107+
- [LeetCode Problem 610](https://leetcode.com/problems/triangle-judgement/)
108+
- [SQL IF Function](https://www.w3schools.com/sql/func_mysql_if.asp)
109+
- [Pandas apply() Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html)

0 commit comments

Comments
 (0)