Code Wars Python Challenge: Vowel Count

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@themarkymark·
0.000 HBD
Code Wars Python Challenge: Vowel Count
![image.png](https://files.peakd.com/file/peakd-hive/themarkymark/S50BYhed-image.png)

I haven't done a Code Wars challenge in a little bit, so I logged in and grabbed my next challenge.

https://i.imgur.com/w6kVqWS.png

This one looks pretty easy, but let's walk through it.

Our starting code looks like this:

https://i.imgur.com/pWq82CE.png

We are given a few example tests, but the solution will be run through more random tests as well.

https://i.imgur.com/XNfgafP.png

As always, let's identify the problems we need to solve.

* Process each character of the input string
* Count each occurrence of any vowel outside of Y
* Return count

Seeing as I don't know any built-in that understands vowels, we are going to need to make our own collection.  Let's use a set.

https://i.imgur.com/1EOEbQp.png

Using a set allows us to specify our collection and ignore Y easily.

We are going to next iterate through each value of the string.  Let's do that with a for loop.

https://i.imgur.com/qWnwBm9.png

Let's test this, as this looks like a complete solution.

https://i.imgur.com/ajSnti4.png

All tests pass!  There is a lot of code here for such a simple problem, if you remember my previous lessons we had some more python ways to handle this sort of problem.  I also don't think we need a counter variable.  Let's refactor!

We can eliminate half the code by using a **list comprehension**.

https://i.imgur.com/hnFpU9Q.png
https://i.imgur.com/sHQXj9Z.png

This is much cleaner, and passes all test.  But we can do better.

https://i.imgur.com/akHKajJ.png

Let's remove the counter variable and just return directly.  This cuts our solution down to 2 lines of code.  This is likely as succinct as you can get for this problem, but I want to bring up one caveat.  While returning the count directly we have eliminated most of the useless code, we do open ourselves to one problem.  I'll tell you about that in a second, let's submit this and compare to other solutions first.

https://i.imgur.com/yV5tcMy.png

Complete series of tests pass, we have a good solution.

https://i.imgur.com/f5Sidtq.png

There is always someone smarter and more clever.  The top solution is a single line of code.  I am not convinced it is better though.  It is a little harder to read, and not as obvious, but still good code.  I'm happy with our solution and that's the way I would do it even after reviewing "better" solutions.

Now earlier I mentioned we have a caveat with our solution.  Namely the fact we performed an action and directly returned it.  While this is good a lot of the time, keep in mind in real world coding you likely want to do some validation on your output before returning it.  In this case the code is pretty simple and trust worthy, many cases you will want to validate prior to using return.  Keep that in mind when coding, ask yourself is this output sanitized, does it need error handling, are there scenarios this may fail?  This probing questions will help you find bugs before they become bugs.

Hope you enjoy this lesson, if you like this series let me know in the comments.


---

<center>Securely chat with me on [Keybase](https://keybase.io/officialmarky)
<center>https://images.hive.blog/0x0/https://steemitimages.com/DQmcWxV1dpA1eAtw2ipwZiWZkydyVNU5LaLa2Ak1GUnbGmS/The-Marky-Mark.png </center>
# <center>  [Why you should vote me as witness](https://peakd.com/witness-category/@themarkymark/why-you-should-vote-for-themarkymark-as-witness)  </center> #

[![](https://steemitimages.com/DQmPkQhAQceC7aHr4Gy5GKv7LMoiy47P7PyaNCeZBHMpFke/white-bg-with-drop-shadow.gif)](https://hivesigner.com/sign/account-witness-vote?witness=themarkymark&approve=1)     
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,