Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9bffb82

Browse files
more
1 parent 2f19201 commit 9bffb82

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Problem 3421: Find Students Who Improved
2+
# Difficulty: Medium
3+
4+
# Table: Scores
5+
# +-------------+---------+
6+
# | Column Name | Type |
7+
# +-------------+---------+
8+
# | student_id | int |
9+
# | subject | varchar |
10+
# | score | int |
11+
# | exam_date | varchar |
12+
# +-------------+---------+
13+
# (student_id, subject, exam_date) is the primary key.
14+
15+
# Problem Statement:
16+
# Write a function to find students who have shown improvement.
17+
# A student is considered to have improved if:
18+
# - They have taken exams in the same subject on at least two different dates.
19+
# - Their latest score in that subject is higher than their first score.
20+
# Return the result table ordered by student_id, subject in ascending order.
21+
22+
# Solution 1
23+
24+
import pandas as pd
25+
26+
def find_students_who_improved(scores: pd.DataFrame) -> pd.DataFrame:
27+
# I sort the table by student_id, subject, exam_date so that first and last exams align naturally
28+
temp_df = scores.sort_values(by=['student_id', 'subject', 'exam_date'])
29+
30+
# I compute first and latest scores by grouping and using 'first' and 'last'
31+
temp_df1 = temp_df.groupby(['student_id', 'subject'], as_index=False).agg(
32+
first_score=('score', 'first'),
33+
latest_score=('score', 'last')
34+
)
35+
36+
# I filter out records where the latest score is higher than the first
37+
result_df = temp_df1[temp_df1['first_score'] < temp_df1['latest_score']]
38+
39+
# I return the final result
40+
return result_df
41+
42+
# Intuition:
43+
# I need to compare the first and latest exam scores for each student in each subject and check if the latest score improved.
44+
45+
# Explanation:
46+
# I first sort the scores by student, subject, and exam date.
47+
# Then, I group by student and subject to get the first and last scores using 'first' and 'last'.
48+
# After that, I filter where the latest score is higher than the first and return this result.
49+
50+
# Solution 2
51+
52+
import pandas as pd
53+
54+
def find_students_who_improved(scores: pd.DataFrame) -> pd.DataFrame:
55+
# I group by student_id and subject, then compute first and latest scores by date comparison
56+
temp_df = scores.groupby(['student_id', 'subject'], as_index=False).apply(
57+
lambda x: pd.Series({
58+
'first_score': x[x['exam_date'] == x['exam_date'].min()]['score'].iloc[0],
59+
'latest_score': x[x['exam_date'] == x['exam_date'].max()]['score'].iloc[0]
60+
})
61+
)
62+
63+
# I filter where the latest score is higher than the first and sort the result
64+
result_df = temp_df.loc[
65+
(temp_df['first_score'] < temp_df['latest_score'])
66+
].sort_values(by=['student_id', 'subject'], ascending=[True, True])
67+
68+
# I return the final result
69+
return result_df
70+
71+
# Intuition:
72+
# Similar to Solution 1 — but I explicitly select scores on the earliest and latest exam dates for each student and subject.
73+
74+
# Explanation:
75+
# I group by student_id and subject, then for each group, find the score on the earliest and latest exam dates.
76+
# I construct a new DataFrame with these values.
77+
# Finally, I filter where the latest score is higher than the first, sort by student_id and subject, and return the result.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /