← run

he-010

1.000
1/1 tests· basic
Challenge · difficulty 2/5
# HumanEval/10

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 is_palindrome(string: str) -> bool:
    """ Test if given string is a palindrome """
    return string == string[::-1]


def make_palindrome(string: str) -> str:
    """ Find the shortest palindrome that begins with a supplied string.
    Algorithm idea is simple:
    - Find the longest postfix of supplied string that is a palindrome.
    - Append to the end of the string reverse of a string prefix that comes before the palindromic suffix.
    >>> make_palindrome('')
    ''
    >>> make_palindrome('cat')
    'catac'
    >>> make_palindrome('cata')
    'catac'
    """
```

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

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


def check(candidate):
    assert candidate('') == ''
    assert candidate('x') == 'x'
    assert candidate('xyz') == 'xyzyx'
    assert candidate('xyx') == 'xyx'
    assert candidate('jerry') == 'jerryrrej'


def test_he_010():
    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 is_palindrome(string: str) -> bool:
    """ Test if given string is a palindrome """
    return string == string[::-1]


def make_palindrome(string: str) -> str:
    """ Find the shortest palindrome that begins with a supplied string.
    Algorithm idea is simple:
    - Find the longest postfix of supplied string that is a palindrome.
    - Append to the end of the string reverse of a string prefix that comes before the palindromic suffix.
    >>> make_palindrome('')
    ''
    >>> make_palindrome('cat')
    'catac'
    >>> make_palindrome('cata')
    'catac'
    """
    if not string:
        return string
    
    # Find the longest suffix that is a palindrome
    for i in range(len(string)):
        suffix = string[i:]
        if is_palindrome(suffix):
            # The prefix to add is the reverse of the part before the suffix
            prefix_to_add = string[:i][::-1]
            return string + prefix_to_add
    
    # Fallback (should not be reached for non-empty strings due to single char palindrome)
    return string
```

Test output (stdout)

.                                                                        [100%]
1 passed in 0.01s