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 task is to implement a function `count_distinct_characters` that counts the number of distinct characters in a string, regardless of case.
Looking at the examples:
- `count_distinct_characters('xyzXYZ')` returns 3 (x, y, z - case insensitive)
- `count_distinct_characters('Jerry')` returns 4 (J, e, r, r, y - but J and j are the same, so J, e, r, y = 4 distinct characters)
So I need to:
1. Convert all characters to the same case (either upper or lower)
2. Count the unique characters
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:
- For 'xyzXYZ': lower() gives 'xyzxyz', set gives {'x', 'y', 'z'}, len is 3
- For 'Jerry': lower() gives 'jerry', set gives {'j', 'e', 'r', 'y'}, len is 4
This is a simple and correct solution.