Skip to content

Commit 8106177

Browse files
authored
Merge pull request #79 from iamAntimPal/Branch-1
Branch 1
2 parents dd4d475 + c976da4 commit 8106177

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pandas as pd
2+
3+
def classes_with_five_or_more_students(courses: pd.DataFrame) -> pd.DataFrame:
4+
# Group by 'class' and count the number of students
5+
result = courses.groupby('class').filter(lambda x: len(x) >= 5)
6+
# Return only the distinct class names
7+
return result[['class']].drop_duplicates().reset_index(drop=True)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# **596. Classes More Than 5 Students**
2+
3+
## **Problem Statement**
4+
You are given a table `Courses` that contains the names of students and the class in which they are enrolled.
5+
6+
### **Courses Table**
7+
```
8+
+---------+---------+
9+
| Column | Type |
10+
+---------+---------+
11+
| student | varchar |
12+
| class | varchar |
13+
+---------+---------+
14+
```
15+
- The combination of `(student, class)` is the **primary key**.
16+
- Each row indicates the name of a student and the class they are enrolled in.
17+
18+
### **Task:**
19+
Write a solution to find all the classes that have **at least five students**.
20+
21+
Return the result table in **any order**.
22+
23+
---
24+
25+
## **Example 1:**
26+
27+
### **Input:**
28+
```
29+
Courses table:
30+
+---------+----------+
31+
| student | class |
32+
+---------+----------+
33+
| A | Math |
34+
| B | English |
35+
| C | Math |
36+
| D | Biology |
37+
| E | Math |
38+
| F | Computer |
39+
| G | Math |
40+
| H | Math |
41+
| I | Math |
42+
+---------+----------+
43+
```
44+
45+
### **Output:**
46+
```
47+
+---------+
48+
| class |
49+
+---------+
50+
| Math |
51+
+---------+
52+
```
53+
54+
### **Explanation:**
55+
- **Math** has 6 students, so it is included.
56+
- **English**, **Biology**, and **Computer** have fewer than 5 students, so they are excluded.
57+
58+
---
59+
60+
## **Solution Approaches**
61+
62+
### **SQL Solution**
63+
```sql
64+
SELECT class
65+
FROM Courses
66+
GROUP BY class
67+
HAVING COUNT(student) >= 5;
68+
```
69+
**Explanation:**
70+
- The query groups records by `class`.
71+
- The `HAVING` clause filters out groups with fewer than 5 students.
72+
73+
---
74+
75+
### **Pandas Solution**
76+
```python
77+
import pandas as pd
78+
79+
def classes_with_five_or_more_students(courses: pd.DataFrame) -> pd.DataFrame:
80+
# Group by 'class' and count the number of students
81+
result = courses.groupby('class').filter(lambda x: len(x) >= 5)
82+
# Return only the distinct class names
83+
return result[['class']].drop_duplicates().reset_index(drop=True)
84+
85+
# Example usage:
86+
# courses_df = pd.read_csv('courses.csv')
87+
# print(classes_with_five_or_more_students(courses_df))
88+
```
89+
**Explanation:**
90+
- The Pandas solution groups the DataFrame by `class` and filters groups with 5 or more students.
91+
- It then extracts and returns the distinct class names.
92+
93+
---
94+
95+
## **File Structure**
96+
```
97+
LeetCode596/
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 for Python users.
101+
├── README.md # Overview of the problem and available solutions.
102+
```
103+
104+
---
105+
106+
## **Useful Links**
107+
- [LeetCode Problem 596](https://leetcode.com/problems/classes-more-than-5-students/)
108+
- [SQL GROUP BY and HAVING Clause](https://www.w3schools.com/sql/sql_groupby.asp)
109+
- [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html)
110+

0 commit comments

Comments
 (0)