Code Wars Python Challenge: Vowel Count

in #codewithmarky10 days ago

image.png

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

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

Our starting code looks like this:

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

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.

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.

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

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.


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

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.

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

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.


Securely chat with me on Keybase

Why you should vote me as witness

Sort:  

One of these days I'm going to learn how to code, and when I do I'll be reviewing all your great posts. A lot better than the wormhole of googling! This is really good content and I'm a fan of your work!

There are plenty of reasons why some solutions are better sometimes, but not always. And there are always many ways to do the same thing which are good enough. Short code is good not just because it sometimes executes faster but because you can read it faster. But if it's too obscure what's the point?

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

Absolutely. Thank-you :-)

The syntax for coding these days is just so complex.....for a numptie! Very difficult to simply sit down and teach yourself anymore. Again, perhaps my age and impatience but long gone are the days of picking up a book written by a bloke called Zaks and sitting down and just fannying around to solve a problem.

Of course, these days as computing hardware has become more powerful and complex, the code has had to keep up but sadly, some people couldn't lol ;-) Nathen, you ARE the weakest link!!

List comprehensions are cool, but putting everything in one line can make code hard to debug. Starting with a more step by step approach can help validate your algorithm. I notice one of the solutions checks for upper case, but the exercise specified it was all lower case.

I've been doing a course that uses functional languages where you are discouraged from using loops. That means lots of recursion which can be inefficient in some language, but can make for elegant code.

List comprehensions are the preferred way to iterate in Python. It is considered the "Pythonic" way to do things.

For the upper() you mean the RGB to hex challenge? I noticed the test output required upper case. The solution also showed uppercase output.

From experience I know that writing the shortest code can cause issues when you need to expand functionality later. Need to consider what is best for each case. Sometimes shorter code is not necessarily more efficient. It depends a lot on optimisations done in the compiler or interpreter and they are pretty clever these days.

The one line solution for finding vowels had

in 'aeiouAEIOU'

but that went beyond the requirement.

Oh I thought you meant one of my solutions so I looked through my older posts lol.

I agree about shorter code isn't better all the time I've mentioned it in most of my posts.

That's a skill I wish I had of learned when younger. You make it seem so simple yet its not. I worked for Infoxchange as a project manager for an app they were developing. I tried learning how to code but there are so many different scripts/languages/codes was too confusing.

It will be interesting to see what coding will be like once quantum computing is a thing.

It is never too late to learn and there are a lot of fantastic tools To learn out there. Have a look at Code Combat to start at the beginning. Use Code Wars to challenge yourself to grow.

Upvoted by GITPLAIT!

We have a curation trial on Hive.vote. you can earn a passive income by delegating to @gitplait
We share 80 % of the curation rewards with the delegators.

To delegate, use the links or adjust 10HIVE, 20HIVE, 50HIVE, 100HIVE, 200HIVE, 500HIVE, 1,000HIVE, 10,000HIVE, 100,000HIVE

Join the Community and chat with us on Discord let’s solve problems & build together.

@themarkymark, In my opinion Future is all about the Coding and Programming.

And looks like you are challenging yourself in a fun way and at the same time brushing your skills.

This is the Era Of Coding and many are exploring coding space and becoming Creators and Developers.

This Narration is appreciable aspect too.

Good wishes from my side and stay blessed.