create account

Code Wars Python Challenge: Vowel Count by themarkymark

View this thread on: hive.blogpeakd.comecency.com
· @themarkymark ·
$24.45
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)     
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 380 others
πŸ‘Ž  , , , , , ,
properties (23)
authorthemarkymark
permlinkcode-wars-python-challenge-vowel-count
categorycodewithmarky
json_metadata{"app":"peakd/2020.09.4","format":"markdown","tags":["codewithmarky","programming","development","codewars","challenge","palnet","neoxian","technology"],"users":["themarkymark"],"links":["https://keybase.io/officialmarky","/witness-category/@themarkymark/why-you-should-vote-for-themarkymark-as-witness","https://hivesigner.com/sign/account-witness-vote?witness=themarkymark&amp;approve=1"],"image":["https://files.peakd.com/file/peakd-hive/themarkymark/S50BYhed-image.png","https://i.imgur.com/w6kVqWS.png","https://i.imgur.com/pWq82CE.png","https://i.imgur.com/XNfgafP.png","https://i.imgur.com/1EOEbQp.png","https://i.imgur.com/qWnwBm9.png","https://i.imgur.com/ajSnti4.png","https://i.imgur.com/hnFpU9Q.png","https://i.imgur.com/sHQXj9Z.png","https://i.imgur.com/akHKajJ.png","https://i.imgur.com/yV5tcMy.png","https://i.imgur.com/f5Sidtq.png","https://steemitimages.com/DQmcWxV1dpA1eAtw2ipwZiWZkydyVNU5LaLa2Ak1GUnbGmS/The-Marky-Mark.png","https://steemitimages.com/DQmPkQhAQceC7aHr4Gy5GKv7LMoiy47P7PyaNCeZBHMpFke/white-bg-with-drop-shadow.gif"]}
created2020-09-14 07:54:51
last_update2020-09-14 07:54:51
depth0
children12
last_payout2020-09-21 07:54:51
cashout_time1969-12-31 23:59:59
total_payout_value15.018 HBD
curator_payout_value9.433 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,681
author_reputation1,776,036,775,447,145
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,618,448
net_rshares93,146,460,139,101
author_curate_reward""
vote details (451)
@chireerocks ·
@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.
properties (22)
authorchireerocks
permlinkqgo217
categorycodewithmarky
json_metadata{"users":["themarkymark"],"app":"hiveblog/0.1"}
created2020-09-14 20:53:36
last_update2020-09-14 20:53:36
depth1
children0
last_payout2020-09-21 20:53:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length393
author_reputation327,821,690,945,691
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,628,923
net_rshares0
@cryptoknight12 ·
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!
πŸ‘  ,
properties (23)
authorcryptoknight12
permlinkqgnk6k
categorycodewithmarky
json_metadata{"app":"hiveblog/0.1"}
created2020-09-14 14:28:00
last_update2020-09-14 14:28:00
depth1
children1
last_payout2020-09-21 14:28:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length203
author_reputation77,839,196,919,506
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,623,268
net_rshares2,300,012,454
author_curate_reward""
vote details (2)
@themarkymark ·
Check out https://codecombat.com/
πŸ‘  
πŸ‘Ž  
properties (23)
authorthemarkymark
permlinkre-cryptoknight12-qgnmjv
categorycodewithmarky
json_metadata{"tags":["codewithmarky"],"app":"peakd/2020.09.4"}
created2020-09-14 15:19:06
last_update2020-09-14 15:19:06
depth2
children0
last_payout2020-09-21 15:19:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length33
author_reputation1,776,036,775,447,145
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,624,004
net_rshares-725,896,937,390
author_curate_reward""
vote details (2)
@gitplait ·
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.
properties (22)
authorgitplait
permlinkqgnxob
categorycodewithmarky
json_metadata{"users":["gitplait"],"app":"hiveblog/0.1"}
created2020-09-14 19:19:39
last_update2020-09-14 19:19:39
depth1
children0
last_payout2020-09-21 19:19:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length387
author_reputation911,220,543,569
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,627,577
net_rshares0
@helcim ·
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?
properties (22)
authorhelcim
permlinkre-themarkymark-qgn2ol
categorycodewithmarky
json_metadata{"tags":["codewithmarky"],"app":"peakd/2020.09.4"}
created2020-09-14 08:09:57
last_update2020-09-14 08:09:57
depth1
children0
last_payout2020-09-21 08:09:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length302
author_reputation210,556,141,380,886
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,618,638
net_rshares0
@melbourneswest ·
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. 
properties (22)
authormelbourneswest
permlinkre-themarkymark-qgn5oy
categorycodewithmarky
json_metadata{"tags":["codewithmarky"],"app":"peakd/2020.09.4"}
created2020-09-14 09:15:03
last_update2020-09-14 09:15:03
depth1
children1
last_payout2020-09-21 09:15:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length368
author_reputation720,388,722,725,951
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,619,251
net_rshares0
@themarkymark ·
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. 
πŸ‘  
πŸ‘Ž  
properties (23)
authorthemarkymark
permlinkre-melbourneswest-qgn7t4
categorycodewithmarky
json_metadata{"tags":["codewithmarky"],"app":"peakd/2020.09.4"}
created2020-09-14 10:00:39
last_update2020-09-14 10:00:39
depth2
children0
last_payout2020-09-21 10:00:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length190
author_reputation1,776,036,775,447,145
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,619,745
net_rshares-725,992,841,302
author_curate_reward""
vote details (2)
@nathen007 · (edited)
>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!!
properties (22)
authornathen007
permlinkqgn30w
categorycodewithmarky
json_metadata{"app":"hiveblog/0.1"}
created2020-09-14 08:17:21
last_update2020-09-14 08:31:24
depth1
children0
last_payout2020-09-21 08:17:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length606
author_reputation260,270,214,309,122
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,618,736
net_rshares0
@steevc ·
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.
properties (22)
authorsteevc
permlinkre-themarkymark-qgn3jr
categorycodewithmarky
json_metadata{"tags":["codewithmarky"],"app":"peakd/2020.09.4"}
created2020-09-14 08:28:39
last_update2020-09-14 08:28:39
depth1
children3
last_payout2020-09-21 08:28:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length478
author_reputation1,382,384,998,017,387
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,618,849
net_rshares0
@themarkymark · (edited)
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.  
πŸ‘  
πŸ‘Ž  
properties (23)
authorthemarkymark
permlinkre-steevc-qgn456
categorycodewithmarky
json_metadata{"tags":["codewithmarky"],"app":"peakd/2020.09.4"}
created2020-09-14 08:41:30
last_update2020-09-14 08:43:12
depth2
children2
last_payout2020-09-21 08:41:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length260
author_reputation1,776,036,775,447,145
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,618,961
net_rshares-155,229,000,897
author_curate_reward""
vote details (2)
@steevc ·
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.
properties (22)
authorsteevc
permlinkre-themarkymark-qgn56b
categorycodewithmarky
json_metadata{"tags":["codewithmarky"],"app":"peakd/2020.09.4"}
created2020-09-14 09:03:48
last_update2020-09-14 09:03:48
depth3
children1
last_payout2020-09-21 09:03:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length430
author_reputation1,382,384,998,017,387
root_title"Code Wars Python Challenge: Vowel Count"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,619,171
net_rshares0