The Full Counting Sort

  • + 1 comment

    thank you very much I submitted the change as you suggested but the testcase #5 failed again :( what else would it be? Have you test the result in your local machine?

    • + 1 comment

      I submitted the change and it really works for me :)
      Here is the code:

      function processData(input) {
          input = input.split('\n');
          //console.log(input);
          var n = parseInt(input[0]);
          var numbers = [];
          for (var i = 0; i < 100; i++) {
              numbers[i] = [];
          }
          var half = n / 2;
          for (var i = 1; i <= n; i++) {
              //console.log(input)
              var obj = input[i].split(' ');
              //console.log(obj);
              var index = parseInt(obj[0]);
              var s = obj[1];
              if (i <= half) {
                  s = '-';
              }
              numbers[index].push(s);
          }
          //var result = '';
          for (var i = 0; i < 100; i++) {
              for (var j = 0; j < numbers[i].length; j++) {
                  process.stdout.write(numbers[i][j] + ' ');
                  //result += numbers[i][j] + ' ';
              }
          }
          //console.log(result);
      } 
      

      Since the size of testcase#5 is too large, I still can't figure out the reason why your string operation(result += numbers[i][j] + ' ') is wrong. But if u puts process.stdout.write outside the for loop i.e. use process.stdout.write to print result directly, it will be WA. Hence, it it the reason why I think the string operation has problem.

      • + 1 comment

        Wow! Kakason, thank you very much for your time and debugging my code. It passed all the test cases! Bravo! But now, I have several questions and I hope this can help others with the same problem: Before submitting the last working version (yours), I attempted another following the recomendations you gave me but it didn't pass test case #5, here is the code:

        function processData(input) {
            input = input.trim().split('\n');
            var n = parseInt(input[0]);
            var numbers = [];
            for (var i = 0; i < 100; i++) {
                numbers[i] = [];
            }
            var half = n / 2;
            for (i = 1; i <= n; i += 1) {
                var obj = input[i].trim().split(' ');
                var number = parseInt(obj[0]);
                var string = obj[1];
                if (i <= half) {
                    string = '-';
                }
                numbers[number].push(string);
            }
            for (i = 0; i < 100; i += 1) {
                var len = numbers[i].length;
                for (var j = 0; j < len; j += 1) {
                    process.stdout.write(numbers[i][j] + ' ');
                }
            }
        }
        

        As you can see, I didn't concatenate the answer and I used the process.stdout.write instead of console, and that would be the first question, why? is there some benefit using process.stout?

        Second, if you agree, that code should work too, but as I said, it didn't, so? what would it be the real problem? There are several diferences between your code and mine, 1. you're not using the trim() function, and I've gotten used to use it because there where some troubles with whitespaces in some exercises. 2. the ++ operator. 3. the name of my variable 'string' and 4.pre calculating the numbers[i].length. Which of all of these differences is causing the real problem? I'm going to test all of them and see what are the results.

        Thanks again Kakason.

        • + 2 comments

          After several tests and 11 submissions, some of them failed and some other passed. This was my first original code with the Kakason's upgrades that passes all test cases.

          function processData(input) {
              input = input.trim().split('\n');
              var n = parseInt(input[0]);
              var numbers = [];
              for (var i = 0; i < 100; i++) {
                  numbers[i] = [];
              }
              var half = n / 2;
              for (i = 1; i <= n; i += 1) {
                  var obj = input[i].split(' ');
                  numbers[parseInt(obj[0])].push(i <= half ? '-' : obj[1]);
              }
              for (i = 0; i < numbers.length; i += 1) {
                  for (var j = 0; j < numbers[i].length; j += 1) {
                      process.stdout.write(numbers[i][j] + ' ');
                  }
              }
          }
          

          In conclusion, YES, concatenating the resulting string is too big for this environment that it doesn't fit, and in those cases is better to use process.stdout.write to print in-line instead of console.log because this adds breaking lines.

          I really hope this can help others JS developers, and please thank Kakason, I couldn't do it without him.

          • + 0 comments

            Thanks! Debugging is also one of my interest :D

          • + 1 comment

            Thanks a lot. That really helped. While all the other discussions here are about avoiding IO calls, it's the only solution for javascript :)

            I still don't understand what was going on, I don't think the string length was the problem (http://stackoverflow.com/questions/5926263/javascript-object-max-size-limit#answer-5926426).

            btw I also experience regularly the issue with getting different responses for the same code. I already wrote support, seems to be a JS problem...

            • + 0 comments

              Yes, it seems to be a problem with the hackerrank's JS environment. I'm glad this post helped you ;)