← 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 a Python function `flip_case` that takes a string and swaps the case of each character.
Lowercase becomes uppercase, uppercase becomes lowercase.
Python strings have a built-in method `.swapcase()` that does exactly this.
I should implement it using `.swapcase()` or manually iterate and swap.
The prompt asks to complete the function in `solution.py`.

Let's verify the signature:
```python
def flip_case(string: str) -> str:
```

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

Check constraints/edge cases:
- Empty string: `"".swapcase()` returns `""`. Correct.
- Non-alphabetic characters: `"123 !@#".swapcase()` returns `"123 !@#"`. Correct.
- Mixed case: `"Hello".swapcase()` returns `"hELLO"`. Correct.

The problem is straightforward. I will output the solution in the requested format.
I'll make sure to include the docstring as given, or just the function definition.
The prompt says: "Implement a file **`solution.py`** that completes the function below. Keep the given name and signature; your file must define it at module level."

I will provide the exact code.
No extra fluff.
Done. 
Let's produce the output. 
I'll wrap it in a python code block.
Check if any imports are needed: none.
Check function name: `flip_case`.
Check signature: `def flip_case(string: str) -> str:`
All good.
Output matches request.✅
Proceed. 
Self-Correction/Verification during thought:
Is there any trick? HumanEval/27 is exactly this. `swapcase()` is the standard solution.
I will write it out.
```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()
```
Matches perfectly.
Done. 
Output matches. 
Proceeds. 
[Output Generation] -> *Proceeds*