Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 08eb967

Browse filesBrowse files
authored
Improved tasks 2877, 2878, 2879, 2880, 2881, 2882, 2883
1 parent 39452de commit 08eb967
Copy full SHA for 08eb967

File tree

Expand file treeCollapse file tree

7 files changed

+445
-0
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+445
-0
lines changed
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unittest
2+
import pandas as pd
3+
from typing import List
4+
5+
def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:
6+
column_name = ['student_id','age']
7+
result = pd.DataFrame(student_data, columns=column_name)
8+
return result
9+
10+
class TestCreateDataframe(unittest.TestCase):
11+
12+
def test_valid_data(self):
13+
student_data = [[1, 15], [2, 11], [3, 11], [4, 20]]
14+
expected_df = pd.DataFrame({
15+
'student_id': [1, 2, 3, 4],
16+
'age': [15, 11, 11, 20]
17+
})
18+
result_df = createDataframe(student_data)
19+
pd.testing.assert_frame_equal(result_df, expected_df)
20+
21+
def test_empty_data(self):
22+
student_data = []
23+
expected_df = pd.DataFrame(columns=['student_id', 'age'])
24+
result_df = createDataframe(student_data)
25+
pd.testing.assert_frame_equal(result_df, expected_df)
26+
27+
def test_single_row(self):
28+
student_data = [[5, 18]]
29+
expected_df = pd.DataFrame({
30+
'student_id': [5],
31+
'age': [18]
32+
})
33+
result_df = createDataframe(student_data)
34+
pd.testing.assert_frame_equal(result_df, expected_df)
35+
36+
def test_negative_age(self):
37+
student_data = [[6, -10]]
38+
expected_df = pd.DataFrame({
39+
'student_id': [6],
40+
'age': [-10]
41+
})
42+
result_df = createDataframe(student_data)
43+
pd.testing.assert_frame_equal(result_df, expected_df)
44+
45+
if __name__ == '__main__':
46+
unittest.main()
+64Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import unittest
2+
import pandas as pd
3+
from typing import List
4+
5+
def getDataframeSize(players: pd.DataFrame) -> List[int]:
6+
return [players.shape[0], players.shape[1]]
7+
8+
class TestGetDataframeSize(unittest.TestCase):
9+
def test_example_case(self):
10+
# Example DataFrame
11+
data = {
12+
"player_id": [846, 749, 155, 583, 388, 883, 355, 247, 761, 642],
13+
"name": ["Mason", "Riley", "Bob", "Isabella", "Zachary", "Ava", "Violet", "Thomas", "Jack", "Charlie"],
14+
"age": [21, 30, 28, 32, 24, 23, 18, 27, 33, 36],
15+
"position": ["Forward", "Winger", "Striker", "Goalkeeper", "Midfielder", "Defender", "Striker", "Striker", "Midfielder", "Center-back"],
16+
"team": ["RealMadrid", "Barcelona", "ManchesterUnited", "Liverpool", "BayernMunich", "Chelsea", "Juventus", "ParisSaint-Germain", "ManchesterCity", "Arsenal"]
17+
}
18+
players = pd.DataFrame(data)
19+
20+
# Expected result: 10 rows, 5 columns
21+
expected_output = [10, 5]
22+
self.assertEqual(getDataframeSize(players), expected_output)
23+
24+
def test_empty_dataframe(self):
25+
# Empty DataFrame
26+
players = pd.DataFrame(columns=["player_id", "name", "age", "position", "team"])
27+
28+
# Expected result: 0 rows, 5 columns
29+
expected_output = [0, 5]
30+
self.assertEqual(getDataframeSize(players), expected_output)
31+
32+
def test_single_row(self):
33+
# DataFrame with a single row
34+
data = {
35+
"player_id": [1],
36+
"name": ["John"],
37+
"age": [25],
38+
"position": ["Forward"],
39+
"team": ["TestTeam"]
40+
}
41+
players = pd.DataFrame(data)
42+
43+
# Expected result: 1 row, 5 columns
44+
expected_output = [1, 5]
45+
self.assertEqual(getDataframeSize(players), expected_output)
46+
47+
def test_different_columns(self):
48+
# DataFrame with more columns
49+
data = {
50+
"player_id": [1, 2],
51+
"name": ["John", "Doe"],
52+
"age": [25, 30],
53+
"position": ["Forward", "Midfielder"],
54+
"team": ["TestTeam", "AnotherTeam"],
55+
"goals": [15, 20]
56+
}
57+
players = pd.DataFrame(data)
58+
59+
# Expected result: 2 rows, 6 columns
60+
expected_output = [2, 6]
61+
self.assertEqual(getDataframeSize(players), expected_output)
62+
63+
if __name__ == "__main__":
64+
unittest.main()
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import unittest
2+
import pandas as pd
3+
4+
def selectFirstRows(zs: pd.DataFrame) -> pd.DataFrame:
5+
return zs.head(3)
6+
7+
class TestSelectFirstRows(unittest.TestCase):
8+
def test_example_case(self):
9+
# Example DataFrame
10+
data = {
11+
"employee_id": [3, 90, 9, 60, 49, 43],
12+
"name": ["Bob", "Alice", "Tatiana", "Annabelle", "Jonathan", "Khaled"],
13+
"department": ["Operations", "Sales", "Engineering", "InformationTechnology", "HumanResources", "Administration"],
14+
"salary": [48675, 11096, 33805, 37678, 23793, 40454]
15+
}
16+
employees = pd.DataFrame(data)
17+
18+
# Expected DataFrame with the first 3 rows
19+
expected_data = {
20+
"employee_id": [3, 90, 9],
21+
"name": ["Bob", "Alice", "Tatiana"],
22+
"department": ["Operations", "Sales", "Engineering"],
23+
"salary": [48675, 11096, 33805]
24+
}
25+
expected_output = pd.DataFrame(expected_data)
26+
27+
pd.testing.assert_frame_equal(selectFirstRows(employees), expected_output)
28+
29+
def test_less_than_three_rows(self):
30+
# DataFrame with less than 3 rows
31+
data = {
32+
"employee_id": [1, 2],
33+
"name": ["John", "Doe"],
34+
"department": ["HR", "IT"],
35+
"salary": [50000, 60000]
36+
}
37+
employees = pd.DataFrame(data)
38+
39+
# Expected DataFrame (same as input since there are fewer than 3 rows)
40+
expected_output = employees.copy()
41+
42+
pd.testing.assert_frame_equal(selectFirstRows(employees), expected_output)
43+
44+
def test_empty_dataframe(self):
45+
# Empty DataFrame
46+
employees = pd.DataFrame(columns=["employee_id", "name", "department", "salary"])
47+
48+
# Expected result: Empty DataFrame with same columns
49+
expected_output = employees.copy()
50+
51+
pd.testing.assert_frame_equal(selectFirstRows(employees), expected_output)
52+
53+
def test_exactly_three_rows(self):
54+
# DataFrame with exactly 3 rows
55+
data = {
56+
"employee_id": [10, 20, 30],
57+
"name": ["Eve", "Mark", "Lily"],
58+
"department": ["Finance", "Operations", "Engineering"],
59+
"salary": [70000, 65000, 72000]
60+
}
61+
employees = pd.DataFrame(data)
62+
63+
# Expected DataFrame (same as input since there are exactly 3 rows)
64+
expected_output = employees.copy()
65+
66+
pd.testing.assert_frame_equal(selectFirstRows(employees), expected_output)
67+
68+
if __name__ == "__main__":
69+
unittest.main()
+68Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import unittest
2+
import pandas as pd
3+
4+
def selectData(students: pd.DataFrame) -> pd.DataFrame:
5+
return students[students.student_id == 101][['name', 'age']]
6+
7+
class TestSelectData(unittest.TestCase):
8+
def test_example_case(self):
9+
# Example DataFrame
10+
data = {
11+
"student_id": [101, 53, 128, 3],
12+
"name": ["Ulysses", "William", "Henry", "Henry"],
13+
"age": [13, 10, 6, 11]
14+
}
15+
students = pd.DataFrame(data)
16+
17+
# Expected output DataFrame with explicit data types
18+
expected_data = {
19+
"name": pd.Series(["Ulysses"], dtype="object"),
20+
"age": pd.Series([13], dtype="int64")
21+
}
22+
expected_output = pd.DataFrame(expected_data)
23+
24+
pd.testing.assert_frame_equal(selectData(students), expected_output, check_dtype=False)
25+
26+
def test_no_matching_id(self):
27+
# DataFrame with no matching student_id = 101
28+
data = {
29+
"student_id": [102, 53, 128, 3],
30+
"name": ["John", "William", "Henry", "Doe"],
31+
"age": [12, 10, 6, 11]
32+
}
33+
students = pd.DataFrame(data)
34+
35+
# Expected output: Empty DataFrame with columns ['name', 'age']
36+
expected_output = pd.DataFrame(columns=['name', 'age'])
37+
38+
pd.testing.assert_frame_equal(selectData(students), expected_output, check_dtype=False)
39+
40+
def test_multiple_students_with_101(self):
41+
# DataFrame with multiple students having student_id = 101
42+
data = {
43+
"student_id": [101, 101, 128],
44+
"name": ["Alice", "Bob", "Charlie"],
45+
"age": [20, 21, 22]
46+
}
47+
students = pd.DataFrame(data)
48+
49+
# Expected output: DataFrame with both rows where student_id = 101
50+
expected_data = {
51+
"name": ["Alice", "Bob"],
52+
"age": [20, 21]
53+
}
54+
expected_output = pd.DataFrame(expected_data)
55+
56+
pd.testing.assert_frame_equal(selectData(students), expected_output, check_dtype=False)
57+
58+
def test_empty_dataframe(self):
59+
# Empty DataFrame with the same structure
60+
students = pd.DataFrame(columns=["student_id", "name", "age"])
61+
62+
# Expected output: Empty DataFrame with columns ['name', 'age']
63+
expected_output = pd.DataFrame(columns=['name', 'age'])
64+
65+
pd.testing.assert_frame_equal(selectData(students), expected_output, check_dtype=False)
66+
67+
if __name__ == "__main__":
68+
unittest.main()
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import unittest
2+
import pandas as pd
3+
4+
def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame:
5+
employees["bonus"] = employees["salary"] * 2
6+
return employees
7+
8+
class TestCreateBonusColumn(unittest.TestCase):
9+
def test_create_bonus_column(self):
10+
# Example DataFrame as input
11+
data = {
12+
"name": ["Piper", "Grace", "Georgia", "Willow", "Finn", "Thomas"],
13+
"salary": [4548, 28150, 1103, 6593, 74576, 24433]
14+
}
15+
employees = pd.DataFrame(data)
16+
17+
# Expected output DataFrame
18+
expected_data = {
19+
"name": ["Piper", "Grace", "Georgia", "Willow", "Finn", "Thomas"],
20+
"salary": [4548, 28150, 1103, 6593, 74576, 24433],
21+
"bonus": [9096, 56300, 2206, 13186, 149152, 48866]
22+
}
23+
expected_output = pd.DataFrame(expected_data)
24+
25+
# Test the function
26+
result = createBonusColumn(employees)
27+
28+
# Use pandas testing utilities to compare DataFrames
29+
pd.testing.assert_frame_equal(result, expected_output)
30+
31+
if __name__ == '__main__':
32+
unittest.main()
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import unittest
2+
import pandas as pd
3+
from pandas.testing import assert_frame_equal
4+
5+
def dropDuplicateEmails(customers: pd.DataFrame) -> pd.DataFrame:
6+
customers.drop_duplicates(subset='email', keep='first', inplace=True)
7+
return customers
8+
9+
class TestDropDuplicateEmails(unittest.TestCase):
10+
11+
def test_no_duplicates(self):
12+
data = {
13+
'customer_id': [1, 2, 3],
14+
'name': ['Ella', 'David', 'Zachary'],
15+
'email': ['emily@example.com', 'michael@example.com', 'sarah@example.com']
16+
}
17+
customers = pd.DataFrame(data)
18+
expected = pd.DataFrame(data)
19+
20+
result = dropDuplicateEmails(customers).reset_index(drop=True)
21+
expected = expected.reset_index(drop=True)
22+
23+
assert_frame_equal(result, expected)
24+
25+
def test_with_duplicates(self):
26+
data = {
27+
'customer_id': [1, 2, 3, 4, 5, 6],
28+
'name': ['Ella', 'David', 'Zachary', 'Alice', 'Finn', 'Violet'],
29+
'email': [
30+
'emily@example.com', 'michael@example.com', 'sarah@example.com',
31+
'john@example.com', 'john@example.com', 'alice@example.com'
32+
]
33+
}
34+
customers = pd.DataFrame(data)
35+
36+
expected_data = {
37+
'customer_id': [1, 2, 3, 4, 6],
38+
'name': ['Ella', 'David', 'Zachary', 'Alice', 'Violet'],
39+
'email': ['emily@example.com', 'michael@example.com', 'sarah@example.com', 'john@example.com', 'alice@example.com']
40+
}
41+
expected = pd.DataFrame(expected_data)
42+
43+
result = dropDuplicateEmails(customers).reset_index(drop=True)
44+
expected = expected.reset_index(drop=True)
45+
46+
assert_frame_equal(result, expected)
47+
48+
def test_empty_dataframe(self):
49+
customers = pd.DataFrame(columns=['customer_id', 'name', 'email'])
50+
expected = customers.copy()
51+
52+
result = dropDuplicateEmails(customers).reset_index(drop=True)
53+
expected = expected.reset_index(drop=True)
54+
55+
assert_frame_equal(result, expected)
56+
57+
def test_single_row(self):
58+
data = {
59+
'customer_id': [1],
60+
'name': ['Ella'],
61+
'email': ['emily@example.com']
62+
}
63+
customers = pd.DataFrame(data)
64+
expected = pd.DataFrame(data)
65+
66+
result = dropDuplicateEmails(customers).reset_index(drop=True)
67+
expected = expected.reset_index(drop=True)
68+
69+
assert_frame_equal(result, expected)
70+
71+
if __name__ == '__main__':
72+
unittest.main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.