Ad
  • Default User Avatar

    Length of an array is a public final int, so no calculation is done.

    see here

    http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7

  • Default User Avatar
  • Default User Avatar

    That is not true.

    i<length

    and

    i<arr.length

    have exactly the same cost.

    The difference would be, when comparing to collection size, such as ArrayList and using .size() method, but here it doesn't matter.

  • Default User Avatar

    for (int i=0; i<arr.length; i++)

    Calculate the length in the cycle, is not efficient

    int sum = 0;
    int length = arr.length;
    for (int i=0; i<length; i++) {
    sum += (i % 2 == 0) ? arr[i] : arr[i] * arr[i];
    }
    return sum;

  • Default User Avatar

    yes it works on the test cases. It actually passed on the RandomTest.

  • Default User Avatar

    I can't see any basic tests with only "59544965313" in the input string. I just tried the Java tests with a Codewarrior's Java solution taken at random and it worked fine. Does >> your solution work for "Your example test cases"?

    It does. It works for the example test cases, and for the random tests when I submit.

  • Default User Avatar

    Hi. Is the solution now available for this problem?
    What should be returned when this value was entered "59544965313"? (JAVA)

  • Custom User Avatar

    I can't see any basic tests with only "59544965313" in the input string. I just tried the Java tests with a Codewarrior's Java solution taken at random and it worked fine. Does your solution work for "Your example test cases"?

  • Default User Avatar

    After coming back to this kata, I'm now stuck at exactly the same point.
    When running in eclipse, my method orderedWeights("59544965313") simply returns "59544965313".
    Is it not supposed to do that ? I pass the other test(s). It basically does exactly what is supposed to do.

  • Default User Avatar

    Same issue. Java.
    BasicTests(WeightSortTest)
    ****** Basic Tests ******
    For input string: "59544965313"
    RandomTests(WeightSortTest)
    ****** Random tests ******
    Test Passed
    1 Passed
    1 Failed
    0 Errors
    Process took 3125ms to completeBasicTests(WeightSortTest)

  • Default User Avatar

    Awesome help, the sort integer thing is something I've seen on here in other people's code but not understood until your reference. Thanks once again for the great advice :)

  • Custom User Avatar

    The false is not the problem.
    When you return false, you are giving the correct answer instead of the other wrong stuff.
    When you give a correct answer, you move on to the next batch of tests which is where your code fails.

    The major bug in the code is here

    for (i=0; i<array1.length; i++)
    

    Always use var i. As of now, you are modifying the global variable i.
    I actually can't believe that the test code is making the same mistake and using a global i instead of a var i.

    As a result, your function and the test code are messing with each other. I am not really sure how exactly the submission timeout error is occuring, but this mutual interaction is definitely the cause.

    Also, the sort function by default uses string sort, even if the arguments are numbers.
    So, [9, 100, 20, 25] will get sorted as [100, 20, 25, 9] instead of [9, 20, 25, 100].

    Sometimes this difference in sorting won't matter. Sometimes it will.

    How to sort an array of integers correctly

  • Custom User Avatar

    Please which language?

  • Default User Avatar

    I didn't catch that this was supposed to be a joke, sorry for misinterpreting you there.

  • Custom User Avatar

    In your citation you are forgetting the :-) . I wanted this sentence to be a joke, maybe a bad joke. If somebody finds it as insulting or condescending I am very sorry and I apologize.

  • Loading more items...