Skip to content

Commit a4498d9

Browse files
committed
fix: update 1031 solution and test
1 parent 430e1c2 commit a4498d9

File tree

5 files changed

+1105
-1110
lines changed

5 files changed

+1105
-1110
lines changed

data/clean/f_1031_zhihan_refined.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ def f_1031(list_of_pairs):
2222
>>> print(product_array)
2323
360
2424
"""
25-
# Extract the second element from each tuple using a list comprehension
26-
values = [pair[1] for pair in list_of_pairs]
27-
28-
# Use reduce to calculate the product of all elements in the values list
29-
product = reduce(lambda x, y: x * y, values)
30-
31-
# Return the result as a numpy array with a single element
32-
return np.array([product])
25+
second_values = [pair[1] for pair in list_of_pairs]
26+
product = reduce(np.multiply, second_values)
27+
product_array = np.array([product])
28+
29+
return product_array
3330

3431
import unittest
3532
import numpy as np
@@ -46,36 +43,36 @@ class TestCases(unittest.TestCase):
4643
def test_case_1(self):
4744
# Basic test case with positive and negative numbers
4845
list_of_pairs = [('Fruits', 5), ('Vegetables', 9), ('Dairy', -1), ('Bakery', -2), ('Meat', 4)]
49-
expected_output = np.array(360)
46+
expected_output = np.array([360])
5047
actual_output = f_1031(list_of_pairs)
5148
print(actual_output, expected_output)
5249
self.assertTrue(np.array_equal(actual_output, expected_output))
5350

5451
def test_case_2(self):
5552
# Test case with all positive numbers
5653
list_of_pairs = [('A', 2), ('B', 3), ('C', 4)]
57-
expected_output = np.array(24)
54+
expected_output = np.array([24])
5855
actual_output = f_1031(list_of_pairs)
5956
self.assertTrue(np.array_equal(actual_output, expected_output))
6057

6158
def test_case_3(self):
6259
# Test case with all negative numbers
6360
list_of_pairs = [('A', -2), ('B', -3), ('C', -4)]
64-
expected_output = np.array(-24)
61+
expected_output = np.array([-24])
6562
actual_output = f_1031(list_of_pairs)
6663
self.assertTrue(np.array_equal(actual_output, expected_output))
6764

6865
def test_case_4(self):
6966
# Test case with a single tuple
7067
list_of_pairs = [('A', 10)]
71-
expected_output = np.array(10)
68+
expected_output = np.array([10])
7269
actual_output = f_1031(list_of_pairs)
7370
self.assertTrue(np.array_equal(actual_output, expected_output))
7471

7572
def test_case_5(self):
7673
# Test case with zeros
7774
list_of_pairs = [('A', 0), ('B', 5), ('C', 10)]
78-
expected_output = np.array(0)
75+
expected_output = np.array([0])
7976
actual_output = f_1031(list_of_pairs)
8077
self.assertTrue(np.array_equal(actual_output, expected_output))
8178
if __name__ == "__main__":

data/processed/33_w_doc.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ def task_func(list_of_pairs):
2222
>>> print(product_array)
2323
360
2424
"""
25-
values = [pair[1] for pair in list_of_pairs]
26-
product = reduce(lambda x, y: x * y, values)
27-
return np.array([product])
25+
second_values = [pair[1] for pair in list_of_pairs]
26+
product = reduce(np.multiply, second_values)
27+
product_array = np.array([product])
28+
return product_array
2829

2930
import unittest
3031
import numpy as np
@@ -34,35 +35,35 @@ class TestCases(unittest.TestCase):
3435
def test_case_1(self):
3536
# Basic test case with positive and negative numbers
3637
list_of_pairs = [('Fruits', 5), ('Vegetables', 9), ('Dairy', -1), ('Bakery', -2), ('Meat', 4)]
37-
expected_output = np.array(360)
38+
expected_output = np.array([360])
3839
actual_output = task_func(list_of_pairs)
3940
print(actual_output, expected_output)
4041
self.assertTrue(np.array_equal(actual_output, expected_output))
4142

4243
def test_case_2(self):
4344
# Test case with all positive numbers
4445
list_of_pairs = [('A', 2), ('B', 3), ('C', 4)]
45-
expected_output = np.array(24)
46+
expected_output = np.array([24])
4647
actual_output = task_func(list_of_pairs)
4748
self.assertTrue(np.array_equal(actual_output, expected_output))
4849

4950
def test_case_3(self):
5051
# Test case with all negative numbers
5152
list_of_pairs = [('A', -2), ('B', -3), ('C', -4)]
52-
expected_output = np.array(-24)
53+
expected_output = np.array([-24])
5354
actual_output = task_func(list_of_pairs)
5455
self.assertTrue(np.array_equal(actual_output, expected_output))
5556

5657
def test_case_4(self):
5758
# Test case with a single tuple
5859
list_of_pairs = [('A', 10)]
59-
expected_output = np.array(10)
60+
expected_output = np.array([10])
6061
actual_output = task_func(list_of_pairs)
6162
self.assertTrue(np.array_equal(actual_output, expected_output))
6263

6364
def test_case_5(self):
6465
# Test case with zeros
6566
list_of_pairs = [('A', 0), ('B', 5), ('C', 10)]
66-
expected_output = np.array(0)
67+
expected_output = np.array([0])
6768
actual_output = task_func(list_of_pairs)
6869
self.assertTrue(np.array_equal(actual_output, expected_output))

data/raw/f_1031_zhihan_refined.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,11 @@ def f_1031(list_of_pairs):
2222
>>> print(product_array)
2323
360
2424
"""
25-
# Extract the second element from each tuple using a list comprehension
26-
values = [pair[1] for pair in list_of_pairs]
27-
28-
# Use reduce to calculate the product of all elements in the values list
29-
product = reduce(lambda x, y: x * y, values)
30-
31-
# Return the result as a numpy array with a single element
32-
return np.array([product])
25+
second_values = [pair[1] for pair in list_of_pairs]
26+
product = reduce(np.multiply, second_values)
27+
product_array = np.array([product])
28+
29+
return product_array
3330

3431
import unittest
3532
import numpy as np
@@ -46,36 +43,36 @@ class TestCases(unittest.TestCase):
4643
def test_case_1(self):
4744
# Basic test case with positive and negative numbers
4845
list_of_pairs = [('Fruits', 5), ('Vegetables', 9), ('Dairy', -1), ('Bakery', -2), ('Meat', 4)]
49-
expected_output = np.array(360)
46+
expected_output = np.array([360])
5047
actual_output = f_1031(list_of_pairs)
5148
print(actual_output, expected_output)
5249
self.assertTrue(np.array_equal(actual_output, expected_output))
5350

5451
def test_case_2(self):
5552
# Test case with all positive numbers
5653
list_of_pairs = [('A', 2), ('B', 3), ('C', 4)]
57-
expected_output = np.array(24)
54+
expected_output = np.array([24])
5855
actual_output = f_1031(list_of_pairs)
5956
self.assertTrue(np.array_equal(actual_output, expected_output))
6057

6158
def test_case_3(self):
6259
# Test case with all negative numbers
6360
list_of_pairs = [('A', -2), ('B', -3), ('C', -4)]
64-
expected_output = np.array(-24)
61+
expected_output = np.array([-24])
6562
actual_output = f_1031(list_of_pairs)
6663
self.assertTrue(np.array_equal(actual_output, expected_output))
6764

6865
def test_case_4(self):
6966
# Test case with a single tuple
7067
list_of_pairs = [('A', 10)]
71-
expected_output = np.array(10)
68+
expected_output = np.array([10])
7269
actual_output = f_1031(list_of_pairs)
7370
self.assertTrue(np.array_equal(actual_output, expected_output))
7471

7572
def test_case_5(self):
7673
# Test case with zeros
7774
list_of_pairs = [('A', 0), ('B', 5), ('C', 10)]
78-
expected_output = np.array(0)
75+
expected_output = np.array([0])
7976
actual_output = f_1031(list_of_pairs)
8077
self.assertTrue(np.array_equal(actual_output, expected_output))
8178
if __name__ == "__main__":

0 commit comments

Comments
 (0)