Intro π
Problem solving is an important skill, for your career and your life in general.
That's why I take interesting katas of all levels, customize them and explain how to solve them.
Understanding the Exerciseβ
First, we need to understand the exercise!
If you don't understand it, you can't solve it!.
My personal method:
- Input: What do I put in?
- Output: What do I want to get out?
Today's exercise
Today, another 7 kyu
kata,
meaning we slightly increase the difficulty.
Source: Codewars
Write a function higherVersion
, that accepts two parameters: version1
and version2
.
Given two strings, e.g. "1.2.3"
and "1.2.0"
, return if the first string is higher than the second, e.g. true
.
There are no leading zeros, e.g. 100.020.003
is given as 100.20.3
.
Input: two strings.
Output: a boolean.
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:
- Check if the current number of the first string is higher, lower or equal than/to the first number of the second string
- If higher, then return true
- If lower, then return false
- If equal, go to the next number of both strings and start from step 1
Example:
- Input:
"1.2.3", "1.2.0"
- Check if the current number of the first string is higher, lower or equal than/to the first number of the second string:
1
and1
=>equal
- If equal, go to the next number of both strings and start from step 1
- Check if the second number of the first string is higher, lower or equal than/to the second number of the second string:
2
and2
=>equal
- If equal, go to the next number of both strings and start from step 1
- Check if the third number of the first string is higher, lower or equal than/to the third number of the second string:
3
and0
=>higher
- If higher, then return
true
- Output:
true
β
Implementation β
function higherVersion(version1, version2) {
// split the strings into numbers
const split1 = version1.split(".").map((s) => Number(s));
const split2 = version2.split(".").map((s) => Number(s));
let result = null;
for (let i = 0; i < split1.length; i++) {
if (split1[i] > split2[i]) {
// is higher, so break out of the whole loop
result = true;
break;
} else if (split1[i] < split2[i]) {
// is smaller, so break out of the whole loop
result = false;
break;
} else {
// is equal, so check the next number
result = false;
}
}
return result;
}
Result
console.log(higherVersion("1.2.3", "1.2.0"));
// true β
console.log(higherVersion("9", "10"));
// false β
Playground β½
You can play around with the code here
Next Part β‘οΈ
Great work!
We learned how to use split
, map
, for
, break
.
I hope you can use your new learnings to solve problems more easily!
Next time, we'll solve another interesting 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 β
- How often do you do katas?
- Which implementation do you like more? Why?
- Any alternative solution?
Top comments (9)
A little abstraction goes a long way to improving this answer.
I appreciate the desire for a single exit point, but seems unnecessary in this situation.
Here's a version that's much easier to read based on the CodeWars discussion with the additional feature of handling mismatched lengths:
But this is faster and simpler right?
With the assumption that the two versions are the same length and have no leading zeros, yes. I'd probably recommend
.replaceAll('.', '')
oversplit('.').join('')
.let stringOne = Number(v1.split(".").join(""))
let stringTwo = Number(v2.split(".").join(""))
Adjust the partition if you want to exclude the equal case.
would
Number(version1.split('.').join('')) > Number(version2.split('.').join(''))
be enough or are there cases where it doesn't work?The nice thing is that it should also allow you to sort an arbitrary number of versions.