0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Codewards 7 kyu Find the capitals

Posted at

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?