Skip to content

Improved tasks 2877-2881 #1812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
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()
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 src/test/java/g2801_2900/s2880_select_data/solution_test.py
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()
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()
Loading