Coding Challenge: Rotate your vowels

Coding Challenge: Rotate your vowels

4 Mar, 2017. | By: AceLewis


This is the first coding challenge snippet that I am going to share on my website.

The coding challenge here is to rotate the vowels in a given string multiple times and then print the rotated strings.

Sections

  • What are coding challenges?
  • Rotate your vowels
  • Solutions
  • Table of performance
  • Conclusion

What are coding challenges?

Coding challenges are little challenges that I am going to use to highlight features of programming languages. The main language I will start these challenges in will be Python however in the future I may also use other languages. I will in some cases show multiple ways of doing each coding challenge.

Rotate your vowels

First off the challenge name is a joke based of a silly old music video Rotate Your Owl which in turn is based off a video showing the head stability of owls. So the challenge I found is from Writing Impositions in Matlab. The challenge is simple for any input string change all a’s to e’s, e’s to i’s and so on, effectively rotating the vowels in the string. Also you should apply this rotation multiple times printing it out each time.

Solutions

The code for all solutions can be found here on my GitHub. I have broken down each solution here.

regex

So we want to be able to swap only the vowels, we can use regex to only select vowels then input the vowel matched into a lambda function to find the corresponding substituted vowel.

The pros of this method is it shows regex a good way to match strings and it shows that functions can be used in regex in Python. Functions in regex are very useful in certain circumstances. It also shows lambda functions in Python, although the def style functions are in most cases preferred. The con is that using regex is overkill on such a simple problem. Code here.

The indexing method

This method relies on the .find method returning -1 if the string is not found. -1 is also the way to index the last thing in an string, so by putting the character on the end of the string means that if the character is not found by .find it will default to the character. Code here.

The dictionary method

This is solution relies on using the vowels as key, values. The dictionary is built via using zip to pair up the characters in the two strings. Also the .get on the dictionary uses a default value if the key is not found. Code here.

The string translate method

The last method is using the string translate function, this function is built to do these translations so is the fastest. Code here.

I also made code that can be used to easily do this translation on other letters and by different amounts of shift. Code here.

Table of performance

Performance is not everything, readability counts however I have also compared the execution time of these pieces of code to translate the string 100,000 times (without printing to the console).

Method Time
regex 1.07 s
indexing 2.22 s
dictionary 1.10 s
string translate 0.39 s

I knew the string translate method would be fastest however I am surprised that the regex solution was that fast, I thought it would be slower than the other methods. I also thought that the indexing method would be the second fastest as it only used basic inbuilts however it was twice as slow as the other methods, however looking in hindsight it makes sense because the dictionary method builds the dictionary once whilst in the indexing method the string used for indexing is built on repeatedly.

Conclusion

To conclude I have shown multiple ways to solve the problem and have showcased many features of Python.

Subscribe

Subscribe to this blog via RSS.

Recent Blog Posts

Recent Posts

All Posts and Blog Posts

Click here to view all blog and other post I have written.

Popular Tags

Adblock (2) Ethics (1) Adverts (2) Images (1) Formats (1) Zeronet (2) P2p (3) Dns (1) Zookos triangle (1) Data (2) Maths (1) Bitcoin (1) Blockchain (1) Svg (1) Python (2) Regex (2) Coding (2) Coding challenge (2) Repl.it (1) Emoji (1)

About me

I am interested in Physics, Maths, Programming and also play a few games in my spare time. I have an interest in data and have made webscrapers to gather and process information, I am currently looking into nice ways to display information easily in infographics using Python. I can code in Matlab, Python and have also made a Steam trading bot in Node.JS.

Social Links