반응형
altERnaTIng cAsE <=> ALTerNAtiNG CaSe
Define String.prototype.toAlternatingCase (or a similar function/method such as to_alternating_case/toAlternatingCase/ToAlternatingCase in your selected language; see the initial solution for details) such that each lowercase letter becomes uppercase and each uppercase letter becomes lowercase. For example:
"hello world".toAlternatingCase() === "HELLO WORLD"
"HELLO WORLD".toAlternatingCase() === "hello world"
"hello WORLD".toAlternatingCase() === "HELLO world"
"HeLLo WoRLD".toAlternatingCase() === "hEllO wOrld"
"12345".toAlternatingCase() === "12345" // Non-alphabetical characters are unaffected
"1a2b3c4d5e".toAlternatingCase() === "1A2B3C4D5E"
"String.prototype.toAlternatingCase".toAlternatingCase() === "sTRING.PROTOTYPE.TOaLTERNATINGcASE"
As usual, your function/method should be pure, i.e. it should not mutate the original string.
Solution:
def to_alternating_case(string):
return ''.join(i.lower() if i.isupper() else i.upper() for i in string)
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
0112. Tip Calculator (0) | 2023.01.13 |
---|---|
0111. You Can't Code Under Pressure #1 (0) | 2023.01.12 |
0109. A Needle in the Haystack (0) | 2023.01.09 |
0108. How many lightsabers do you own? (0) | 2023.01.09 |
0107. Exclamation marks series #4: Remove all exclamation marks from sentence but ensure a exclamation mark at the end of string (0) | 2023.01.07 |