6 kyu

Power Laws

27 of 77marbiru
Description
Loading description...
Fundamentals
  • Please sign in or sign up to leave a comment.
  • saudiGuy Avatar

    python new test framework is required. updated in this fork

  • saudiGuy Avatar

    This comment has been hidden.

  • Rud1 Avatar

    Great kata, howewer I really dont see the point of last test case :

    # should work if (x1,y1) = (x2,y2) = (x3,y3)
    Test.assert_equals(power_law([1,120], [1,120], 1), 120)
    

    Does anyone see any interest, or any physical or practical meaning to that requirement, given that if (x1,y1) = (x2,y2), there is an infinity of solutions except on the given point...
    That forces the user to construct a special case for that very situation (except for lechevalier clever hack but then it would silently yeald a wrong result when x1=x2!=x3...) and makes the solution ugly and bloated IMHO.
    Would'nt it be more natural to give two distinct points or throw an exception if x1==x2?

  • aweleshetu Avatar

    This comment has been hidden.

  • marbiru Avatar

    hey everyone: I decided to change up the kata so that it gives you two points in the distribution and you have to find a third. I think this is a better way for people to understand what a power law distribution is, but it also makes the kata harder. My apologies to anyone/everyone who already solved this kata and whose solutions are now invalid, sorry about that. Any feedback/suggestions much appreciated as always!

  • marbiru Avatar

    I'm thinking of changing this kata so that instead of giving you the points held by position n and the relationship between position n and position 2n, it just gives you [position, points] stats for two arbitrary positions and has you infer the correct power law relationship from there (then finally prove you have done so by returning the points held by another arbitrary position). It adds a little extra challenge (will make the solution more than twice as long I think), but I think it better captures what power laws are about. Any thoughts?

  • marbiru Avatar

    hey guys -- I can imagine that this kata might run into problems with someone coming up with an equally-correct solution that is just a little different from mine, and which as a result is marked wrong by the checker (e.g. if they do rounding at a slightly different moment and come out with an answer that is 1 point different that would be marked as wrong). Could a more experienced coder help me figure out how to fix the tests so that this doesn't happen? (Or let me know if this isn't actually an issue)? Some possibilities I can think of are rounding the random decimals down to 2d.p. (currently I'm just using Math.random() as-is), or using some kind of leeway on the assertEquals statement (not sure how to do that but I know it exists). Any help/advice much appreciated; I've got no experience with these kinds of issues unfortunately so any fix I did right now would be kind of arbitrary.

    • JohanWiltink Avatar

      You could write a custom test function that only passes if the difference between the actual answer and the reference answer (taken as a positive number, so Math.abs it) is smaller than some absolute or relative value. Test.approxEquals does this, but only allows a 1e-9 relative error; I think you want something a little more forgiving.

      So calculate a Boolean from actual and expected, and Test.expect that. Make sure to supply decent failure messaging, and you're good to go.

      Would you need an actual code snippet for that, or can you take it from there?

    • marbiru Avatar

      ah, thanks so much, that's exactly what I needed -- I was going to ask for a code snippet but then I realised I could just try it myself and managed it on my own in the end :) I put in leeway of 1% of the reference solution, after playing around a bit with different induced discrepancies; hopefully that's a good-enough compromise to allow different methods to work but without letting "cheat" solutions through, but we'll see what happens as people solve it. thanks so much again for your help.