나는 이렇게 학습한다/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()]

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

0825. Find the first non-consecutive number  (0) 2022.08.25
0824. N-th Power  (0) 2022.08.24
Deodorant Evaporator  (0) 2022.08.22
Sum of Minimums!  (0) 2022.08.21
Flatten and sort an array  (0) 2022.08.20