Skip to content

Latest commit

 

History

History
259 lines (255 loc) · 6.91 KB

File metadata and controls

259 lines (255 loc) · 6.91 KB

Results of GPT-3.5 on HumanEval/132

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

Prompt

Tokens: 159

def is_nested(string):
    '''
    Create a function that takes a string as input which contains only square brackets.
    The function should return True if and only if there is a valid subsequence of brackets 
    where at least one bracket in the subsequence is nested.

    is_nested('[[]]') ➞ True
    is_nested('[]]]]]]][[[[[]') ➞ False
    is_nested('[][]') ➞ False
    is_nested('[]') ➞ False
    is_nested('[[][]]') ➞ True
    is_nested('[[]][[') ➞ True
    '''

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

Correct solution

def is_nested(string):
    '''
    Create a function that takes a string as input which contains only square brackets.
    The function should return True if and only if there is a valid subsequence of brackets 
    where at least one bracket in the subsequence is nested.

    is_nested('[[]]') ➞ True
    is_nested('[]]]]]]][[[[[]') ➞ False
    is_nested('[][]') ➞ False
    is_nested('[]') ➞ False
    is_nested('[[][]]') ➞ True
    is_nested('[[]][[') ➞ True
    '''opening_bracket_index = []
    closing_bracket_index = []
    for i in range(len(string)):
        if string[i] == '[':
            opening_bracket_index.append(i)
        else:
            closing_bracket_index.append(i)
    closing_bracket_index.reverse()
    cnt = 0
    i = 0
    l = len(closing_bracket_index)
    for idx in opening_bracket_index:
        if i < l and idx < closing_bracket_index[i]:
            cnt += 1
            i += 1
    return cnt >= 2

Unit tests

def check(candidate):

    # Check some simple cases
    assert candidate('[[]]') == True, "This prints if this assert fails 1 (good for debugging!)"
    assert candidate('[]]]]]]][[[[[]') == False
    assert candidate('[][]') == False
    assert candidate(('[]')) == False
    assert candidate('[[[[]]]]') == True
    assert candidate('[]]]]]]]]]]') == False
    assert candidate('[][][[]]') == True
    assert candidate('[[]') == False
    assert candidate('[]]') == False
    assert candidate('[[]][[') == True
    assert candidate('[[][]]') == True

    # Check some edge cases that are easy to work out by hand.
    assert candidate('') == False, "This prints if this assert fails 2 (also good for debugging!)"
    assert candidate('[[[[[[[[') == False
    assert candidate(']]]]]]]]') == False

Runs

❌ Run 1

Start time: 2023-07-18 17:06:12
Duration: 3.26s
Completion tokens: 68
Result: Fail

def is_nested(string):
    stack = []
    for char in string:
        if char == '[':
            stack.append(char)
        elif char == ']':
            if len(stack) > 0 and stack[-1] == '[':
                stack.pop()
            else:
                stack.append(char)
    return len(stack) > 0

❌ Run 2

Start time: 2023-07-18 17:06:09
Duration: 3.21s
Completion tokens: 76
Result: Fail

def is_nested(string):
    stack = []
    for char in string:
        if char == '[':
            stack.append(char)
        elif char == ']':
            if len(stack) > 0 and stack[-1] == '[':
                stack.pop()
            else:
                stack.append(char)
    return len(stack) < len(string) and len(stack) > 0

❌ Run 3

Start time: 2023-07-18 17:06:06
Duration: 3.52s
Completion tokens: 76
Result: Fail

def is_nested(string):
    stack = []
    for char in string:
        if char == '[':
            stack.append(char)
        elif char == ']':
            if len(stack) > 0 and stack[-1] == '[':
                stack.pop()
            else:
                stack.append(char)
    return len(stack) < len(string) and len(stack) > 0

❌ Run 4

Start time: 2023-07-18 17:06:02
Duration: 3.2s
Completion tokens: 76
Result: Fail

def is_nested(string):
    stack = []
    for char in string:
        if char == '[':
            stack.append(char)
        elif char == ']':
            if len(stack) > 0 and stack[-1] == '[':
                stack.pop()
            else:
                stack.append(char)
    return len(stack) < len(string) and len(stack) > 0

❌ Run 5

Start time: 2023-07-18 17:06:00
Duration: 2.62s
Completion tokens: 69
Result: Fail

def is_nested(string):
    stack = []
    for char in string:
        if char == '[':
            stack.append(char)
        elif char == ']':
            if len(stack) > 0 and stack[-1] == '[':
                stack.pop()
            else:
                stack.append(char)
    return len(stack) < len(string)

❌ Run 6

Start time: 2023-07-18 17:05:56
Duration: 3.42s
Completion tokens: 69
Result: Fail

def is_nested(string):
    stack = []
    for char in string:
        if char == '[':
            stack.append(char)
        elif char == ']':
            if len(stack) > 0 and stack[-1] == '[':
                stack.pop()
            else:
                stack.append(char)
    return len(stack) < len(string)

❌ Run 7

Start time: 2023-07-18 17:05:53
Duration: 3.02s
Completion tokens: 69
Result: Fail

def is_nested(string):
    stack = []
    for char in string:
        if char == '[':
            stack.append(char)
        elif char == ']':
            if len(stack) > 0 and stack[-1] == '[':
                stack.pop()
            else:
                stack.append(char)
    return len(stack) < len(string)

❌ Run 8

Start time: 2023-07-18 17:05:50
Duration: 3.21s
Completion tokens: 69
Result: Fail

def is_nested(string):
    stack = []
    for char in string:
        if char == '[':
            stack.append(char)
        elif char == ']':
            if len(stack) > 0 and stack[-1] == '[':
                stack.pop()
            else:
                stack.append(char)
    return len(stack) < len(string)

❌ Run 9

Start time: 2023-07-18 17:05:46
Duration: 3.35s
Completion tokens: 69
Result: Fail

def is_nested(string):
    stack = []
    for char in string:
        if char == '[':
            stack.append(char)
        elif char == ']':
            if len(stack) > 0 and stack[-1] == '[':
                stack.pop()
            else:
                stack.append(char)
    return len(stack) < len(string)

❌ Run 10

Start time: 2023-07-18 17:05:43
Duration: 2.71s
Completion tokens: 69
Result: Fail

def is_nested(string):
    stack = []
    for char in string:
        if char == '[':
            stack.append(char)
        elif char == ']':
            if len(stack) > 0 and stack[-1] == '[':
                stack.pop()
            else:
                stack.append(char)
    return len(stack) < len(string)