Skip to content

Commit e008b9f

Browse files
authored
feat: add pandas solution to lc problem: No.0180 (doocs#1863)
1 parent 763b141 commit e008b9f

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

solution/0100-0199/0180.Consecutive Numbers/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,21 @@ GROUP BY p
116116
HAVING COUNT(1) >= 3;
117117
```
118118

119+
```python
120+
import pandas as pd
121+
122+
123+
def consecutive_numbers(logs: pd.DataFrame) -> pd.DataFrame:
124+
all_the_same = lambda lst: lst.nunique() == 1
125+
logs["is_consecutive"] = (
126+
logs["num"].rolling(window=3, center=True, min_periods=3).apply(all_the_same)
127+
)
128+
return (
129+
logs.query("is_consecutive == 1.0")[["num"]]
130+
.drop_duplicates()
131+
.rename(columns={"num": "ConsecutiveNums"})
132+
)
133+
134+
```
135+
119136
<!-- tabs:end -->

solution/0100-0199/0180.Consecutive Numbers/README_EN.md

+17
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,21 @@ GROUP BY p
112112
HAVING COUNT(1) >= 3;
113113
```
114114

115+
```python
116+
import pandas as pd
117+
118+
119+
def consecutive_numbers(logs: pd.DataFrame) -> pd.DataFrame:
120+
all_the_same = lambda lst: lst.nunique() == 1
121+
logs["is_consecutive"] = (
122+
logs["num"].rolling(window=3, center=True, min_periods=3).apply(all_the_same)
123+
)
124+
return (
125+
logs.query("is_consecutive == 1.0")[["num"]]
126+
.drop_duplicates()
127+
.rename(columns={"num": "ConsecutiveNums"})
128+
)
129+
130+
```
131+
115132
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pandas as pd
2+
3+
4+
def consecutive_numbers(logs: pd.DataFrame) -> pd.DataFrame:
5+
all_the_same = lambda lst: lst.nunique() == 1
6+
logs["is_consecutive"] = (
7+
logs["num"].rolling(window=3, center=True, min_periods=3).apply(all_the_same)
8+
)
9+
return (
10+
logs.query("is_consecutive == 1.0")[["num"]]
11+
.drop_duplicates()
12+
.rename(columns={"num": "ConsecutiveNums"})
13+
)

0 commit comments

Comments
 (0)