Ad
  • Custom User Avatar
  • Custom User Avatar

    Good one. You just need to free the allocated memory for sx and sy. The caller is responsible for freeing sz - that would be the test suite. In fact, the caller should have allocated the necessary memory for the result too.

  • Custom User Avatar

    True. Thanks for noticing!

  • Custom User Avatar

    Nice but the heap was not freed. References to allocated memory sx, sy are lost.

  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar

    Thank you for taking the time to reply!

  • Custom User Avatar

    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..2
    n+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..2
    n, 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

  • Custom User Avatar

    how did you figure out the pattern of fun x ?

  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar

    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.

  • Custom User Avatar

    You also forgot the random tests. This was 2017, no longer 2013.

    I'm not about to fix that for Yet Another Evaluate-My-Expression Kata, but it would have been really rather trivial.

    I won't bother OP with a downvote, but the translation deserves it.

  • Custom User Avatar
  • Loading more items...