Given a string, capitalize the letters within the string that occupy even indexes and odd indexes separately, and return as a list! Index 0 will be considered even.

For example, capitalize(“abcdef”) = [‘AbCdEf’, ‘aBcDeF’]!

The input will be a lowercase string with no spaces.

def capitalize(s): s = list(s) li = [] stri = ” n = 1 first = False time = 0 while(time < 2): if first == False: for e in s: if n % 2 != 0: stri += e.upper() else: stri += e n+=1 first = True n = 1 li.append(stri) stri = '' time += 1 else: for e in s: if n % 2 == 0: stri += e.upper() else: stri += e n+=1 li.append(stri) time += 1 return li [/php]

Do you know we actually can achieve the above outcome with just 3 lines of code? Provide your answer in the comment box below!

Please follow and like us:

3 Comments

  1. def capitalize(s):
    return [”.join([c.upper() if not (i % 2) else c for i , c in enumerate(s)]),
    ”.join([c.upper() if i % 2 else c for i , c in enumerate(s)])]

  2. Hi, that’s fun.
    My solution in one line :

    def capitalize2(s):
    return [“”.join(letters) for letters in zip(*[(e1, e2) if i % 2 else (e2, e1) for (i, (e1, e2)) in enumerate(zip(s.lower(), s.upper()))])]

Leave a Reply

Your email address will not be published. Required fields are marked *