Python Challenge 1 - Capital indexes

||
Posted 7 months, 2 weeks ago
||
Views 167
||
1 min read
0 reactions

Write a function named capital_indexes. The function takes a single parameter, which is a string. Your function should return a list of all the indexes in the string that have capital letters.

For example, calling capital_indexes("HeLlO") should return the list [0, 2, 4].

Approach 1

def capital_indexes(string):
    out = []
    for index,val in enumerate(string):
        if ord(val) >= 65 and ord(val) <= 90:
            out.append(index)
    return out

Approach 2

# naive solution
def capital_indexes(s):
    upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = []
    for i, l in enumerate(s):
        if l in upper:
            result.append(i)
    return result

Approach 3

from string import uppercase
def capital_indexes(s):
    return [i for i in range(len(s)) if s[i] in uppercase]

# you can also use the .isupper() string method.


0 reactions

Discussion


Looking for Freelancing Jobs
Joined on April 15, 2020


Latest Videos


Wanna Post Ad?
Reach a large group of audience by posting an ad about your business here. For more details
Drop us a Message