he-016
1.000
Challenge · difficulty 1/5
# HumanEval/16
Implement a file **`solution.py`** that completes the function below. Keep the given name and signature; your file must define it at module level.
```python
def count_distinct_characters(string: str) -> int:
""" Given a string, find out how many distinct characters (regardless of case) does it consist of
>>> count_distinct_characters('xyzXYZ')
3
>>> count_distinct_characters('Jerry')
4
"""
```
<!-- imported from OpenAI HumanEval (HumanEval/16) -->
tests/test_he_016.py
# Auto-generated from OpenAI HumanEval HumanEval/16. Do not edit by hand.
from solution import * # noqa: F401,F403 (prompt helpers may be referenced by tests)
from solution import count_distinct_characters as candidate
METADATA = {
'author': 'jt',
'dataset': 'test'
}
def check(candidate):
assert candidate('') == 0
assert candidate('abcde') == 5
assert candidate('abcde' + 'cade' + 'CADE') == 5
assert candidate('aaaaAAAAaaaa') == 1
assert candidate('Jerry jERRY JeRRRY') == 5
def test_he_016():
check(candidate)
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
def count_distinct_characters(string: str) -> int:
""" Given a string, find out how many distinct characters (regardless of case) does it consist of
>>> count_distinct_characters('xyzXYZ')
3
>>> count_distinct_characters('Jerry')
4
"""
return len(set(string.lower()))
```Test output (stdout)
. [100%]
1 passed in 0.01s
Reasoning (chain-of-thought)
The user wants me to implement a function `count_distinct_characters` that counts the number of distinct characters in a string, ignoring case.
Let me think about this:
- Input: a string
- Output: an integer representing the count of distinct characters (case-insensitive)
Examples:
- 'xyzXYZ' -> 3 (x, y, z are the distinct characters when ignoring case)
- 'Jerry' -> 4 (j, e, r, y are the distinct characters when ignoring case)
The approach is simple:
1. Convert the string to lowercase (or uppercase)
2. Use a set to get unique characters
3. Return the length of the set
Let me write the solution:
```python
def count_distinct_characters(string: str) -> int:
""" Given a string, find out how many distinct characters (regardless of case) does it consist of
>>> count_distinct_characters('xyzXYZ')
3
>>> count_distinct_characters('Jerry')
4
"""
return len(set(string.lower()))
```
This should work correctly. Let me verify:
- 'xyzXYZ'.lower() = 'xyzxyz', set = {'x', 'y', 'z'}, len = 3 ✓
- 'Jerry'.lower() = 'jerry', set = {'j', 'e', 'r', 'y'}, len = 4 ✓
Looks good.