bcb-0003
1.000
Challenge · difficulty 2/5
# BigCodeBench/3
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`, `random`.
```python
import random
import numpy as np
def task_func(LETTERS):
"""
Create a dictionary where keys are specified letters and values are lists of random integers.
Then calculate the mean of these integers for each key and return a dictionary of these means.
Parameters:
LETTERS (list of str): List of single-character strings to be used as keys in the output dictionary.
Returns:
dict: A dictionary where each key is a letter from the input list and the value is the mean of
a randomly generated list of integers (with each list having 1 to 10 integers ranging from 0 to 100).
Requirements:
- random
- np (numpy)
Example:
>>> LETTERS = ['a', 'b', 'c']
>>> mean_dict = task_func(LETTERS)
>>> isinstance(mean_dict, dict)
True
>>> 'a' in mean_dict.keys() and 'b' in mean_dict.keys() and 'c' in mean_dict.keys()
True
>>> all(isinstance(v, float) for v in mean_dict.values()) # Check if all values are floats
True
"""
```
<!-- imported from BigCodeBench (BigCodeBench/3) -->
tests/test_bcb_0003.py
# Auto-generated from BigCodeBench BigCodeBench/3. Do not edit by hand.
import pathlib as _pathlib
exec(_pathlib.Path(__file__).with_name("solution.py").read_text(), globals())
import unittest
class TestCases(unittest.TestCase):
def setUp(self):
# Common setup for all tests: explicitly define the list of letters
self.letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def test_case_1(self):
# Test if the function returns a dictionary
mean_dict = task_func(self.letters)
self.assertIsInstance(mean_dict, dict)
def test_case_2(self):
# Test if the dictionary contains all letters of the alphabet
mean_dict = task_func(self.letters)
self.assertTrue(all(letter in mean_dict for letter in self.letters))
def test_case_3(self):
# Test if the values in the dictionary are floats (means of lists of integers)
mean_dict = task_func(self.letters)
self.assertTrue(all(isinstance(val, float) for val in mean_dict.values()))
def test_case_4(self):
# Test if the mean values are reasonable given the range of random integers (0-100)
mean_dict = task_func(self.letters)
self.assertTrue(all(0 <= val <= 100 for val in mean_dict.values()))
def test_case_5(self):
# Test if the dictionary has 26 keys (one for each letter of the alphabet)
mean_dict = task_func(self.letters)
self.assertEqual(len(mean_dict), 26)
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 random
import numpy as np
def task_func(LETTERS):
"""
Create a dictionary where keys are specified letters and values are lists of random integers.
Then calculate the mean of these integers for each key and return a dictionary of these means.
Parameters:
LETTERS (list of str): List of single-character strings to be used as keys in the output dictionary.
Returns:
dict: A dictionary where each key is a letter from the input list and the value is the mean of
a randomly generated list of integers (with each list having 1 to 10 integers ranging from 0 to 100).
Requirements:
- random
- np (numpy)
Example:
>>> LETTERS = ['a', 'b', 'c']
>>> mean_dict = task_func(LETTERS)
>>> isinstance(mean_dict, dict)
True
>>> 'a' in mean_dict.keys() and 'b' in mean_dict.keys() and 'c' in mean_dict.keys()
True
>>> all(isinstance(v, float) for v in mean_dict.values()) # Check if all values are floats
True
"""
mean_dict = {}
for letter in LETTERS:
# Generate a random list of integers (1 to 10 integers, each between 0 and 100)
list_length = random.randint(1, 10)
random_integers = [random.randint(0, 100) for _ in range(list_length)]
# Calculate the mean using numpy
mean_value = np.mean(random_integers)
# Store in dictionary
mean_dict[letter] = float(mean_value) # Convert to float for consistency
return mean_dict
```Test output (stdout)
..... [100%] 5 passed in 0.08s