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
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.
In the solution above, the last
break
statement should instead becontinue
In the current test
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
This solution would have failed the test