Monday, September 2, 2019

IslandT: Capitalize the letters that occupy even indexes and odd indexes separately

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

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



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...