Step 1: Create a function called encode() to replace all the lowercase vowels in a given string with numbers according to the following pattern:
a -> 1
e -> 2
i -> 3
o -> 4
u -> 5
For example, encode("hello") would return "h2ll4". There is no need to worry about uppercase letters for this challenge.
Step 2: Now create a function called decode() to turn the numbers back into vowels according to the same pattern shown above. For the sake of simplicity, you can assume that any numbers passed into the function will correspond to vowels.
For example, decode("h3 th2r2") would return "hi there".
Tests
decode('h2ll4'), 'hello')
encode('This is an encoding test.')
encode('How are you today?')
encode('This is an encoding test.')
Good luck!
This challenge comes from yaphi1 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (10)
Let's do it in JS using neat little generic HOF...
Bit of a sanity check:
Edit:
inputString => inputString.replace(/./g, c => replacementMapping[c] ?? c)
is a less supported but better choice -- caniuse.com/#search=%3F%3FVery nice
Cheers
JavaScript
normal (aka filthy coercion)
sneaky
generic
Easy JS solution.
Here is my simple solution with Python:
A nice, simple, symmetric solution in Scala:
Tests:
A symmetric solution is probably better, but indexing letters but a number just called out to me for an array, even if the characters need to be converted to numbers to do so...
My Python solution:
After submitting this to Kata, I learned about the
str.translate()
method, which would make this much simpler 🧐My Javascript solution: