6 kyu

Split Without Loss

Description
Loading description...
Fundamentals
  • Please sign in or sign up to leave a comment.
  • ahmet_popaj Avatar

    Fancy kata, congratulations.

  • saudiGuy Avatar

    python new test framework is required. updated in this fork

  • DelroyGayle Avatar

    Greetings

    I have written two different solutions which both use a loop. No recursion. Both run fine in Visual Studio but both crash with memory problems in Codewars

    FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

    I have tried both 'resetting' and changing the Node versions. I get the same errors

    How do I proceed?

    This is my output from Visual Code:

    console.log(splitWithoutLoss("hello world!", " |"), ["hello ", "world!"]); console.log(splitWithoutLoss("hello world!", "o|rl"), ["hello wo", "rld!"]); console.log(splitWithoutLoss("hello world!", "ello| "), ["hello", " world!"]); console.log(splitWithoutLoss("hello world!", "hello wo|rld!"), [ "hello wo", "rld!", ]); console.log(splitWithoutLoss("hello world!", "h|ello world!"), [ "h", "ello world!", ]);

    console.log(splitWithoutLoss("hello world! hello world!", " |"), [ "hello ", "world! ", "hello ", "world!", ]);

    console.log(splitWithoutLoss("hello world! hello world!", "o|rl"), [ "hello wo", "rld! hello wo", "rld!", ]);

    console.log(splitWithoutLoss("hello world! hello world!", "ello| "), [ "hello", " world! hello", " world!", ]);

    console.log(splitWithoutLoss("hello world! hello world!", "hello wo|rld!"), [ "hello wo", "rld! hello wo", "rld!", ]);

    console.log(splitWithoutLoss("hello hello hello", " | "), [ "hello ", " hello ", " hello", ]);

    Which Produced:

    [ 'hello ', 'world!' ] [ 'hello ', 'world!' ] [ 'hello wo', 'rld!' ] [ 'hello wo', 'rld!' ] [ 'hello', ' world!' ] [ 'hello', ' world!' ] [ 'hello wo', 'rld!' ] [ 'hello wo', 'rld!' ] [ 'h', 'ello world!' ] [ 'h', 'ello world!' ]

    [ 'hello ', 'world! ', 'hello ', 'world!' ] [ 'hello ', 'world! ', 'hello ', 'world!' ]

    [ 'hello wo', 'rld! hello wo', 'rld!' ] [ 'hello wo', 'rld! hello wo', 'rld!' ]

    [ 'hello', ' world! hello', ' world!' ] [ 'hello', ' world! hello', ' world!' ]

    [ 'hello wo', 'rld! hello wo', 'rld!' ] [ 'hello wo', 'rld! hello wo', 'rld!' ]

    [ 'hello ', ' hello ', ' hello' ] [ 'hello ', ' hello ', ' hello' ]

    • akar-0 Avatar

      FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

      This means you are using too much memory (you are probably building a gigantic array or something like this, or a part of your code keeps pushing value into something and never stops), and it is not a kata issue.

      Advice: your while loop is probably the culprit. You are probably not testing your code with the same input in VSC as here. Please refer to the documentation: https://docs.codewars.com/training/troubleshooting/

      Issue marked resolved by akar-0 3 years ago
    • DelroyGayle Avatar

      Thank you, akar-0

      But as you can see I posted exactly the input so that you can see what I am using! All I did was change the 'test.assert...' string to 'console.log' That's it! And ran both programs which displayed the answers straightaway As you can see

      I would gladly send you the source code of my solution so that you can see what I mean, if that helps!

    • akar-0 Avatar

      I solved the kata in JavaScript, so I am able to see your code. Your code passes the first sample tests, but fails in some of the series

        it("Special cases (splits at start/end and overlapping splits)", function() {
      

      for example

          Test.assertSimilar(splitWithoutLoss("aaaa", "|aa"), 
                             ["aa", "aa"])
      

      I don't think it works fine in VSC either.

    • DelroyGayle Avatar

      Aha! I understand now. Thank you

  • JohanWiltink Avatar

    What is the expected output for split("aaa","|aa"), and why?

    I think it should be ["a","aa"], not ["aa","a"], and definitely not ["aaa"].

    • narayanswa30663 Avatar

      It is ["aaa"].

      Why?

      The first point "|aa" is matched in the string ("aaa") is at index 0.

       aaa
      |aa # one more |aa will not be matched, and there are no overlaps
      # becomes ["", "aaa"]
      # becomes ["aaa"] (no empty strings)
      

      I agree that it seems weird. But there are no overlaps, and the first match is the one that counts. Hope that clarifies it. Thanks!

    • JohanWiltink Avatar

      Weird, but correct. Thank you!

      I have some solutions to update. The random tests don't always catch this (but sometimes do); you might want to add a static test with an odd number of as, and maybe clarify this in the description.

      Question marked resolved by JohanWiltink 9 years ago
    • narayanswa30663 Avatar

      Thanks for the input. Will do!

    • narayanswa30663 Avatar

      And... done! Added five static tests to both examples and full suite for all languages. Hope it's ok!

    • JohanWiltink Avatar

      Also, I figured out where I was thinking differently. I was not matching if left would be an empty string. However, you have to match but throw away the result. (I think my reasoning is valid as well. It's just not what you specified. :D )

  • kwilson541 Avatar

    Your instructions state that we should remove any empty strings from our output, which I have done. However, around 20-30 of the random tests expect the output to include empty strings, therefore it is not possible to pass all tests.

    For example:

    Expected: ["y", "", " vnsha jol ecz dqy", "", " vnsha jol ecz dq"], instead got: ["y", " vnsha jol ecz dqy", " vnsha jol ecz dq"]

    • narayanswa30663 Avatar

      This comment has been hidden.

    • kwilson541 Avatar

      Hi! Thanks for the reply. I'm working in Ruby. Here are some of the tests that fail in the Random cases (note that the expected output includes empty strings ""):

      str = "r ij tcnmr ij tcnm" and split_p = "|j t" Expected: ["r i", "", "j tcnmr i", "", "j tcnm"], instead got: ["r i", "j tcnmr i", "j tcnm"]

      str = "u xgowdny z he u xgowdny z he " and split_p = "|ow" Expected: ["u xg", "", "owdny z he u xg", "", "owdny z he "], instead got: ["u xg", "owdny z he u xg", "owdny z he "]

      str = "xdvp iqyntwgcbxdvp iqyntwgcb" and split_p = "|p " Expected: ["xdv", "", "p iqyntwgcbxdv", "", "p iqyntwgcb"], instead got: ["xdv", "p iqyntwgcbxdv", "p iqyntwgcb"]

    • narayanswa30663 Avatar

      That is very strange. The random test function returns the valid ["r i", "j tcnmr i", "j tcnm"] for your first example, and don't have any empty strings for the rest. Could you please paste your code and mark it as a spoiler? Thanks!

    • kwilson541 Avatar

      This comment has been hidden.

    • narayanswa30663 Avatar

      This comment has been hidden.

    • kwilson541 Avatar

      This comment has been hidden.

    • narayanswa30663 Avatar

      This comment has been hidden.

    • narayanswa30663 Avatar

      I hope you managed to get it fixed! :)

    • kwilson541 Avatar

      It worked! Thank you so much for taking the time to help out. I will be sure to leave feedback on your kata. Despite my coding mistakes I enjoyed it :)

  • Unnamed Avatar

    Overlapping occurrences ("aaaa", "|aa") would be a nice edge case to have in the tests because it ruins some nice and simple solutions.

    • narayanswa30663 Avatar

      I like it! Unfortunately, I feel like too many solutions have been submitted, and invalidating them would not really be fair. Sorry about it.

      Suggestion marked resolved by narayanswa30663 9 years ago
    • Unnamed Avatar

      It's beta, invalidating everything is fair, 9 solutions is not many at all, correct (according to the current description) solutions won't be invalidated. If you still don't want to test on this kind of delimiters then it should be stated in the description and the random tests should be modified because they do generate such delimiters sometimes.

    • narayanswa30663 Avatar

      Sorry. In the example that you provided, what is the current answer and the answer that you expect?

    • Unnamed Avatar

      It should be ["aa", "aa"] while with the first solution that came to my mind it was ["aa", "aa", "aa"] and I didn't immediately notice that the solution was incorrect.

    • narayanswa30663 Avatar

      Clarified in the description. Hope it's ok.

    • Unnamed Avatar

      It was already clear, the problem is that there are no static tests for that, so some invalid solutions may be submitted.

    • narayanswa30663 Avatar

      I have added 5 static tests for that. Your 'incorrect' solution has been invalidated. Thanks for your help!

  • ZozoFouchtra Avatar

    This comment has been hidden.