DEV Community

Cover image for Python challenges 1
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on • Edited on

Python challenges 1

level of challenge = 2/10

Capital indexes

  • 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].

Hint

  • Your code should consider each letter in the string at a time.
  • Keep track of the current index; you can do this with enumerate() or manually.
  • To check if a letter is uppercase, you can use the .isupper() method,
  • or use the in operator to see if the letter is in "ABCD..XYZ".

My solution

def capital_indexes(word):
 char_list = []
 for index, char in enumerate(word):
   if char.isupper():
     char_list.append(index) 
   return char_list
print(capital_indexes("HeLlO"))
Enter fullscreen mode Exit fullscreen mode

Another solution

naive solution

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

print(capital_indexes("TiTo"))
Enter fullscreen mode Exit fullscreen mode

shorter version

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

You can your solution in the comment

Top comments (2)

Collapse
 
oritrix profile image
Oritrix

Why is this not a solution?

def capital_indexes(word):
list = []
p = 0
for i in str(word):
if i.isupper():
list.append(p)
p = p + 1

print(list)
Enter fullscreen mode Exit fullscreen mode

capital_indexes("HeLlO")

This does everything it should? but i'm confused?

Collapse
 
aheisleycook profile image
privatecloudev

You can import string
String.ascii_uppercae