Ad
  • Custom User Avatar
  • Custom User Avatar

    Initial solution also needs vector and cstddef headers.

  • Custom User Avatar

    Hi, I did a C++ translation. Let me know if it is ok! :D

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Thanks for the kata. I really like it. I was stuck on this for a while. If you're stuck, writing out the substrs for "baceb" on paper and look for patterns can help. It helped me.

  • Custom User Avatar
  • 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

    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

    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

    See issue above :)

  • Custom User Avatar

    Thank you - will do!

  • Custom User Avatar

    Hi, I made a C++ translation. The author hasn't been on since 2016. Could someone check/approve it? :)

  • Custom User Avatar

    I don't know C++ so have approved. Watch out for alerts - people will let you know if it doesnt work.

  • Loading more items...