Intro 🌐
I take interesting katas of all levels and explain how to solve them.
Problem solving is an important skill, for your career and your life in general.
You'd better learn to solve problems!
Source
I take the ideas for the katas from different sources and re-write them.
Today's source: Codewars
Understanding the Exercise ❗
First, we need to understand the exercise!
This is a crucial part of (software) engineering.
Go over the exercise explanation again until you understand it 100%.
Do NOT try to save time here.
My method to do this:
- Input: What do I put in?
- Output: What do I want to get out?
Today's exercise
Character recognition software is widely used to digitize printed texts. When documents are digitized character recognition softwares often make mistakes. Your task is to correct the errors in the digitized text. You only have to handle the following mistakes:
-
S
is misinterpreted as5
-
O
is misinterpreted as0
-
I
is misinterpreted as1
Write a function correctMistakes
, that accepts one parameter: inputString
, a valid string.
The function should return a string. The string contains the inputString
with the corrections.
Input: a string.
Output: a string with the corrections.
Thinking about the Solution 💭
I think I understand the exercise (= what I put into the function and what I want to get out of it).
Now, I need the specific steps to get from input to output.
I try to do this in small baby steps.
- loop over every character
- if the character is misinterpreted (see above), correct it
- return the result (the corrected string)
Example:
- Input:
"0h!"
- Iteration 1:
"0h!"
// next character is0
=> it is in the list of corrections => correct it (0
becomesO
) - Iteration 2:
"Oh!"
// next character ish
=> it is NOT in the list of corrections => return it unchanged - Iteration 3:
"Oh!"
// next character is!
=> it is NOT in the list of corrections => return it unchanged - Output:
"Oh!"
// return the string
Implementation (functional) ⛑
function correctMistakes(inputString) {
// organize the corrections in a human-readable object/map
const corrections = {
"5": "S",
"0": "O",
"1": "I",
};
return (
inputString
// split the string into an array of characters
.split("")
// check if the current character is in the corrections object
// if it is, correct it, else return it unchanged
.map((char) =>
corrections.hasOwnProperty(char) ? corrections[char] : char
)
// join the array of characters to a string
.join("")
);
}
Result
console.log(correctMistakes("0h!"));
// "Oh!"
console.log(correctMistakes("1 l0ve m1ss1ss1pp1🧶!"));
// "I lOve mIssIssIppI🧶!"
Implementation (regex) ⛑
function correctMistakes(inputString) {
// organize the corrections in a human-readable object/map
const corrections = {
"5": "S",
"0": "O",
"1": "I",
};
// use a regex (//)
// search for 5 or 0 or 1 ([501])
// do this for the whole input (g)
// if it finds a mistake, run the function that takes the mistake and replaces it with the correct char from the corrections object
return inputString.replace(/[501]/g, (char) => corrections[char]);
}
Result
console.log(correctMistakes("0h!"));
// "Oh!"
console.log(correctMistakes("1 l0ve m1ss1ss1pp1🧶!"));
// "I lOve mIssIssIppI🧶!"
Playground ⚽
You can play around with the code here
Next Part ➡️
Great work, mate!
Next time, we'll solve the next kata. Stay tuned!
If I should solve a specific kata, shoot me a message here.
If you want to read my latest stuff, get in touch with me!
Further Reading 📖
Questions ❔
- Do you like to solve katas?
- Which implementation do you like more? Why?
- Any alternative solution?
- Do you regularly use regex?
Top comments (0)