7 kyu Find the capitals
https://www.codewars.com/kata/539ee3b6757843632d00026b/python
Write a function that takes a single non-empty string of only lowercase and uppercase ascii letters (word) as its argument, and returns an ordered list containing the indices of all capital (uppercase) letters in the string.
*verbalization
Take word in order from words.(use for i in words)
Search it it is capital. (use isupper())
Change to ordered list (use index)
Return list numbers (use append())
*Code
def capitals(words):
n = []
for i in words:
if i.isupper():
n.append(words.index(i))
return n
-> Passed in TEST, but failed at ATTEMPT.
Failed examples:
("sHxbDKysubDswD")
[1, 4, 5, 4, 4] should equal [1, 4, 5, 10, 13]
("bGfaIEiZcjbKrKsWryduimBW")
[1, 4, 5, 7, 11, 11, 15, 22, 15] should equal [1, 4, 5, 7, 11, 13, 15, 22, 23]
Why does this happen? It seems to happen in long sentences..the errors happen at same alphabet that already appeared? But why?
-> The index() method finds the FIRST occurrence of the specified value.
(reference: https://www.w3schools.com/python/ref_string_index.asp)
Use enumerate? So,
*verbalization
Take word in order from words and Change to ordered list (use for a, b in enumerate(A, n):)
Search it it is capital. (use isupper())
Return list numbers (use append())
def capitals(words):
n = []
for a,b in enumerate(words):
if b.isupper():
n.append(a)
return n