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.
Speak for yourself. I found it valuable as it forced me to delve into Reflection. I learned something along the way.
Also, as Grandpaw liked to say: "If you ain't got nothin' nice t'say, 'tis better ain't t'say nothin' at all."
Cheers.
I blame YouTube for that.
Hence mob-upvoting the refactoring of a brute force solution into a method that replicates the behavior of native Java, except with higher CPU overhead.
This site offers a wonderful chance to actually learn a new language and use everything it has to offer, but not everyone is willing to take on the challenge.
I say they're missing out, but to each their own.
Best pratices doesn't mean most faster or efficient. Good balance (performance, readability, maintainability) is the key of best pratices: so it includes good readability.
As for why so many people marked it as "Best Practice"... maybe they liked the variable names and spacing between lines?! Just kidding. I'm guessing they just didn't realize there was a better way. Or they exhibited mob behaviour - once it has a few upvotes others upvote to go with the crowd.
@sheriv, it's not one iteration. For each index he's checking he sums up all the numbers on the left and all the numbers on the right. He's forcing the CPU to do the same calculations over and over, and to solve this it's unnecessary. For example if you are at an index and already know the left and right sums at that point, you only need to remove one element and add one element to check the neighbouring index - but the solution above has the program recalculate the entire sum on both sides. It's functional but it's not elegant or efficient.
I think the best solution gives us O(n) complexity since there'is only one iteration.
I don't think that this is as difficult as coming up with a formula of relativity theory. Others have decided that this kata is worth 4 kyu, which means that, yes, it's difficult, but not that difficult. As for the inverse function, it has a precise mathematical definition: If
f
is a function from the non-negative integers to the non-negative integers such thatf
is one-to-one and onto, thenf
has an inverseg
such that iff(x) = y
theng(y) = x
. Since I'm telling you that the mystery function has an inverse, I am implicitely telling you that it is one-to-one and onto.Not even easy to read. Just bad solution... ;)
I'm always wondering why people consider solutions like this best practice. Easy to read and understand: Yes. Best practice: Never ever.