5 kyu

Probabilities for Sums in Rolling Cubic Dice

1,818 of 2,648raulbc777
Description
Loading description...
Mathematics
Statistics
Probability
Logic
  • Please sign in or sign up to leave a comment.
  • tobeannouncd Avatar
  • saudiGuy Avatar

    python new test framework is required. updated in this fork

  • depial Avatar
  • ejini战神 Avatar

    Puzzle tag should be removed

  • ejini战神 Avatar

    Description should be language-agnostic

  • ejini战神 Avatar
    • Python new test framework should be used

    • Initial solution setup should just return 0

    • test.approx_equals should be used

  • ejini战神 Avatar
    • JS Node 14. should be enabled

    • Initial solution setup should just return 0

  • trashy_incel Avatar

    JS parameter names are misleading :

    arr is not an array, and should be sum

    totalSides is a number of dice not sides

  • akar-0 Avatar
  • akar-0 Avatar
  • piocyg Avatar

    This comment has been hidden.

  • readerboy7 Avatar

    In Python 3.8, attempting to return a fraction.Fraction object causes the test suite to crash, since it can't format the returned object, whether or not the answer is correct.

    Note that returning floats works.

    Error message:

    Traceback (most recent call last):
      File "tests.py", line 17, in <module>
        assertFuzzyEquals(rolldice_sum_prob(11, 2), 0.055555555555)
      File "tests.py", line 14, in assertFuzzyEquals
        msg = msg.format(expected, actual)
    TypeError: unsupported format string passed to Fraction.__format__
    
  • akar-0 Avatar
  • Muratuzo Avatar

    This comment has been hidden.

  • Rostecki Avatar

    Hi, I solved it but my question is why: P(8, 2) = 5/36 counts five ({4,4}, {3,5}, {5,3}, {2,6}, {6,2}) and not 6 that is {4,4} should be 2 times like {3,5} and {5,3}. So the output should be 6/36. It's only my opinion.

  • user2066964 Avatar

    Passed all tests, but knocks out this error: "TypeError: unsupported operand type (s) for -: 'NoneType' and 'float'".

  • elmstedt Avatar

    Corrected R translation added. https://www.codewars.com/kumite/59f66c4025d5756d340000fb?sel=5f629d84e4729a002391aa93

    Original reference solution appears to have been copy/pasted from some source as it includes code for using dice with other than 6 sides.

    Original test cases test for a sum uniformly sampled from 10 - 40 and a number of dice uniformly from 2-7. This has the effect of approximately 43% of all test cases being degenerate, that is the target sum is larger than the highest possible total from the number of dice. New random test cases first select a random number of dice, then simulate that number of rolls and use that number as the target, ensuring there are no degenerate random cases. Degenerate cases are included in the sample tests. An assumption is made that degenerate cases are essentially the same so there is no reason for the random tests to include them.

    Orgininal reference solution attempts a closed form solution for caluclating the probabilities. This solution, however, generates results outside the stipulated margin of error in as few as 36 dice.

    New code performs tests with up to 400 dice.

  • Rhybon Avatar

    This comment has been hidden.

  • hypodermic Avatar

    I don't like the variable names in JavaScript for the function, it makes things very unclear.

    function rolldiceSumProb(arr, totalSides){ ...

    I don't know what arr means and totalSides confuses the number of dice with the number of sides each dice have. It took me awhile to realize that it meant totalDice, and even then the name "totalSides" is not good for programming.

    I recommend changing the functon name to function rolldiceSumProb(sum, numDice){ // Each die has 6-sides... With this the programmer can understand exactly what is expected and what the function should do at a glance.

  • piatra Avatar

    This comment has been hidden.

  • xarxziux Avatar

    There's a problem with the sample tests for R. I got the following error with the second test:

    Test Failed abs(rolldice_sum_prob(8, 2) - 0.13888888889) is not less than 1e-12. Difference: 1.11e-13

    But the same code passed the final tests.

  • mentalplex Avatar

    R translation kumited

    Please carefully review and approve!

    Reference solution and test suite commented for your pleasure!

    -mpx

  • zzandy Avatar

    For rust, even with all tests passing the test fails with this:

    warning: unnecessary parentheses around assigned value, #[warn(unused_parens)] on by default
      --> src/tests.rs:30:27
       |
    30 |         let dice_amount = (1 + seed % 10);
       |                           ^^^^^^^^^^^^^^^
    

    It looks like a problem with test cases' code.

    Looks like it can be bypassed with:

    #![allow(unused_parens)]
    
  • iphark Avatar

    Yo PLEASE unlock that for Python 3. Took me long enough to find out why all my answers are changed to 0 out of nowhere when it worked fine on my PC.

    Reason is that Python 2 handles the '/' operator different than Python 3

  • Darkest Dark Avatar

    This comment has been hidden.

  • net70 Avatar

    Hey there, great kata!

    though I seem to have a problem. I've got a solution to the kata (Python), but for some reason the return line changes the answer to 0 automatically. In Pycharm and online IDE's it seems to work just fine.

  • pyramidka Avatar

    Thanks for the kata! I've learnt a lot.

  • B4cchus Avatar

    This comment has been hidden.

  • md.farhanmemon Avatar

    For Ruby, the solution template is missing end keyword.

    Should be:

    def rolldice_sum_prob(sum_, dice_amount)
        # your code here
    end
    

    instead of:

    def rolldice_sum_prob(sum_, dice_amount)
        # your code here
        return prob
    
  • kazk Avatar

    Thanks for this kata, I just solved in all languages and didn't see any issue and my rank assessment made it approvable :] I'd suggest removing total_sides = 6 default parameter from "Initial Solution" for Ruby and Python because it's misleading. Let me know when you are done fixing (or decide to keep them), so I can approve with the average assessed 5 kyu :)

  • finsternacht Avatar

    Depending on the difficulty you're aiming at, I would include some testcases for larger numbers of dice to exclude solutions that just brute-force enumerate all possible combinations and then check whether they give the desired sum.

    If you were aiming for a lower difficulty, ignore that ;)

    Thanks for the nice challenge!

    PS: I would also like to see python3 support

  • JohanWiltink Avatar

    The JavaScript boilerplate is inconsistent with the tests.