Monday, February 28, 2022

Alternate capitalization Python Codewars

Task:

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):
    res1 = ''
    res2 = ''
    for i, j in enumerate(s):
        if i % 2 == 0:
            res1 += j.upper()
        else:
            res1 += j
        if i % 2 != 0:
            res2 += j.upper()
        else:
            res2 += j
    return [res1, res2]

def capitalize(s):
    s = ''.join(c if i%2 else c.upper() for i,c in enumerate(s))
    return [s, s.swapcase()]
    
print(capitalize("abcdef"))

Alternate capitalization Solutions | Codewars



Related Posts


No comments :

Post a Comment