Find Capital Indexes

Lazy Code Crafters - Feb 14 - - Dev Community

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

This is the challenge here. There are many ways to achieve it as you know in programming you can achieve a result using different methods. The main takeaway is your method should be good not some junk and throwing different methods and functions here and there.

So I solved this problem with two different approach. One is how I think a beginner will try to do and other is an experienced python programmer will do.

So let's check them both.......

The Beginner Way

Image description

Here we're taking a variable val as a list and then using for loop on our word parameter. After that we are checking if the character is capital or not if it is then we will take its index and append in our val variable. At last we will return the val variable and that's it.

This is a beginner approach. Now let's lok how an experienced programmer will handle this.

The Experienced Way

Image description

_In this approach we're using list comprehension to first loop over our word. And we are using enumerate method so we can get the index as well as the character. After looping we are checking if the character is capital or not; if it is then we'll take its index and return it. That's it.
_
I hope you liked how I have solved it. If you will solve this problem any way and that's better than this please let me know. I'd love to know.

Thank You!!!

.