Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Caps Lock Annotated solution in Clear category for Caps Lock by BootzenKatzen
def caps_lock(text: str) -> str:
fat_fingers = "" # creating an empty string to add our new text to
a_count = 0 # counting instances of "a" (Caps lock hits)
for letter in text: # iterating through our text
if letter == 'a': # if that letter is 'a'
a_count += 1 # we don't add it to our string, we add to our count
elif a_count % 2 == 0 or \
letter in "!#$%&'()*+, -./:;<=>?@[\]^_`{|}~": # if the a count is even, Caps Lock is off
# and nothing special happens with punctuation
fat_fingers += letter # so we add the normal character to the string
else: # if the a count is odd - caps lock is on
fat_fingers += letter.upper() # so add the uppercase letter to the string
return fat_fingers # return our completed string
# If Caps lock worked like it normally does (i.e. shift + d with caps lock on is 'd' instead of 'D):
# (I did this at first then wondered why I got the wrong results XD )
def caps_lock_real(text: str) -> str:
fat_fingers = ""
a_count = 0
for letter in text:
if letter == 'a':
a_count += 1
elif a_count % 2 == 0 or \
letter in "!#$%&'()*+, -./:;<=>?@[\]^_`{|}~":
fat_fingers += letter # it's identical up to this point so:
else: # if the a count is odd - caps lock is on
if letter.islower(): # if the letter was originally lowercase
fat_fingers += letter.upper() # Make it uppercase
else: # if it was already uppercase
fat_fingers += letter.lower() # make it lowercase
return fat_fingers
if __name__ == "__main__":
print("Example:")
print(caps_lock("Why are you asking me that?"))
# These "asserts" are used for self-checking and not for an auto-testing
assert caps_lock("Why are you asking me that?") == "Why RE YOU sking me thT?"
assert caps_lock("Always wanted to visit Zambia.") == "AlwYS Wnted to visit ZMBI."
assert caps_lock("Aloha from Hawaii") == "Aloh FROM HwII"
print("Coding complete? Click 'Check' to earn cool rewards!")
June 15, 2023
Comments: