bcb-0001
1.000
Proposed solution
```python
import collections
import random
import string
def task_func(length=100):
"""
Generate a random string of the specified length composed of uppercase and lowercase letters,
and then count the occurrence of each character in this string.
Parameters:
length (int, optional): The number of characters in the generated string. Default is 100.
Returns:
dict: A dictionary where each key is a character from the generated string and the value
is the count of how many times that character appears in the string.
Requirements:
- collections
- random
- string
Raises:
ValueError if the length is a negative number
Example:
>>> import random
>>> random.seed(42) # Ensures reproducibility for demonstration
>>> task_func(10)
{'h': 1, 'B': 2, 'O': 1, 'L': 1, 'm': 1, 'j': 1, 'u': 1, 'E': 1, 'V': 1}
"""
if length < 0:
raise ValueError("Length cannot be negative")
# Generate random string of specified length
characters = string.ascii_letters # This includes both uppercase and lowercase letters
random_string = ''.join(random.choice(characters) for _ in range(length))
# Count occurrences of each character
char_count = collections.Counter(random_string)
# Convert Counter to regular dict
return dict(char_count)
```Test output (stdout)
... [100%] 3 passed in 0.02s
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.