[코딩테스트]/[Pandas]
[LeetCode - Pandas] (Easy) 620. Not Boring Movies
잰잰'
2025. 4. 25. 19:03
Table: Cinema
+----------------+----------+
| Column Name | Type |
+----------------+----------+
| id | int |
| movie | varchar |
| description | varchar |
| rating | float |
+----------------+----------+
id는 이 테이블의 기본 키(고유 값이 있는 열)입니다. 각 행은 영화의 이름, 장르, 그리고 평점에 대한 정보를 포함합니다. 평점은 [0, 10] 범위의 소수점 2자리 실수입니다.
문제
ID가 홀수인 영화와 설명이 "boring"이 아닌 영화를 보고하는 솔루션을 작성하세요.
결과 테이블은 평점 순으로 내림차순으로 정렬하여 반환하세요.
결과 형식은 다음 예시와 같습니다.
Example 1:
Input:
Cinema table:
+----+------------+-------------+--------+
| id | movie | description | rating |
+----+------------+-------------+--------+
| 1 | War | great 3D | 8.9 |
| 2 | Science | fiction | 8.5 |
| 3 | irish | boring | 6.2 |
| 4 | Ice song | Fantacy | 8.6 |
| 5 | House card | Interesting | 9.1 |
+----+------------+-------------+--------+
Output:
+----+------------+-------------+--------+
| id | movie | description | rating |
+----+------------+-------------+--------+
| 5 | House card | Interesting | 9.1 |
| 1 | War | great 3D | 8.9 |
+----+------------+-------------+--------+
설명 :
홀수 ID를 가진 영화는 1, 3, 5번이 있습니다. ID가 3인 영화는 "boring"이므로 답변에 포함하지 않습니다.
✏️ 풀이
import pandas as pd
def not_boring_movies(cinema: pd.DataFrame) -> pd.DataFrame:
df = cinema[(cinema['description'] != 'boring') & (cinema['id'] % 2 == 1)]
result = df.sort_values(by='rating', ascending=False)
return result
1. 영화 설명이 boring이 아니다 '!='
2. id가 홀수이다 '% 2 == 1' 2로 나눠 나머지가 1인 것은 홀수
3. 평점 순 내림차순 정렬 'ascending=False'