• Custom User Avatar

    @Chrono79 - Thanks for the tip, I'll definitely remember that after this!

    @Exterminator - No problem, thank you for the learning experience. ^.^

  • Custom User Avatar

    Are you mutating the input somehow?
    71 * 71 = 5041

  • Custom User Avatar

    Can i get some help please.
    I fail on the random tests and fail to understand why.
    Test cases indicate that this should be false. But my code outputs true.

    array (
      9 => 82,
      2 => 76,
      10 => 75,
      1 => 71,
      16 => 68,
      4 => 64,
      7 => 62,
      15 => 52,
      11 => 46,
      6 => 44,
      5 => 38,
      13 => 25,
      0 => 23,
      8 => 23,
      12 => 21,
      14 => 16,
      3 => 8,
    )
    array (
      9 => 6724,
      2 => 5776,
      10 => 5625,
      1 => 5042,
      16 => 4624,
      4 => 4096,
      7 => 3844,
      15 => 2704,
      11 => 2116,
      6 => 1936,
      5 => 1444,
      13 => 625,
      0 => 529,
      8 => 529,
      12 => 441,
      14 => 256,
      3 => 64,
    )
    

    Failed asserting that true matches expected false.

  • Custom User Avatar

    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 ;)

  • Custom User Avatar

    Hey all,

    I'll have to agree with Chrono.
    Someone else may have the same problem as i did so this is something best fixed in the original solution.

    Thank you guys very much for the support. With copying the input i pass all tests without any issue.

    P.S.
    Also deleting the content of my comment with the solution so someone does not stumble on it.

  • Custom User Avatar

    @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.

  • Custom User Avatar

    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.

  • Custom User Avatar

    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?

  • Custom User Avatar

    -- This comment originally contained a solution to this task, now removed so no one stumbles on it by accident.

  • Custom User Avatar

    Hey @TheDrw,

    I pass all test cases like so:

    Time: 11ms Passed: 2 Failed: 1
    Test Results:
    Tests
    Example_Tests
    Test Passed
    Edge_Cases
    Test Passed
    Random_Tests
    Expected: equal to ttteyefph
    Actual: rutkkpcstndbatbe

    What wierd i can see is that from time to time i get these really really long strings in the expcted. Like:

    scapabskcecebiztbaapsnrrpghd - 269
    bksebpeeoppohzteao - 202
    cte - 28
    sstatlaraiouteimo - 218
    uoasfr - 80
    etiaensra - 92
    ks - 30
    tt - 40
    nrei - 46
    phpor - 73
    dyk - 40
    ekaiuuonhaaka - 119
    er - 23
    yi - 34
    at - 21
    Expected: equal to scapabskcecebiztbaapsnrrpghdbksebpeeoppohzteaoctesstatlaraiouteimouoasfretiaensra
    Actual: scapabskcecebiztbaapsnrrpghd

    In a second reply i will give my solution and mark it as a spoiler. If other people have solved this than the problem must be with me.

  • Custom User Avatar

    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.

  • Custom User Avatar

    oops, sorry, I'll check it out right now!

  • Custom User Avatar

    @TheDrw. Ping!

  • Custom User Avatar

    Hello, you're surely doing it in C. Well, you are asked to return a string (char*), so you need to allocate it with malloc (if you use a char[], its memory will be theorically released when you leave the function).

    The error you have is that I retrieve your answer, test it and then free it's content. Which is not possible because you didn't return a malloced string.
    If your solution actually works aside from my free() (and C tolerance on segfaults on freshly released memory), please comment your solution between triple ` and as a hidden comment, and I'll fix my testing accordingly!

  • Custom User Avatar

    Hi, can someone tell me what does this mean? "*** Error in `/home/codewarrior/solution': free(): invalid pointer: 0x00007ffe092588b8 *** "
    Thanks for help.

  • Loading more items...