why not pass all test case in problem balanced numbers in codewars?

505 Views Asked by At

Currently i am doing a challenge in codewars on Balanced numbers and i wrote the code in dart and it successfully completed 100 test cases but for long numbers it is not working properly...So i think that it need some conditions for this long numbers:

String balancedNum(numb) {
  // your code here
  var numb1 = numb.toString();
  List a = numb1.split("");

  var left_sum = 0;
  var right_sum = 0;

  for (var i = 0; i <= a.length / 2; i++) {
    left_sum += int.parse(a[i]);
  }

  List<String> b = a.reversed.toList();

  //print(b);

  for (var i = 0; i < b.length / 2; i++) {
    right_sum += int.parse(b[i]);
  }

  //print(right_sum);

  if (left_sum == right_sum) {
    return 'Balanced';
  } else {
    return 'Not Balanced';
  }
}

link to the challenge:https://www.codewars.com/kata/balanced-number-special-numbers-series-number-1/train/dart

enter image description here

2

There are 2 best solutions below

0
Deepak Tatyaji Ahire On BEST ANSWER

The compiler verdict for the code is wrong answer because of a simple logical error.

This is because the length of the list has not been calculated correctly

Kindly have a look at the corrected code below, which passes all the 110 test on the platform:

String balancedNum(numb) {
      // your code here
      var numb1 = numb.toString();
      List a = numb1.split("");

      var left_sum = 0;
      var right_sum = 0;

      double d = ((a.length-1) / 2);
      int len = d.floor();

      print(len);

      for (var i = 0; i < len; i++) {
            left_sum += int.parse(a[i]);
      }

      List<String> b = a.reversed.toList();

      print(b);

      for (var i = 0; i < len; i++) {
            right_sum += int.parse(b[i]);
      }

      print(left_sum);
      print(right_sum);

      if (left_sum == right_sum) {
                return 'Balanced';
      } else {
                return 'Not Balanced';
      }
}
2
julemand101 On

Your problem is not about "long numbers" since you code also fails for the test case using the number 13.

You properly did not read the following rules:

  • If the number has an odd number of digits then there is only one middle digit, e.g. 92645 has middle digit 6; otherwise, there are two middle digits , e.g. 1301 has middle digits 3 and 0

  • The middle digit(s) should not be considered when determining whether a number is balanced or not, e.g 413023 is a balanced number because the left sum and right sum are both 5.

So you need to test if the number are even or uneven since we need to use that information to know how many digits we should use in the sum on each side.

I have done that in the following implementation:

String balancedNum(int numb) {
  final numb1 = numb.toString();
  final a = numb1.split("");
  var split = (a.length / 2).floor();
  
  if (a.length % 2 == 0) {
    split--; // the number is even
  }

  var left_sum = 0;
  var right_sum = 0;

  for (var i = 0; i < split; i++) {
    print(a[i]);
    left_sum += int.parse(a[i]);
  }

  final b = a.reversed.toList();

  for (var i = 0; i < split; i++) {
    right_sum += int.parse(b[i]);
  }

  if (left_sum == right_sum) {
    return 'Balanced';
  } else {
    return 'Not Balanced';
  }
}