Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in canonical solution 95 check_dict_case #22

Open
PootieT opened this issue Apr 19, 2023 · 0 comments
Open

Error in canonical solution 95 check_dict_case #22

PootieT opened this issue Apr 19, 2023 · 0 comments

Comments

@PootieT
Copy link

PootieT commented Apr 19, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant