Skip to content

Commit

Permalink
1050.Actors and Directors Who Cooperated
Browse files Browse the repository at this point in the history
  • Loading branch information
SepidehHosseinian committed Aug 30, 2023
1 parent 7f074bf commit 131c86f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
### 1050. Actors and Directors Who Cooperated At Least Three Times
|Easy

Table: ActorDirector
```
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| actor_id | int |
| director_id | int |
| timestamp | int |
+-------------+---------+
```
timestamp is the primary key (column with unique values) for this table.


Write a solution to find all the pairs (actor_id, director_id) where the actor has cooperated with the director at least three times.

Return the result table in any order.

The result format is in the following example.



Example 1:
```
Input:
ActorDirector table:
+-------------+-------------+-------------+
| actor_id | director_id | timestamp |
+-------------+-------------+-------------+
| 1 | 1 | 0 |
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 2 | 3 |
| 1 | 2 | 4 |
| 2 | 1 | 5 |
| 2 | 1 | 6 |
+-------------+-------------+-------------+
Output:
+-------------+-------------+
| actor_id | director_id |
+-------------+-------------+
| 1 | 1 |
+-------------+-------------+
Explanation: The only pair is (1, 1) where they cooperated exactly 3 times.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pandas as pd

def actors_and_directors(actor_director: pd.DataFrame) -> pd.DataFrame:
stats = actor_director.groupby(
['actor_id', 'director_id'],
).agg(
count=('director_id', 'count'),
).reset_index()
return stats[stats['count'] >= 3][['actor_id', 'director_id']]

0 comments on commit 131c86f

Please sign in to comment.