NO! It's a fundamental basic that you must NOT change the input (unless stated otherwise), and even more when it's given to you as a const!!! It's a good thing that (thanks serendipity) TheDrw's testcases actually test that. And I think it would send a very bad signal to change the testcases to accept such answers. It would be good to update the Expected: equal to pkhh of the first test to whatever is really the longest string in the verification test, though. And maybe add If that string wasn't the best in the string you received, did you somehow alter your input? or such, in the Error output.
Btw @Exterminator you can also hide your solution by checking the Mark as having spoiler content checkbox before posting ;)
@TheDrw it'll save you a lot of headaches if you obtain expected before calling the user's function (if your code doesn't mutate the input that is). Another way of doing it is passing a copy of the input to the user's function. Or you could add a note about not mutating the input.
All right, so I think I figured out what's wrong. I believe your code isn't wrong. But, there's an odd problem with it. When I do the random tests, I'll run your function, then after I'll run my function and it checks if we come up with the same answer. The problem is, when it runs your function, it is modifying my test string. I am not sure how, but in your code, you check for a space or " ". But for some reason, for each string with multiple spaces, it will delete single spaces and mash the strings together when there's multiple spaces and make it the new test string.
For example, if the original string was ( str = "food pizza spinach codewars" ). Your solution will get the correct word out, whichever one that will be. Then after that, I'll run my function to go through the string. And because your code somehow modified my string, my function will use ( str = "foodpizza spinachcodewars" ) and my function will pick the bigger one from this new modified string. You can actually see the modification if you put this in your code
std::cout << "orginal: " << str << "\n\n"; // prints string
char *pch; // put it above this line
std::cout << "now : " << str << \n\n"; // prints string
return ostr; // put it above this line
Also #include if you haven't. I honestly don't know why it does that, my guess it has something to do with your pointers modifying my string somehow even if it is const. ¯\(ツ)/¯
It looks like you are approaching the problem more like it is in C. I don't really know much about some details of C, so I am not sure what to suggest. Have you tried doing the C version of this problem?
All right, I checked it out real quick, and I am not sure what it could be. I ran the input you sent, and I get "inrpahbtnftueiearpnkeras" as the solution as well. I also get the same scores as you on all the words so that might be a good sign. Just wondering, do you also pass the edge test cases? I didn't print out each case out for the random tests, so when you're printing them, are you printing "\n\n" so strings don't overlap? It seems odd "pkhh" is showing as the expected answer since I am not getting it. My best guess it could be the next random case. I am not really sure.
@Chrono79 - Thanks for the tip, I'll definitely remember that after this!
@Exterminator - No problem, thank you for the learning experience. ^.^
Are you mutating the input somehow?
71 * 71 = 5041
NO! It's a fundamental basic that you must NOT change the input (unless stated otherwise), and even more when it's given to you as a
const
!!! It's a good thing that (thanks serendipity) TheDrw's testcases actually test that. And I think it would send a very bad signal to change the testcases to accept such answers. It would be good to update theExpected: equal to pkhh
of the first test to whatever is really the longest string in the verification test, though. And maybe addIf that string wasn't the best in the string you received, did you somehow alter your input?
or such, in the Error output.Btw @Exterminator you can also hide your solution by checking the
Mark as having spoiler content
checkbox before posting ;)@TheDrw it'll save you a lot of headaches if you obtain expected before calling the user's function (if your code doesn't mutate the input that is). Another way of doing it is passing a copy of the input to the user's function. Or you could add a note about not mutating the input.
I found out instead of using str directly. You can make a copy of it and use that. So you would do something like,
std::string strcopy = str;
char cstr = const_cast<char>(strcopy.c_str()); // changed str.c_str() to strcopy.c_str()
Or you can delete the '&' from the function instead of making a copy. Now it should pass, but I am not sure if it is the best approach.
All right, so I think I figured out what's wrong. I believe your code isn't wrong. But, there's an odd problem with it. When I do the random tests, I'll run your function, then after I'll run my function and it checks if we come up with the same answer. The problem is, when it runs your function, it is modifying my test string. I am not sure how, but in your code, you check for a space or " ". But for some reason, for each string with multiple spaces, it will delete single spaces and mash the strings together when there's multiple spaces and make it the new test string.
For example, if the original string was ( str = "food pizza spinach codewars" ). Your solution will get the correct word out, whichever one that will be. Then after that, I'll run my function to go through the string. And because your code somehow modified my string, my function will use ( str = "foodpizza spinachcodewars" ) and my function will pick the bigger one from this new modified string. You can actually see the modification if you put this in your code
std::cout << "orginal: " << str << "\n\n"; // prints string
char *pch; // put it above this line
std::cout << "now : " << str << \n\n"; // prints string
return ostr; // put it above this line
Also #include if you haven't. I honestly don't know why it does that, my guess it has something to do with your pointers modifying my string somehow even if it is const. ¯\(ツ)/¯
It looks like you are approaching the problem more like it is in C. I don't really know much about some details of C, so I am not sure what to suggest. Have you tried doing the C version of this problem?
All right, I checked it out real quick, and I am not sure what it could be. I ran the input you sent, and I get "inrpahbtnftueiearpnkeras" as the solution as well. I also get the same scores as you on all the words so that might be a good sign. Just wondering, do you also pass the edge test cases? I didn't print out each case out for the random tests, so when you're printing them, are you printing "\n\n" so strings don't overlap? It seems odd "pkhh" is showing as the expected answer since I am not getting it. My best guess it could be the next random case. I am not really sure.
oops, sorry, I'll check it out right now!
@TheDrw could you check it out?