Skip to content

Commit b6e4e1b

Browse files
committed
feat(pandas): add problem 585
1 parent a6073e1 commit b6e4e1b

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Fiddling around with DataFusion, pandas, and PyArrow.
88
|----------:|:------|:-----------|:-------:|:----------:|:------:|
99
| 1757 | [Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products) | Easy ||||
1010
| 584 | [Find Customer Referee](https://leetcode.com/problems/find-customer-referee) | Easy ||||
11-
| 595 | [Big Countries](https://leetcode.com/problems/big-countries) | Easy ||| |
11+
| 595 | [Big Countries](https://leetcode.com/problems/big-countries) | Easy ||| |
1212
| 1148 | [Article Views I](https://leetcode.com/problems/article-views-i) | Easy ||||
1313
| 1683 | [Invalid Tweets](https://leetcode.com/problems/invalid-tweets) | Easy ||||
1414

problems/pandas.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,31 @@ def problem_584(customer: pd.DataFrame) -> pd.DataFrame:
5050
return mask[["name"]]
5151

5252

53+
def problem_595(world: pd.DataFrame) -> pd.DataFrame:
54+
"""Find the name, population, and area of the big countries.
55+
56+
A country is big if:
57+
it has an area of at least three million (i.e., 3000000 km2), or
58+
it has a population of at least twenty-five million (i.e., 25000000).
59+
60+
Return the result table in any order.
61+
62+
Parameters
63+
----------
64+
world : pd.DataFrame
65+
Table lists countries with their continent, area, population, and GDP details.
66+
67+
Returns
68+
-------
69+
pd.DataFrame
70+
71+
"""
72+
columns = ["name", "population", "area"]
73+
big_mask = (world["area"] >= 3_000_000) | (world["population"] >= 25_000_000)
74+
world = world[big_mask]
75+
return world[columns]
76+
77+
5378
def problem_1321(customer: pd.DataFrame) -> pd.DataFrame:
5479
"""Compute the moving average of how much the customer paid in a seven days window.
5580

tests/test_pandas.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
import pandas as pd
44
import pytest
55

6-
from problems.pandas import problem_176, problem_584, problem_1321, problem_1757
6+
from problems.pandas import (
7+
problem_176,
8+
problem_584,
9+
problem_595,
10+
problem_1321,
11+
problem_1757,
12+
)
713

814

915
@pytest.mark.parametrize(
@@ -82,6 +88,60 @@ def test_problem_584(input_data, expected_data):
8288
), f"Expected table {expected_table}, but got {result}"
8389

8490

91+
@pytest.mark.parametrize(
92+
"input_data, expected_data",
93+
[
94+
pytest.param(
95+
{
96+
"name": ["Afghanistan", "Albania", "Algeria", "Andorra", "Angola"],
97+
"continent": ["Asia", "Europe", "Africa", "Europe", "Africa"],
98+
"area": [652_230, 28_748, 2_381_741, 468, 1_246_700],
99+
"population": [25_500_100, 2_831_741, 37_100_000, 78_115, 20_609_294],
100+
"gdp": [
101+
20_343_000_000,
102+
12_960_000_000,
103+
188_681_000_000,
104+
3_712_000_000,
105+
100_990_000_000,
106+
],
107+
},
108+
{
109+
"name": ["Afghanistan", "Algeria"],
110+
"population": [25_500_100, 37_100_000],
111+
"area": [652_230, 2_381_741],
112+
},
113+
id="happy_path_various_countries",
114+
),
115+
pytest.param(
116+
{
117+
"name": ["CountryA", "CountryB"],
118+
"continent": ["ContinentA", "ContinentB"],
119+
"area": [3_000_000, 4_000_000],
120+
"population": [30_000_000, 40_000_000],
121+
"gdp": [1_000_000_000, 2_000_000_000],
122+
},
123+
{
124+
"name": ["CountryA", "CountryB"],
125+
"population": [30_000_000, 40_000_000],
126+
"area": [3_000_000, 4_000_000],
127+
},
128+
id="edge_case_all_countries_meeting_criteria",
129+
),
130+
],
131+
)
132+
def test_problem_595(input_data, expected_data):
133+
table = pd.DataFrame(input_data)
134+
expected_table = pd.DataFrame(expected_data)
135+
result = problem_595(table).reset_index(drop=True)
136+
if result.shape == (0, len(expected_table.columns)):
137+
assert result.shape == expected_table.shape
138+
assert result.columns.equals(expected_table.columns)
139+
else:
140+
assert result.equals(
141+
expected_table
142+
), f"Expected table {expected_table}, but got {result}"
143+
144+
85145
@pytest.mark.parametrize(
86146
"input_data, expected_data",
87147
[

0 commit comments

Comments
 (0)