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.
So that time complexity is O(n) instead of O(n log n) which seems to be what most people have done. IMHO, the test cases weren't rigorous enough to force people to take the more optimal route.
Why?
Doesn't seem to work for n = 0
Wow! Best answer I've seen all day!
This solution made me happy
Creation of intermediate list is unnecessary, use generator expression instead:
Yustynn did a great job explaining what I meant.
I'm new to programming, but I think he's talking about having two iterators instead of one.
In the original solution, you had ruby run through each element one by one to filter out unwanted elements. Then you go through those selected elements one by one again to sum them up.
His solution does it all one shot. In his inject, he goes through the elements one by one but performs both actions (filtering and sumation) before moving on to the next element. Meaning that instead of running through all the elements twice (once to filter, once to sum), he does it once and he's done.
So worst case: all integer values. All elements are run through once, none are rejected. Then all have to be run through yet again to sum. Two run throughs instead of one, so supposedly 2x the time taken.
Simple, elegant - the entire reason I use codewars is to be enlightened by such answers!
Using it and works like a charm... What was it that frustrated you?
Could you explain further? I am trying to get my head around why it'll require twice as much computing in worst case.
Already submitted the random test cases. Waiting for author's answer.
I knew I had to take advantage of the bit representation of the numbers, but I didn't think of using this solution.
http://stackoverflow.com/questions/600293/how-to-check-if-a-number-is-a-power-of-2
Anyone wanna explain this one ?
Loading more items...