Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
You're right! I came across this issue when I tried to use my isPrime implementation in a Kata.
The following test will fail:
Assert::That(false, Equals(PrimeNumber::isPrime(4)));
You should replace:
if (num % 2 == 0) return true;
with:
if (num == 2) return true;
if (num % 2 == 0) return false;
This comment is hidden because it contains spoiler information about the solution
Actually, I forgot you were using a regex, so the max possible value of
s
is 99, which the if statement will handle just fine. My bad.Good point! The thought crossed my mind while solving, but this passed the tests, so I kept it as-is.
This comment is hidden because it contains spoiler information about the solution
IMO, casts are a red flag for bad design (that's also why the syntax is so ugly), so if you can get rid of one, that's a good idea. It has no runtime impact, indeed.
I am also not 100% certain about how it works, just that it makes code a little more readable as the functor is defined where it's used, instead of the reader having to look it up.
But there are really minor points.
Thanks for you comment! I used static cast to explicitly show that it will be int. As far as I know it has no impact during runtime, i.e. check performed only during compilation.
As for lambda, I use it in C# a lot, but C++ is a mishmash on its own, so this was again for clarity reasons. Besides I don't have a foggiest idea how lamda works in C++ "under the hood" :)
That static cast to int is not required due to the C++ integer promition rules: every arithmetic operation is converted to at least integer size.
Also, have you considered using a lambda instead of the acumCubes function? Worthwile.
Other than that: my compliments that you are using algorithms as much as possible! That makes this the first solution I've read that's actually easy to read and comprehend.
This comment is hidden because it contains spoiler information about the solution