← run

bcb-0011

1.000
5/5 tests· lib-knowledge
Challenge · difficulty 3/5
# BigCodeBench/11

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

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

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


def task_func(T1, max_value=100):
    """
    Converts elements in 'T1', a tuple of tuples containing string representations 
    of integers, to integers and creates a list of random integers. The size of the 
    list equals the sum of these integers. Returns the 25th, 50th, and 75th percentiles 
    of this list.

    Parameters:
    T1 (tuple of tuple of str): A tuple of tuples, each containing string representations of integers.
    max_value (int): The upper bound for random number generation, exclusive. Default is 100.
    
    Returns:
    tuple: A tuple (p25, p50, p75) representing the 25th, 50th, and 75th percentiles of the list.

    Requirements:
    - numpy
    - itertools
    - random
    
    Example:
    >>> import random
    >>> random.seed(42)
    >>> T1 = (('13', '17', '18', '21', '32'), ('07', '11', '13', '14', '28'), ('01', '05', '06', '08', '15', '16'))
    >>> percentiles = task_func(T1)
    >>> print(percentiles)
    (24.0, 48.0, 77.0)
    """
```

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

import unittest
from unittest.mock import patch
class TestCases(unittest.TestCase):
    @patch('random.randint')
    def test_case_1(self, mock_randint):
        """Test with diverse values and the default range to ensure percentile calculation."""
        mock_randint.return_value = 50  # Mocking random.randint to always return 50
        T1 = (('13', '17', '18', '21', '32'), ('07', '11', '13', '14', '28'), ('01', '05', '06', '08', '15', '16'))
        p25, p50, p75 = task_func(T1)
        self.assertEqual(p25, 50)
        self.assertEqual(p50, 50)
        self.assertEqual(p75, 50)
    @patch('random.randint')
    def test_case_2(self, mock_randint):
        """Check consistency when the total number of elements are small but repeated."""
        mock_randint.return_value = 30  # Consistent lower value for a different perspective
        T1 = (('10',), ('10', '10', '10'))
        p25, p50, p75 = task_func(T1)
        self.assertEqual(p25, 30)
        self.assertEqual(p50, 30)
        self.assertEqual(p75, 30)
    @patch('random.randint')
    def test_case_3(self, mock_randint):
        """Ensure that percentile calculations are consistent for mixed low and medium values."""
        mock_randint.return_value = 75  # Higher consistent value
        T1 = (('5', '5', '5', '5'), ('10', '15'), ('1', '2', '3', '4', '5'))
        p25, p50, p75 = task_func(T1)
        self.assertEqual(p25, 75)
        self.assertEqual(p50, 75)
        self.assertEqual(p75, 75)
    @patch('random.randint')
    def test_case_4(self, mock_randint):
        """Tests percentile values for a simple large-value case."""
        mock_randint.return_value = 10  # Low consistent value to see impact on percentiles
        T1 = (('50',), ('25', '25'))
        p25, p50, p75 = task_func(T1)
        self.assertEqual(p25, 10)
        self.assertEqual(p50, 10)
        self.assertEqual(p75, 10)
    @patch('random.randint')
    def test_case_5(self, mock_randint):
        """Test with an extreme case where all random numbers are the same, ensuring no variability."""
        mock_randint.return_value = 90  # High consistent value
        T1 = (('1', '1', '1', '1', '1', '1', '1', '1', '1', '1'), ('10', '10'))
        p25, p50, p75 = task_func(T1)
        self.assertEqual(p25, 90)
        self.assertEqual(p50, 90)
        self.assertEqual(p75, 90)
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

def task_func(T1, max_value=100):
    # Convert all string representations of integers to integers
    integers = []
    for tup in T1:
        for s in tup:
            integers.append(int(s))

    # Calculate the sum of these integers
    total_size = sum(integers)

    # Generate a list of random integers with the calculated size
    random_list = [random.randint(0, max_value - 1) for _ in range(total_size)]

    # Calculate the 25th, 50th, and 75th percentiles
    p25 = np.percentile(random_list, 25)
    p50 = np.percentile(random_list, 50)
    p75 = np.percentile(random_list, 75)

    return (p25, p50, p75)
```

Test output (stdout)

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