Ad
  • Custom User Avatar

    @ignovak it is always a best practice to keep the original variable intact when it is passed into a function. When a Kata description makes no mention of whether a variable passed into a function should be left intact, you should always assume that a Kata expects the function to be pure.

  • Custom User Avatar

    @donaldsebleung So if the solution using sort is illegal, you should have mentioned this in details. Otherwise it would be correct to fix the kata so that it accepted any kind of solutions.

  • Custom User Avatar

    I think there are two way:
    1.run your check function first, save answer to a value, then send the array to user function.
    2.create a copy of array,send it to user function.

  • Default User Avatar

    Hello BenjaminDowns,

    I apologise for the very late reply; up until now I did not have a clue as to why the random tests did not work for certain solutions.

    After a period of careful investigation I can safely say that the problem is that you used the sort method for arrays which mutates the original array. Therefore, an unsorted array passed into your function will be fully sorted before it is passed into my solution (which then of course confirms that the array is sorted and returns true).

    Please implement your own algorithm that does not involve sort or other array methods that mutate the original array.

    Hope this helps :)

    Cheers,
    donaldsebleung

  • Default User Avatar

    Hello aaronbalthaser,

    I sincerely apologise for the long overdue reply; back then, I had no idea why the random tests did not work for certain (valid) solutions.

    As I can see from your solution you have used the sort method to carry out the comparison. Here lies the problem - since sort alters the original array, regardless of whether the array passed into your function is sorted or not, by the time your function has completed the comparison, the array will inevitably be sorted. The sorted array is then passed into my solution which confirms that it is already sorted.

    Try implementing your own algorithm that does not use sort or other array methods that change the array itself.

    Hope this helps :)

    Yours sincerely,
    Donald

  • Default User Avatar

    Ah, I see. The random tests failed because the sort method for arrays changes the original array. This means that in the random tests:

    1. Initially, an array whose numbers are not in order (e.g. [3,1,2,4]) is passed into your function. Your function then confirms that the array is not sorted since the original array does not equal the sorted array using sort.
    2. However, here is the problem - As you are using sort to do the comparison, you mutate the original array by sorting it. The sorted array ([1,2,3,4]) is then passed into my solution and my solution confirms that the (mutated) array is sorted.

    Perhaps you may want to implement your own sorting algorithm that does not use sort?

    Hope this helps :)

    Cheers,
    donaldsebleung

  • Custom User Avatar

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