Skip to content

Commit 88d9ed4

Browse files
committed
feat(pandas): add problem 180
1 parent 30a0d99 commit 88d9ed4

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Fiddling around with DataFusion, pandas, and PyArrow.
5858
| 1731 | [The Number of Employees Which Report to Each Employee](https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee) | Easy ||||
5959
| 1789 | [Primary Department for Each Employee](https://leetcode.com/problems/primary-department-for-each-employee) | Easy ||||
6060
| 610 | [Triangle Judgement](https://leetcode.com/problems/triangle-judgement) | Easy ||||
61-
| 180 | [Consecutive Numbers](https://leetcode.com/problems/consecutive-numbers) | Medium ||| |
61+
| 180 | [Consecutive Numbers](https://leetcode.com/problems/consecutive-numbers) | Medium ||| |
6262
| 1164 | [Product Price at a Given Date](https://leetcode.com/problems/product-price-at-a-given-date) | Medium ||||
6363
| 1204 | [Last Person to Fit in the Bus](https://leetcode.com/problems/last-person-to-fit-in-the-bus) | Medium ||||
6464
| 1907 | [Count Salary Categories](https://leetcode.com/problems/count-salary-categories) | Medium ||||

problems/pandas.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,35 @@ def problem_176(employee: pd.DataFrame) -> pd.DataFrame:
3030
return pd.DataFrame([None], columns=["SecondHighestSalary"])
3131
return result
3232

33+
def problem_180(logs: pd.DataFrame) -> pd.DataFrame:
34+
"""Find all numbers that appear at least three times consecutively.
35+
36+
Return the result table in any order.
37+
38+
Parameters
39+
----------
40+
logs : pd.DataFrame
41+
A table containing sequential ids and numbers.
42+
43+
Returns
44+
-------
45+
pd.DataFrame
46+
47+
"""
48+
if logs.empty:
49+
return pd.DataFrame({"ConsecutiveNums": [None]})
50+
51+
logs_lead_1 = logs.assign(id=logs["id"] + 1)
52+
logs_lead_2 = logs.assign(id=logs["id"] + 2)
53+
54+
return (
55+
logs.merge(logs_lead_1, on=["id", "num"])
56+
.merge(logs_lead_2, on=["id", "num"])[["num"]]
57+
.drop_duplicates()
58+
.rename(columns={"num": "ConsecutiveNums"})
59+
)
60+
61+
3362

3463
def problem_584(customer: pd.DataFrame) -> pd.DataFrame:
3564
"""Find names of customers not referred by the customer with ID = 2.

tests/test_pandas.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from problems.pandas import (
77
problem_176,
8+
problem_180,
89
problem_584,
910
problem_595,
1011
problem_1148,
@@ -45,6 +46,64 @@ def test_problem_176(input_data, expected_data):
4546
assert result.equals(expected_table)
4647

4748

49+
@pytest.mark.parametrize(
50+
"input_data, expected_data",
51+
[
52+
pytest.param(
53+
{
54+
"id": [1, 2, 3, 4, 5, 6, 7, 8],
55+
"num": [1, 2, 3, 1, 1, 1, 4, 5],
56+
},
57+
{
58+
"ConsecutiveNums": [1],
59+
},
60+
id="one_consecutive_number_three_times",
61+
),
62+
pytest.param(
63+
{
64+
"id": [1, 2, 3, 4, 5, 6, 7, 8],
65+
"num": [1, 2, 3, 1, 1, 1, 1, 5],
66+
},
67+
{
68+
"ConsecutiveNums": [1],
69+
},
70+
id="one_consecutive_number_four_times",
71+
),
72+
pytest.param(
73+
{
74+
"id": [1, 2, 3, 4, 5],
75+
"num": [1, 2, 3, 4, 5],
76+
},
77+
{
78+
"ConsecutiveNums": [],
79+
},
80+
id="no_consecutive_numbers",
81+
),
82+
pytest.param(
83+
{
84+
"id": [],
85+
"num": [],
86+
},
87+
{
88+
"ConsecutiveNums": [None],
89+
},
90+
id="empty_table",
91+
),
92+
],
93+
)
94+
def test_problem_180(input_data, expected_data):
95+
table = pd.DataFrame(input_data)
96+
expected_table = pd.DataFrame(expected_data)
97+
result = problem_180(table).reset_index(drop=True)
98+
if result.shape == (0, len(expected_table.columns)):
99+
assert result.shape == expected_table.shape
100+
assert result.columns.equals(expected_table.columns)
101+
else:
102+
assert result.equals(
103+
expected_table
104+
), f"Expected table {expected_table}, but got {result}"
105+
106+
48107
@pytest.mark.parametrize(
49108
"input_data, expected_data",
50109
[

0 commit comments

Comments
 (0)