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.
His solution is O(n) while yours is O(nlgn) :p
sorted will be delegeted to Arrays.sort() here
and Arrays.sort() is O(nlogn) which is more than
O(4n)
i like how you solved both max and min in the same loop
Very nice solution!
Sounds good. In general, I agree with your opinion. One should write code in a safety way, i.e. check each input and don't make any assumptions. Double-checking is better than getting an exception (or much worse: Having a security issue).
Thanks for comment, I see Your point and I think the disagreement lies on how you define the "best practise". With the definition You cited You're obviously right and I'm happy to admit that, I also checked that this is how the label itself is defined.
Therefore I amend my statement: The solution to this Kata IS a "best practise". I just want to emphasize that it is a best practise because we have some safety measures (only numbers, at least one element) provided, which is something that you might not always rely on in real life. Sounds good?
I'm afraid I have to disagree. The reason is very simple: What is a "best practise"? Answer: It the best solution which solve the requirement / the problem.
According to the task discription, the array has only numbers and at least one element. Why should we write boilerplate code? Why should we write any checks for situations that never occur?
The consequence of your idea would be that you get code which is not good readable and maintainable.
This comment is hidden because it contains spoiler information about the solution
I'm not sure how this ended up as the top solution, two passes over the collection is sub optimal.
You are doing the Task twice. it is not a preferable way. My Solutionis much more compact and operates one time
Pro: use of streams and min/max helper methods
Con: duplicated code results in splitting and int parsing twice
Nice work!
Nice solution! Love Stream API. Just a small note: you may pass Integer::parseInt instead of i -> Integer.parseInt(i)