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.
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.
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.
The code for all solutions can be found here on my GitHub. I have broken down each solution here.
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.
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.
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 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.
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).
|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.
To conclude I have shown multiple ways to solve the problem and have showcased many features of Python.
Subscribe to this blog via RSS.
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)