나는 이렇게 학습한다/Algorithm & SQL
Alternate capitalization
daco2020
2022. 8. 23. 12:21
Given a string, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index 0 will be considered even.
For example, capitalize("abcdef") = ['AbCdEf', 'aBcDeF']. See test cases for more examples.
The input will be a lowercase string with no spaces.
Good luck!
Solution:
def capitalize(s):
return [''.join(v.lower() if i%2==1 else v.upper() for i, v in enumerate(s)),
''.join(v.lower() if i%2==0 else v.upper() for i, v in enumerate(s))]
Other Solution:
def capitalize(s):
s = ''.join(c if i%2 else c.upper() for i,c in enumerate(s))
return [s, s.swapcase()]