openai / human-eval

Code for the paper "Evaluating Large Language Models Trained on Code"
MIT License
2.31k stars 330 forks source link

Error in canonical solution 95 check_dict_case #22

Open PootieT opened 1 year ago

PootieT commented 1 year ago
def check_dict_case(dict: Dict[str, str]) -> bool:
    """
    Given a dictionary, return True if all keys are strings in lower 
    case or all keys are strings in upper case, else return False.
    The function should return False is the given dictionary is empty.
    Examples:
    # >>> check_dict_case({"a":"apple", "b":"banana"})
    # True
    # >>> check_dict_case({"a":"apple", "A":"banana", "B":"banana"})
    # False
    # >>> check_dict_case({"a":"apple", 8:"banana", "a":"apple"})
    # False
    # >>> check_dict_case({"Name":"John", "Age":"36", "City":"Houston"})
    # False
    # >>> check_dict_case({"STATE":"NC", "ZIP":"12345" })
    True
    """
    if len(dict.keys()) == 0:
        return False
    else:
        state = "start"
        for key in dict.keys():

            if isinstance(key, str) == False:
                state = "mixed"
                break
            if state == "start":
                if key.isupper():
                    state = "upper"
                elif key.islower():
                    state = "lower"
                else:
                    break
            elif (state == "upper" and not key.isupper()) or (state == "lower" and not key.islower()):
                    state = "mixed"
                    break
            else:
                break
        return state == "upper" or state == "lower"

In the solution above, the last break statement should instead be continue

In the current test

assert candidate({"p":"pineapple", "A":"banana", "B":"banana"}) == False

current solution would work because the case switch occurs in the second element, and that python happens to preserve the order in which the key, value pairs are iterated.

If the test were changed to

assert candidate({"A":"banana", "B":"banana", "p":"pineapple"}) == False

This solution would have failed the test