-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/test/java/g2801_2900/s2877_create_a_dataframe_from_list/solution_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import unittest | ||
import pandas as pd | ||
from typing import List | ||
|
||
def createDataframe(student_data: List[List[int]]) -> pd.DataFrame: | ||
column_name = ['student_id','age'] | ||
result = pd.DataFrame(student_data, columns=column_name) | ||
return result | ||
|
||
class TestCreateDataframe(unittest.TestCase): | ||
|
||
def test_valid_data(self): | ||
student_data = [[1, 15], [2, 11], [3, 11], [4, 20]] | ||
expected_df = pd.DataFrame({ | ||
'student_id': [1, 2, 3, 4], | ||
'age': [15, 11, 11, 20] | ||
}) | ||
result_df = createDataframe(student_data) | ||
pd.testing.assert_frame_equal(result_df, expected_df) | ||
|
||
def test_empty_data(self): | ||
student_data = [] | ||
expected_df = pd.DataFrame(columns=['student_id', 'age']) | ||
result_df = createDataframe(student_data) | ||
pd.testing.assert_frame_equal(result_df, expected_df) | ||
|
||
def test_single_row(self): | ||
student_data = [[5, 18]] | ||
expected_df = pd.DataFrame({ | ||
'student_id': [5], | ||
'age': [18] | ||
}) | ||
result_df = createDataframe(student_data) | ||
pd.testing.assert_frame_equal(result_df, expected_df) | ||
|
||
def test_negative_age(self): | ||
student_data = [[6, -10]] | ||
expected_df = pd.DataFrame({ | ||
'student_id': [6], | ||
'age': [-10] | ||
}) | ||
result_df = createDataframe(student_data) | ||
pd.testing.assert_frame_equal(result_df, expected_df) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
64 changes: 64 additions & 0 deletions
64
src/test/java/g2801_2900/s2878_get_the_size_of_a_dataframe/solution_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import unittest | ||
import pandas as pd | ||
from typing import List | ||
|
||
def getDataframeSize(players: pd.DataFrame) -> List[int]: | ||
return [players.shape[0], players.shape[1]] | ||
|
||
class TestGetDataframeSize(unittest.TestCase): | ||
def test_example_case(self): | ||
# Example DataFrame | ||
data = { | ||
"player_id": [846, 749, 155, 583, 388, 883, 355, 247, 761, 642], | ||
"name": ["Mason", "Riley", "Bob", "Isabella", "Zachary", "Ava", "Violet", "Thomas", "Jack", "Charlie"], | ||
"age": [21, 30, 28, 32, 24, 23, 18, 27, 33, 36], | ||
"position": ["Forward", "Winger", "Striker", "Goalkeeper", "Midfielder", "Defender", "Striker", "Striker", "Midfielder", "Center-back"], | ||
"team": ["RealMadrid", "Barcelona", "ManchesterUnited", "Liverpool", "BayernMunich", "Chelsea", "Juventus", "ParisSaint-Germain", "ManchesterCity", "Arsenal"] | ||
} | ||
players = pd.DataFrame(data) | ||
|
||
# Expected result: 10 rows, 5 columns | ||
expected_output = [10, 5] | ||
self.assertEqual(getDataframeSize(players), expected_output) | ||
|
||
def test_empty_dataframe(self): | ||
# Empty DataFrame | ||
players = pd.DataFrame(columns=["player_id", "name", "age", "position", "team"]) | ||
|
||
# Expected result: 0 rows, 5 columns | ||
expected_output = [0, 5] | ||
self.assertEqual(getDataframeSize(players), expected_output) | ||
|
||
def test_single_row(self): | ||
# DataFrame with a single row | ||
data = { | ||
"player_id": [1], | ||
"name": ["John"], | ||
"age": [25], | ||
"position": ["Forward"], | ||
"team": ["TestTeam"] | ||
} | ||
players = pd.DataFrame(data) | ||
|
||
# Expected result: 1 row, 5 columns | ||
expected_output = [1, 5] | ||
self.assertEqual(getDataframeSize(players), expected_output) | ||
|
||
def test_different_columns(self): | ||
# DataFrame with more columns | ||
data = { | ||
"player_id": [1, 2], | ||
"name": ["John", "Doe"], | ||
"age": [25, 30], | ||
"position": ["Forward", "Midfielder"], | ||
"team": ["TestTeam", "AnotherTeam"], | ||
"goals": [15, 20] | ||
} | ||
players = pd.DataFrame(data) | ||
|
||
# Expected result: 2 rows, 6 columns | ||
expected_output = [2, 6] | ||
self.assertEqual(getDataframeSize(players), expected_output) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
69 changes: 69 additions & 0 deletions
69
src/test/java/g2801_2900/s2879_display_the_first_three_rows/solution_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import unittest | ||
import pandas as pd | ||
|
||
def selectFirstRows(zs: pd.DataFrame) -> pd.DataFrame: | ||
return zs.head(3) | ||
|
||
class TestSelectFirstRows(unittest.TestCase): | ||
def test_example_case(self): | ||
# Example DataFrame | ||
data = { | ||
"employee_id": [3, 90, 9, 60, 49, 43], | ||
"name": ["Bob", "Alice", "Tatiana", "Annabelle", "Jonathan", "Khaled"], | ||
"department": ["Operations", "Sales", "Engineering", "InformationTechnology", "HumanResources", "Administration"], | ||
"salary": [48675, 11096, 33805, 37678, 23793, 40454] | ||
} | ||
employees = pd.DataFrame(data) | ||
|
||
# Expected DataFrame with the first 3 rows | ||
expected_data = { | ||
"employee_id": [3, 90, 9], | ||
"name": ["Bob", "Alice", "Tatiana"], | ||
"department": ["Operations", "Sales", "Engineering"], | ||
"salary": [48675, 11096, 33805] | ||
} | ||
expected_output = pd.DataFrame(expected_data) | ||
|
||
pd.testing.assert_frame_equal(selectFirstRows(employees), expected_output) | ||
|
||
def test_less_than_three_rows(self): | ||
# DataFrame with less than 3 rows | ||
data = { | ||
"employee_id": [1, 2], | ||
"name": ["John", "Doe"], | ||
"department": ["HR", "IT"], | ||
"salary": [50000, 60000] | ||
} | ||
employees = pd.DataFrame(data) | ||
|
||
# Expected DataFrame (same as input since there are fewer than 3 rows) | ||
expected_output = employees.copy() | ||
|
||
pd.testing.assert_frame_equal(selectFirstRows(employees), expected_output) | ||
|
||
def test_empty_dataframe(self): | ||
# Empty DataFrame | ||
employees = pd.DataFrame(columns=["employee_id", "name", "department", "salary"]) | ||
|
||
# Expected result: Empty DataFrame with same columns | ||
expected_output = employees.copy() | ||
|
||
pd.testing.assert_frame_equal(selectFirstRows(employees), expected_output) | ||
|
||
def test_exactly_three_rows(self): | ||
# DataFrame with exactly 3 rows | ||
data = { | ||
"employee_id": [10, 20, 30], | ||
"name": ["Eve", "Mark", "Lily"], | ||
"department": ["Finance", "Operations", "Engineering"], | ||
"salary": [70000, 65000, 72000] | ||
} | ||
employees = pd.DataFrame(data) | ||
|
||
# Expected DataFrame (same as input since there are exactly 3 rows) | ||
expected_output = employees.copy() | ||
|
||
pd.testing.assert_frame_equal(selectFirstRows(employees), expected_output) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
68 changes: 68 additions & 0 deletions
68
src/test/java/g2801_2900/s2880_select_data/solution_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import unittest | ||
import pandas as pd | ||
|
||
def selectData(students: pd.DataFrame) -> pd.DataFrame: | ||
return students[students.student_id == 101][['name', 'age']] | ||
|
||
class TestSelectData(unittest.TestCase): | ||
def test_example_case(self): | ||
# Example DataFrame | ||
data = { | ||
"student_id": [101, 53, 128, 3], | ||
"name": ["Ulysses", "William", "Henry", "Henry"], | ||
"age": [13, 10, 6, 11] | ||
} | ||
students = pd.DataFrame(data) | ||
|
||
# Expected output DataFrame with explicit data types | ||
expected_data = { | ||
"name": pd.Series(["Ulysses"], dtype="object"), | ||
"age": pd.Series([13], dtype="int64") | ||
} | ||
expected_output = pd.DataFrame(expected_data) | ||
|
||
pd.testing.assert_frame_equal(selectData(students), expected_output, check_dtype=False) | ||
|
||
def test_no_matching_id(self): | ||
# DataFrame with no matching student_id = 101 | ||
data = { | ||
"student_id": [102, 53, 128, 3], | ||
"name": ["John", "William", "Henry", "Doe"], | ||
"age": [12, 10, 6, 11] | ||
} | ||
students = pd.DataFrame(data) | ||
|
||
# Expected output: Empty DataFrame with columns ['name', 'age'] | ||
expected_output = pd.DataFrame(columns=['name', 'age']) | ||
|
||
pd.testing.assert_frame_equal(selectData(students), expected_output, check_dtype=False) | ||
|
||
def test_multiple_students_with_101(self): | ||
# DataFrame with multiple students having student_id = 101 | ||
data = { | ||
"student_id": [101, 101, 128], | ||
"name": ["Alice", "Bob", "Charlie"], | ||
"age": [20, 21, 22] | ||
} | ||
students = pd.DataFrame(data) | ||
|
||
# Expected output: DataFrame with both rows where student_id = 101 | ||
expected_data = { | ||
"name": ["Alice", "Bob"], | ||
"age": [20, 21] | ||
} | ||
expected_output = pd.DataFrame(expected_data) | ||
|
||
pd.testing.assert_frame_equal(selectData(students), expected_output, check_dtype=False) | ||
|
||
def test_empty_dataframe(self): | ||
# Empty DataFrame with the same structure | ||
students = pd.DataFrame(columns=["student_id", "name", "age"]) | ||
|
||
# Expected output: Empty DataFrame with columns ['name', 'age'] | ||
expected_output = pd.DataFrame(columns=['name', 'age']) | ||
|
||
pd.testing.assert_frame_equal(selectData(students), expected_output, check_dtype=False) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
32 changes: 32 additions & 0 deletions
32
src/test/java/g2801_2900/s2881_create_a_new_column/solution_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import unittest | ||
import pandas as pd | ||
|
||
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame: | ||
employees["bonus"] = employees["salary"] * 2 | ||
return employees | ||
|
||
class TestCreateBonusColumn(unittest.TestCase): | ||
def test_create_bonus_column(self): | ||
# Example DataFrame as input | ||
data = { | ||
"name": ["Piper", "Grace", "Georgia", "Willow", "Finn", "Thomas"], | ||
"salary": [4548, 28150, 1103, 6593, 74576, 24433] | ||
} | ||
employees = pd.DataFrame(data) | ||
|
||
# Expected output DataFrame | ||
expected_data = { | ||
"name": ["Piper", "Grace", "Georgia", "Willow", "Finn", "Thomas"], | ||
"salary": [4548, 28150, 1103, 6593, 74576, 24433], | ||
"bonus": [9096, 56300, 2206, 13186, 149152, 48866] | ||
} | ||
expected_output = pd.DataFrame(expected_data) | ||
|
||
# Test the function | ||
result = createBonusColumn(employees) | ||
|
||
# Use pandas testing utilities to compare DataFrames | ||
pd.testing.assert_frame_equal(result, expected_output) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |