Ad
  • Custom User Avatar

    Awesome, this should be considered best practice for C++ prior to C++11. So in my opinion every c++ developer should be aware of this solution too. For example you can't use std::to_string in c++99.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Writting down the detailed steps on a paper helped me to clarify the algo ;)

  • Default User Avatar

    So basically solution can be reduced to 2*(x1 * 2^0 + x2 * 2^1 + x3 * 2^2 ... xn * 2^n-1) + 1 or something similar, I see it now when I write it on paper, but why?
    Is that generally known or you came up with this because of this kata?

  • Custom User Avatar

    I used to do this, but it's better to use std::to_string soo yeah.

  • Default User Avatar

    Somethings wonky with negative test outputs.

    I have derived the negative extension myself, as well as checked it against Wikipedia's...and yet it still gives errors on random negative numbers

  • Custom User Avatar

    Cheers! It looks like you solved the kata.

    I suppose you've figured out that source.Count() enumerates the source and which is why it fails the tests. Using Count as an existence test is bad because the enumerable could be very large and costly to determine when all that is desired to know is whether there is at least one. The LINQ operator for existence tests is Any which does such a test -- but even calling Any would fail the test since it enumerates the first element of the enumerable.

    Imagine the following enumerable for a test case in CodeWars:

    private IEnumerable<int> TimeOutEnumerable()
    {
        Thread.Sleep(TimeSpan.FromDays(1));
        yield return 1;
    }
    

    Implementations with early-out checks for source.Count() or source.Any() will encounter the CodeWars timeout and have their executions stopped.
    Thanks for your questions and for doing the kata.

  • Custom User Avatar

    I see no optimization here... just straighforward implementation of formula.

  • Default User Avatar

    But for -6 -> -8 is expected and that is fine?

    Edit:
    I got it, thanks! :)

  • Custom User Avatar

    No, it should not be negative. Yes, you are missing the "Hint I" from the description.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    If the hashed pin is 99999 you this code will return string.Empty

    I thougth CodeWars would check the boundary values when you do the Atempt?

  • Custom User Avatar

    Hello,

    How fast was your solution?
    Mine passes n = 10e7 in 1.394 seconds but it still times out during the random test.

    [edit] for anyone wondering the threshold is to be able to return n=10e7 in approximately less than 1 second.

  • Default User Avatar

    Tbh took me a while to pass in 12s :D

  • Default User Avatar

    For this part: The operator must call the selector-Func no more than once per element.
    What is considered element, is element considered pair, eg.:

    Calling Tuple.Create(0,1) and Tuple.Create(1,2) is considered double calling for element 1? Or element is considered as pair 0 1 (or 1 2)?

    ------ edit -----
    I figured out that it is pair but my test for iteration once over source was failing because I put

    if (source.Count() == 0) yield break;

    at begining of my second method that contained implementation, can someone explain why? I put that as protection line (not to even go to implementation if there are no elements) and it works fine only without that line. Imo I thing that it should't be an issue to have it or I am missing something?