← run

he-027

1.000
1/1 tests· basic
Challenge · difficulty 1/5
# HumanEval/27

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 flip_case(string: str) -> str:
    """ For a given string, flip lowercase characters to uppercase and uppercase to lowercase.
    >>> flip_case('Hello')
    'hELLO'
    """
```

<!-- imported from OpenAI HumanEval (HumanEval/27) -->
tests/test_he_027.py
# Auto-generated from OpenAI HumanEval HumanEval/27. Do not edit by hand.
from solution import *  # noqa: F401,F403 (prompt helpers may be referenced by tests)
from solution import flip_case as candidate

METADATA = {
    'author': 'jt',
    'dataset': 'test'
}


def check(candidate):
    assert candidate('') == ''
    assert candidate('Hello!') == 'hELLO!'
    assert candidate('These violent delights have violent ends') == 'tHESE VIOLENT DELIGHTS HAVE VIOLENT ENDS'


def test_he_027():
    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 flip_case(string: str) -> str:
    """ For a given string, flip lowercase characters to uppercase and uppercase to lowercase.
    >>> flip_case('Hello')
    'hELLO'
    """
    return string.swapcase()
```

Test output (stdout)

.                                                                        [100%]
1 passed in 0.01s
Reasoning (chain-of-thought)
The user wants me to implement a function `flip_case` that takes a string and flips the case of each character:
- Lowercase characters become uppercase
- Uppercase characters become lowercase

This is a straightforward task. Python strings have a built-in `swapcase()` method that does exactly this.

Let me implement it:

```python
def flip_case(string: str) -> str:
    return string.swapcase()
```

Or I could do it manually:

```python
def flip_case(string: str) -> str:
    result = []
    for char in string:
        if char.islower():
            result.append(char.upper())
        elif char.isupper():
            result.append(char.lower())
        else:
            result.append(char)
    return ''.join(result)
```

The `swapcase()` method is the cleanest solution. Let me use that.