Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using re and upper() solution in Clear category for Caps Lock by H0r4c3
import re
def caps_lock(text: str) -> str:
start = ''
if text.startswith('A'):
start = 'A'
text = text.replace('A', '')
text_without_a = re.split('[aA]+', text)
result = [item.upper() if text_without_a.index(item) % 2 != 0 else item for item in text_without_a]
return start + ''.join(result)
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!")
Dec. 20, 2021
Comments: