Skip to content

Latest commit

 

History

History
274 lines (268 loc) · 6.58 KB

File metadata and controls

274 lines (268 loc) · 6.58 KB

Results of GPT-3.5 on HumanEval/161

Date: 18 July, 2023
Model: gpt-3.5-turbo
Temperature: 0.2
Passing runs: 10
Total runs: 10
Success rate: 100%

Prompt

Tokens: 133

def solve(s):
    """You are given a string s.
    if s[i] is a letter, reverse its case from lower to upper or vise versa, 
    otherwise keep it as it is.
    If the string contains no letters, reverse the string.
    The function should return the resulted string.
    Examples
    solve("1234") = "4321"
    solve("ab") = "AB"
    solve("#a@C") = "#A@c"
    """

The above is an incomplete Python code fragment. Return the complete and correct code with no additional text.

Correct solution

def solve(s):
    """You are given a string s.
    if s[i] is a letter, reverse its case from lower to upper or vise versa, 
    otherwise keep it as it is.
    If the string contains no letters, reverse the string.
    The function should return the resulted string.
    Examples
    solve("1234") = "4321"
    solve("ab") = "AB"
    solve("#a@C") = "#A@c"
    """flg = 0
    idx = 0
    new_str = list(s)
    for i in s:
        if i.isalpha():
            new_str[idx] = i.swapcase()
            flg = 1
        idx += 1
    s = ""
    for i in new_str:
        s += i
    if flg == 0:
        return s[len(s)::-1]
    return s

Unit tests

def check(candidate):

    # Check some simple cases
    assert candidate("AsDf") == "aSdF"
    assert candidate("1234") == "4321"
    assert candidate("ab") == "AB"
    assert candidate("#a@C") == "#A@c"
    assert candidate("#AsdfW^45") == "#aSDFw^45"
    assert candidate("#6@2") == "2@6#"

    # Check some edge cases that are easy to work out by hand.
    assert candidate("#$a^D") == "#$A^d"
    assert candidate("#ccc") == "#CCC"

    # Don't remove this line:

Runs

✅ Run 1

Start time: 2023-07-18 17:01:32
Duration: 5.4s
Completion tokens: 144
Result: Pass

def solve(s):
    """You are given a string s.
    if s[i] is a letter, reverse its case from lower to upper or vise versa, 
    otherwise keep it as it is.
    If the string contains no letters, reverse the string.
    The function should return the resulted string.
    Examples
    solve("1234") = "4321"
    solve("ab") = "AB"
    solve("#a@C") = "#A@c"
    """
    if not any(c.isalpha() for c in s):
        return s[::-1]
    else:
        return ''.join(c.upper() if c.islower() else c.lower() for c in s)

✅ Run 2

Start time: 2023-07-18 17:01:28
Duration: 3.51s
Completion tokens: 84
Result: Pass

def solve(s):
    new_s = ""
    has_letter = False
    for char in s:
        if char.isalpha():
            has_letter = True
            if char.islower():
                new_s += char.upper()
            else:
                new_s += char.lower()
        else:
            new_s += char
    if not has_letter:
        new_s = new_s[::-1]
    return new_s

✅ Run 3

Start time: 2023-07-18 17:01:25
Duration: 3.53s
Completion tokens: 84
Result: Pass

def solve(s):
    new_s = ""
    has_letters = False

    for char in s:
        if char.isalpha():
            has_letters = True
            if char.islower():
                new_s += char.upper()
            else:
                new_s += char.lower()
        else:
            new_s += char

    if not has_letters:
        new_s = new_s[::-1]

    return new_s

✅ Run 4

Start time: 2023-07-18 17:01:19
Duration: 5.6s
Completion tokens: 144
Result: Pass

def solve(s):
    """You are given a string s.
    if s[i] is a letter, reverse its case from lower to upper or vise versa, 
    otherwise keep it as it is.
    If the string contains no letters, reverse the string.
    The function should return the resulted string.
    Examples
    solve("1234") = "4321"
    solve("ab") = "AB"
    solve("#a@C") = "#A@c"
    """
    if not any(c.isalpha() for c in s):
        return s[::-1]
    else:
        return ''.join(c.upper() if c.islower() else c.lower() for c in s)

✅ Run 5

Start time: 2023-07-18 17:01:15
Duration: 3.54s
Completion tokens: 84
Result: Pass

def solve(s):
    new_s = ""
    has_letter = False
    for char in s:
        if char.isalpha():
            has_letter = True
            if char.islower():
                new_s += char.upper()
            else:
                new_s += char.lower()
        else:
            new_s += char
    if not has_letter:
        new_s = new_s[::-1]
    return new_s

✅ Run 6

Start time: 2023-07-18 17:01:12
Duration: 3.08s
Completion tokens: 59
Result: Pass

def solve(s):
    letters = [c for c in s if c.isalpha()]
    if len(letters) == 0:
        return s[::-1]
    else:
        return ''.join([c.upper() if c.islower() else c.lower() for c in s])

✅ Run 7

Start time: 2023-07-18 17:01:09
Duration: 3.3s
Completion tokens: 84
Result: Pass

def solve(s):
    new_s = ""
    has_letter = False
    for char in s:
        if char.isalpha():
            has_letter = True
            if char.islower():
                new_s += char.upper()
            else:
                new_s += char.lower()
        else:
            new_s += char
    if not has_letter:
        new_s = new_s[::-1]
    return new_s

✅ Run 8

Start time: 2023-07-18 17:01:05
Duration: 3.68s
Completion tokens: 84
Result: Pass

def solve(s):
    new_s = ""
    has_letter = False
    for char in s:
        if char.isalpha():
            has_letter = True
            if char.islower():
                new_s += char.upper()
            else:
                new_s += char.lower()
        else:
            new_s += char
    if not has_letter:
        new_s = new_s[::-1]
    return new_s

✅ Run 9

Start time: 2023-07-18 17:01:01
Duration: 4.08s
Completion tokens: 84
Result: Pass

def solve(s):
    new_s = ""
    has_letter = False
    for char in s:
        if char.isalpha():
            has_letter = True
            if char.islower():
                new_s += char.upper()
            else:
                new_s += char.lower()
        else:
            new_s += char
    if not has_letter:
        new_s = new_s[::-1]
    return new_s

✅ Run 10

Start time: 2023-07-18 17:00:58
Duration: 3.03s
Completion tokens: 59
Result: Pass

def solve(s):
    letters = [c for c in s if c.isalpha()]
    if len(letters) == 0:
        return s[::-1]
    else:
        return ''.join([c.upper() if c.islower() else c.lower() for c in s])