Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
3 Annotated Answers solution in Clear category for Correct Capital by BootzenKatzen
# Solution 1:
def correct_capital(line: str) -> bool:
if line.isupper(): # if it's all uppercase
return True # return True
elif line.islower(): # if it's all lowercase
return True # return True
elif line[0].isupper(): # if the first letter is uppercase
if line[1:].islower(): # and the rest of the letters are lowercase
return True # return True
return False # if none of the above, return False
# Solution 2 (after I worked out the logic and figured I could put it in one line):
def correct_capital2(line: str) -> bool:
return line.isupper() or line.islower() or line == line.capitalize()
# with or, if any one of these conditions is True, it will return True,
# otherwise it will return False
# isupper and islower are pretty much the same as above
# I just switched to line == line.capitalize() because I saw another solution that used .capitalize
# and I figured it would work. capitalize() capitalizes the first letter of a sentance, and makes
# the rest lowercase. So cHecKio would turn into Checkio and the two do not match so it would be False
# Solution 3:
def correct_capital3(line: str) -> bool:
return any([line.isupper(), line.islower(), line == line.capitalize()])
# pretty much the same as solution 2 but I decided to use any() which looks at a list,
# and if any of the conditions are True, it will return True - so it's the same as multiple ORs
print("Example:")
print(correct_capital("Checkio"))
# These "asserts" are used for self-checking
assert correct_capital("Checkio") == True
assert correct_capital("CheCkio") == False
assert correct_capital("CHECKIO") == True
print("The mission is done! Click 'Check Solution' to earn rewards!")
Aug. 23, 2023
Comments: