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.
True. Thanks for noticing!
thanks)))
I’m happy to help
Hi,
the idea is to first find a formula for counting the 1s in a binary number (Hamming weight), then to calculate the partial sums.
we define:
fun(n) = sum(i=0..n, a(n)), where a(n) is hamming weight of n
The hamming weight of n is easy to calculate in logarithmic time, once you realise that removing the last digit divides the number by two:
a(2n+1) = a(n) + 1 // last digit is 1
a(2n) = a(n) // last digit is 0
Now we want to use the above formulas to calculate fun(2n) in terms of fun(n). One way to do it is to split the sum in two different sums over the even and the odd indexes. So we get:
fun(2n+1)
== sum(i=0..2n+1, a(i)) // definition of fun(n)
== sum(j=0..n, a(2j)) + sum(j=0..n, a(2j+1)) // splitting odd and even
== sum(j=0..n, a(j)) + sum(j=0..n, a(j)+1) // Hamming weight formula
== fun(n) + fun(n) + n + 1 // definition of fun(n)
== 2*fun(n) + n + 1
fun(2n)
== sum(i=0..2n, a(i))
== sum(j=0..n, a(2j)) + sum(j=0..n-1, a(2j+1))
== sum(j=0..n, a(j)) + sum(j=0..n-1, a(j) + 1)
== fun(n) + fun(n-1) + n
Thanks)
Hi Joohan,
thanks for your feedback.
I did not forget the random tests: I translated the complete kata including the tests.
As I understood from the codewars guidelines the translations are supposed to be faithful to the original rather than an attempt to provide an improved version of the original.
In this case the original kata could have been improved by adding randomized tests (as you suggested), adding more test cases for covering the corner cases and also by expanding the description and the examples provided.
Considering that this is a very simple evaluate-arithmetic-expression kata and many improved versions already existed in codewars, I am not sure the effort would be worth the time.
Regarding the downvote, feel free to downvote as you please.
If you think that translations should provide extra test cases with respect to the original, I think this should be an aspect that should be clarified in the translation guidelines. Perhaps you can contact the codewars website administrator about this?
thanks!
A.
Hi, is it possible to approve the translation?
thanks! they are there now
Haskell translation added (status: waiting approval)