Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
@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.
@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.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.
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 returnstrue
).Please implement your own algorithm that does not involve
sort
or other array methods that mutate the original array.Hope this helps :)
Cheers,
donaldsebleung
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 - sincesort
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
Ah, I see. The random tests failed because the
sort
method for arrays changes the original array. This means that in the random tests:[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 usingsort
.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
This comment is hidden because it contains spoiler information about the solution