← run

bcb-0010

1.000
6/6 tests· lib-knowledge
Challenge · difficulty 4/5
# BigCodeBench/10

Implement a file **`solution.py`** that completes the function below. Keep the given name and signature; define `task_func` at module level.

Allowed libraries: `statistics`, `numpy`, `itertools`, `random`.

```python
import numpy as np
import itertools
import random
import statistics

def task_func(T1, RANGE=100):
    """
    Convert elements in 'T1' to integers and create a list of random integers.
    The size of the list is the sum of the integers in `T1`. Calculate and 
    return the mean, median, and mode of the list.
    
    Parameters:
    T1 (tuple of tuples): Each tuple contains string representations of integers which are converted to integers.
    RANGE (int, optional): The upper limit for generating random integers. Default is 100.
    
    Returns:
    tuple: A tuple containing the mean, median, and mode of the generated list of random integers.
           The mean and median are floats, and the mode is an integer. The calculations use the generated
           list whose size is determined by the sum of converted integers from `T1`.
    
    Requirements:
    - numpy
    - itertools
    - random
    - statistics

    Raises:
    statistics.StatisticsError if T1 is empty
    
    Example:
    >>> import random
    >>> random.seed(42)
    >>> T1 = (('13', '17', '18', '21', '32'), ('07', '11', '13', '14', '28'), ('01', '05', '06', '08', '15', '16'))
    >>> stats = task_func(T1)
    >>> print(stats)
    (49.88, 48.0, 20)
    >>> stats = task_func(T1, RANGE=50)
    >>> print(stats)
    (23.773333333333333, 25.0, 15)
    """
```

<!-- imported from BigCodeBench (BigCodeBench/10) -->
tests/test_bcb_0010.py
# Auto-generated from BigCodeBench BigCodeBench/10. Do not edit by hand.
import pathlib as _pathlib
exec(_pathlib.Path(__file__).with_name("solution.py").read_text(), globals())

import unittest
import numpy as np
import statistics
from unittest.mock import patch
class TestCases(unittest.TestCase):
    @patch('random.randint', return_value=50)
    def test_case_1(self, mock_randint):
        """Tests with small numbers and default range."""
        T1 = (('1', '2'), ('2', '3'), ('3', '4'))
        mean, median, mode = task_func(T1)
        total_elements = sum(map(int, sum(T1, ())))
        self.assertEqual(total_elements, 15)  # Check if the total_elements calculation is correct
        self.assertTrue(isinstance(mean, float))
        self.assertTrue(isinstance(median, float))
        self.assertTrue(isinstance(mode, int))
    @patch('random.randint', return_value=50)
    def test_case_2(self, mock_randint):
        """Tests with mid-range numbers and default range."""
        T1 = (('1', '2', '3'), ('4', '5'), ('6', '7', '8', '9'))
        mean, median, mode = task_func(T1)
        self.assertEqual(mean, 50.0)
        self.assertEqual(median, 50.0)
        self.assertEqual(mode, 50)
    @patch('random.randint', return_value=25)
    def test_case_3(self, mock_randint):
        """Tests with adjusted range to 50, checks new bounds."""
        T1 = (('1', '2', '3'), ('4', '5'), ('6', '7', '8', '9'))
        mean, median, mode = task_func(T1, RANGE=50)
        self.assertEqual(mean, 25.0)
        self.assertEqual(median, 25.0)
        self.assertEqual(mode, 25)
    @patch('random.randint', return_value=75)
    def test_case_4(self, mock_randint):
        """Tests with minimal input of single-digit numbers."""
        T1 = (('1',), ('2',), ('3',))
        mean, median, mode = task_func(T1)
        self.assertEqual(mean, 75.0)
        self.assertEqual(median, 75.0)
        self.assertEqual(mode, 75)
    @patch('random.randint', return_value=10)
    def test_case_5(self, mock_randint):
        """Tests with larger numbers, focusing on correct type checking."""
        T1 = (('10', '20', '30'), ('40', '50'), ('60', '70', '80', '90'))
        mean, median, mode = task_func(T1)
        self.assertEqual(mean, 10.0)
        self.assertEqual(median, 10.0)
        self.assertEqual(mode, 10)
    def test_empty_input(self):
        """Tests behavior with an empty tuple input."""
        T1 = ()
        with self.assertRaises(statistics.StatisticsError):
            mean, median, mode = task_func(T1)
System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.
Proposed solution
```python
import numpy as np
import itertools
import random
import statistics

def task_func(T1, RANGE=100):
    """
    Convert elements in 'T1' to integers and create a list of random integers.
    The size of the list is the sum of the integers in `T1`. Calculate and 
    return the mean, median, and mode of the list.
    
    Parameters:
    T1 (tuple of tuples): Each tuple contains string representations of integers which are converted to integers.
    RANGE (int, optional): The upper limit for generating random integers. Default is 100.
    
    Returns:
    tuple: A tuple containing the mean, median, and mode of the generated list of random integers.
           The mean and median are floats, and the mode is an integer. The calculations use the generated
           list whose size is determined by the sum of converted integers from `T1`.
    
    Requirements:
    - numpy
    - itertools
    - random
    - statistics

    Raises:
    statistics.StatisticsError if T1 is empty
    """
    # Check if T1 is empty
    if not T1:
        raise statistics.StatisticsError("T1 is empty")
    
    # Convert string representations to integers and flatten the tuples
    int_values = []
    for inner_tuple in T1:
        for item in inner_tuple:
            int_values.append(int(item))
    
    # Calculate the total size of the random list
    total_size = sum(int_values)
    
    # Handle case where total_size is 0
    if total_size == 0:
        raise statistics.StatisticsError("Total size is 0")
    
    # Generate random integers in the range [0, RANGE-1]
    random_list = [random.randint(0, RANGE-1) for _ in range(total_size)]
    
    # Calculate mean and median using numpy
    mean_val = float(np.mean(random_list))
    median_val = float(np.median(random_list))
    
    # Calculate mode using statistics
    mode_val = int(statistics.mode(random_list))
    
    return (mean_val, median_val, mode_val)
```

Test output (stdout)

......                                                                   [100%]
6 passed in 0.07s