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

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

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

_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!!!

原文链接:Find Capital Indexes

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
I may not be perfect but at least I’m not fake.
我可能不完美,但是我至少不虚伪
评论 抢沙发

请登录后发表评论

    暂无评论内容