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.
Yes, that way the original array that was passed in as a parameter is not modified.
This comment is hidden because it contains spoiler information about the solution
Performancewise would be "better" to use a StringBuilder or StringBuffer (using the .append() method) as it is faster to reallocate memory for a growing object, than creating a new object in each loop iteration.
Yes, it is working. But it also mutates the inputArray, which might be unwanted and is considered bad practice. It would be better to keep the function pure and return a new array instead.
it crashes when you run it against the sample tests (button
TEST
), and passes when you run it against the full tests (buttonATTEMPT
). The sample and full tests are independent (they are in separate files).Trainer
page; they are only here so that you can get an idea of how your solution is called and which kind of inputs you can expect.usually, the sample tests are also copied to the full tests suite, but this is just a convention and authors can write tests differently. In this particular kata, the author of the C version did not do this: the sample tests call the user function with read-only string literals (which crashes this solution), but the full tests call it with mutable strings, so this solution can get away with removing
const
Thank you for the good explanation. But my question was, why exactly this above solution does not execute properly? If I run it against the tests of this Kata, I get the SIGSEGV. Did some compiler change here?
this solution casts away
const
ness, which is mind-bogglingly bad and should never be done. it does that to pass the string to thestrtok()
function, which mutates the string it receives as argument.You probably tried to call this solution on a read-only string. on most platforms, string literals are placed into the
.rodata
(Read-Only Data) section of a program, which is where the compiler stores compile-time constants. attempting to modifying a.rodata
object at runtime results in a segmentation fault (the OS crashes your program)wrong:
correct:
This solution gives me a SIGSEGV (11) error. Same as my original solution did.
What is wrong?
What about these magic numbers 15 and 20?
I am not an experienced C programmer, but I think it limits the input to a number with 15 digits? I think this should be implemented more generic to be "Best Practice". It is not a big deal to get the number of digits of an int.
Am i wrong?