Ad
  • Custom User Avatar

    It seems like there's some floating point errors with the random tests:

    E.g.
    Expected: [0.29836609045702106, 0.18020364669666114, 0.12242481648117452, 0.0942457968269003, 0.08074828321098745, 0.06961875443997158, 0.05541084537058963, 0.053279659010182334, 0.045702107506511956]
    instead: [0.298366090457027, 0.18020364669666242, 0.12242481648117365, 0.09424579682689997, 0.08074828321098737, 0.06961875443997172, 0.05541084537058983, 0.053279659010182515, 0.04570210750651206]

    The elements of the arrays are accurate to 1-e14, yet tests still fail. I'd avoid comparing two floating point numbers directly.

    A quick fix to solve this would be to map the user solution with .map((v, i) => (v <= expected[i] + 1e-7 && v >= expected[i] - 1e-7) ? expected[i] : v). This will change each element to the expected element if it falls within a range of +- 1e-7.

  • Custom User Avatar

    You can validate source code. isMathySolution(require('fs').readFileSync('/home/codewarrior/solution.txt', 'utf8')); :)

    I like to think of this as a 2 part problem. First you've got to come up with a way of finding the most significant digit of the numbers, then create a distribution array.

    This isn't affected by people using string manipulations, right?
    I asked why you'd want to encourage people to do it 'math-y' because I don't think it adds much difficulty or improve performance. Isn't it just some log10,pow,floor?

  • Custom User Avatar

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

  • Custom User Avatar

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

  • Custom User Avatar

    I think I've got it updated using @kazk's idea. I've also scaled it up or down by multiples of 10 randomly.

  • Custom User Avatar

    Thanks, that's a good idea. I saw that you'd done that and was wondering why.

  • Custom User Avatar

    @sappharx, I also recommend giving user a copy of generated array. I've seen many issues opened by people who chose to modify the input for no good reason :(

    Test.assertDeepEquals(benford(arr.slice()), benfordSolution(arr));
    //                                ^
    
  • Custom User Avatar

    I think there's something wrong with the Kata Editor. Every time I run "validate solution", the top of the output window shows something like Time: 379ms Passed: 0 Failed: 0 Errors: 1 and there's nothing in the output console (even when nothing has changed from when I published it yesterday). So, for the moment I haven't been able to update using assertDeepEquals yet.

    Also, when the editor is working again, I'll update the random tests to have a Benford-ish distribution.

  • Custom User Avatar

    Yeah, the documentation is bit outdated :( But many newer kata uses it.
    You can see the source here: https://github.com/Codewars/codewars-runner-cli/blob/master/frameworks/javascript/cw-2.js#L374

  • Custom User Avatar

    Thanks for the suggestion, I didn't see Test.assertDeepEquals in the test framework docs. I'll try to use that instead.

  • Custom User Avatar

    Maybe probability can be used to generate the first digit. For example you could fill in an array with 9 ones, 8 twos, 7 threes... and choose first digit from that.
    Btw here is one basic way of creating random numbers 0-1 that have an inverse probability of being picked from their value.
    So smaller nums get to be picked more likely.

    let monteCarlo = _ => {
      while(true) {
        var r1 = Math.random(); // choose a random num
        var probability = 1 - r1; // assign the inverse of its value to probability
        var r2 = Math.random(); // choose another random num to measure against
        if(probability > r2) return r1;
      }
    };
    
  • Custom User Avatar

    Nice one.

    Would it be possible somehow to generate random test input that shows a Benford instead of a uniform distribution?

    (Well, yes, of course that's possible. But what would be the best/easiest method to get a population distributed across several orders of magnitude?)

    (Someone make a kata out of this? :P )

  • Custom User Avatar

    Great Kata, keep up the good work :D

  • Custom User Avatar

    Good job with your first kata, using appropriate test functions and includes random tests :)

    One suggestion is to use Test.assertDeepEquals instead of Test.assertSimilar because it has problem dealing with signed zeros. For example, Test.assertSimilar([-0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]) fails :( I don't know how/why signed zero can get included in the result, but I prefer safer option and it may prevent potential issues. So maybe consider changing that if others agree with me.