Skip to content

Commit 8171419

Browse files
committed
feat(pandas): add problem 1517
1 parent 0477146 commit 8171419

File tree

3 files changed

+121
-3
lines changed

3 files changed

+121
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ Fiddling around with DataFusion, pandas, and PyArrow.
8585
| 176 | [Second Highest Salary](https://leetcode.com/problems/second-highest-salary) | Medium ||||
8686
| 1484 | [Group Sold Products By The Date](https://leetcode.com/problems/group-sold-products-by-the-date) | Easy ||||
8787
| 1327 | [List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period) | Easy ||||
88-
| 1517 | [Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails) | Easy ||| |
88+
| 1517 | [Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails) | Easy ||| |

problems/pandas.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,32 @@ def problem_1378(employees: pd.DataFrame, employee_uni: pd.DataFrame) -> pd.Data
292292
return employees.merge(employee_uni, how="left", on="id")[["unique_id", "name"]]
293293

294294

295+
def problem_1517(users: pd.DataFrame) -> pd.DataFrame:
296+
"""Find the users who have valid emails.
297+
298+
A valid e-mail has a prefix name and a domain where:
299+
300+
The prefix name is a string that may contain letters (upper or lower case), digits,
301+
underscore '_', period '.', and/or dash '-'. The prefix name must start with a
302+
letter.
303+
304+
Return the result table in any order.
305+
306+
Parameters
307+
----------
308+
users : pd.DataFrame
309+
Table containing user names and emails.
310+
311+
Returns
312+
-------
313+
pd.DataFrame
314+
315+
"""
316+
return users.loc[
317+
users["mail"].str.match(r"^[a-zA-Z][a-zA-Z0-9_.-]*@leetcode\.com$")
318+
]
319+
320+
295321
def problem_1581(visits: pd.DataFrame, transactions: pd.DataFrame) -> pd.DataFrame:
296322
"""Find users who visited without transactions and count their visit frequency.
297323

tests/test_pandas.py

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
problem_1280,
1717
problem_1321,
1818
problem_1378,
19+
problem_1517,
1920
problem_1581,
2021
problem_1661,
2122
problem_1683,
@@ -606,7 +607,7 @@ def test_problem_1280(input_data_1, input_data_2, input_data_3, expected_data):
606607
)
607608
def test_problem_1321(input_data, expected_data):
608609
table = pd.DataFrame(input_data)
609-
expected_table = pd.DataFrame(expected_data).reset_index(drop=True)
610+
expected_table = pd.DataFrame(expected_data)
610611
result = (
611612
problem_1321(table)
612613
.reset_index(drop=True)
@@ -641,7 +642,7 @@ def test_problem_1321(input_data, expected_data):
641642
def test_problem_1378(input_data_1, input_data_2, expected_data):
642643
table_1 = pd.DataFrame(input_data_1)
643644
table_2 = pd.DataFrame(input_data_2)
644-
expected_table = pd.DataFrame(expected_data).reset_index(drop=True)
645+
expected_table = pd.DataFrame(expected_data)
645646
result = (
646647
problem_1378(table_1, table_2)
647648
.reset_index(drop=True)
@@ -656,6 +657,97 @@ def test_problem_1378(input_data_1, input_data_2, expected_data):
656657
assert result.equals(expected_table)
657658

658659

660+
@pytest.mark.parametrize(
661+
"input_data, expected_data",
662+
[
663+
pytest.param(
664+
{
665+
"user_id": [1, 2, 3, 4, 5, 6, 7],
666+
"name": [
667+
"Winston",
668+
"Jonathan",
669+
"Annabelle",
670+
"Sally",
671+
"Marwan",
672+
"David",
673+
"Shapiro",
674+
],
675+
"mail": [
676+
"winston@leetcode.com",
677+
"jonathanisgreat",
678+
"bella-@leetcode.com",
679+
"sally.come@leetcode.com",
680+
"quarz#2020@leetcode.com",
681+
"david69@gmail.com",
682+
".shapo@leetcode.com",
683+
],
684+
},
685+
{
686+
"user_id": [1, 3, 4],
687+
"name": [
688+
"Winston",
689+
"Annabelle",
690+
"Sally",
691+
],
692+
"mail": [
693+
"winston@leetcode.com",
694+
"bella-@leetcode.com",
695+
"sally.come@leetcode.com",
696+
],
697+
},
698+
id="happy_path",
699+
),
700+
pytest.param(
701+
{
702+
"user_id": [360, 966, 901, 162, 181, 240, 221, 388, 211, 178],
703+
"name": [
704+
"Ezra",
705+
"Daniel",
706+
"Yehudah",
707+
"Daniel",
708+
"Aharon",
709+
"Gavriel",
710+
"Levi",
711+
"Eliyahu",
712+
"Michael",
713+
"Aharon",
714+
],
715+
"mail": [
716+
"Ezra4VZabfK",
717+
"DanielEnEMjNoG6",
718+
"Yehudah*5m9@leetcode.com",
719+
"Daniel07L@leetcode.com",
720+
"AharonxuZA530S8Q",
721+
"GavrielLVC@leetcode.com",
722+
"Leviz6OzK@leetcode.com",
723+
"Eliyahu--wzsgX@leetcode.com",
724+
".Michael@leetcode.com",
725+
"AharonnDFFSqcY",
726+
],
727+
},
728+
{
729+
"user_id": [162, 240, 221, 388],
730+
"name": ["Daniel", "Gavriel", "Levi", "Eliyahu"],
731+
"mail": [
732+
"Daniel07L@leetcode.com",
733+
"GavrielLVC@leetcode.com",
734+
"Leviz6OzK@leetcode.com",
735+
"Eliyahu--wzsgX@leetcode.com",
736+
],
737+
},
738+
id="happy_path_2",
739+
),
740+
],
741+
)
742+
def test_problem_1517(input_data, expected_data):
743+
table = pd.DataFrame(input_data)
744+
expected_table = pd.DataFrame(expected_data)
745+
result = problem_1517(table).reset_index(drop=True)
746+
assert_frame_equal(
747+
result, expected_table, check_dtype=False, check_index_type=False
748+
)
749+
750+
659751
@pytest.mark.parametrize(
660752
"input_data_1, input_data_2, expected_data",
661753
[

0 commit comments

Comments
 (0)