5 kyu
Probabilities for Sums in Rolling Cubic Dice
1,818 of 2,648raulbc777
Loading description...
Mathematics
Statistics
Probability
Logic
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Haskell translation
Approved!
python new test framework is required. updated in this fork
Julia translation
Approved! +1
Cheers!
"Tracking Hits for Different Sum Values for Different Kinds of Dice". When you have some spare time you could solve it and give me your opinion. :)
This comment has been hidden.
Puzzle tag should be removed
This kata no longer has
Puzzle
tagDescription should be language-agnostic
Python new test framework should be used
Initial solution setup should just
return 0
test.approx_equals
should be usedJS Node 14. should be enabled
Initial solution setup should just
return 0
JS parameter names are misleading :
arr
is not an array, and should besum
totalSides
is a number of dice not sidesFixed
D translation
Good. Approved this one, too
Go translation
approved by
This comment has been hidden.
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:
You must return a float, as it appears in the description, you should not expect tests to accept other formats. You can easily make the conversion yourself. This is not a kata issue.
COBOL translation.
Approved
This comment has been hidden.
Hi. It's impossible to help you or fix the issue if there's any, with the few elements you have given. First you need to say which language you're using. I invite you to provide the exact logs you receive as well as the code you're using. Please use a spoiler flag, so users who didn't solve the kata won't be able to read the message. Also use markdown tags to format your code, see there: https://docs.codewars.com/training/troubleshooting/#post-discourse
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Please format your code as I asked you, I gave you the link explaining how to do it above. An unformatted code is not readable and in Python particularly, because of the indentation rules, it is unusable. Please make it easier for people willing to help you.
This comment has been hidden.
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.
Because {3,5} is a different dice roll than {5,3} - in the first instance, die 1 rolled 3 and die 2 rolled 5, whereas in the second instance die 1 rolled 5 and die 2 rolled 3. So the combination of 3 and 5 is counted twice because there are two different ways of achieving it.
{4,4} means die 1 rolled 4 and die 2 rolled 4. There isn't another way of getting both dice to show 4, so it's only counted once.
Passed all tests, but knocks out this error: "TypeError: unsupported operand type (s) for -: 'NoneType' and 'float'".
This is not a kata issue but a problem from your code.
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.
This comment has been hidden.
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.
Already fixed
This comment has been hidden.
Fixed
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.
Fixed, they're now all
1e-10
.R translation kumited
Please carefully review and approve!
Reference solution and test suite commented for your pleasure!
-mpx
For rust, even with all tests passing the test fails with this:
It looks like a problem with test cases' code.
Looks like it can be bypassed with:
This comment has been hidden.
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
This comment has been hidden.
/
What is that supposed to mean?
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.
Hi, net70. I've recently tested the kata and it's working fine for Python 2+
This comment has been hidden.
In Python 2
/
is for integer division compared to Python 3, so along somewhere it'll always truncate the decimals to 0.@raulbc777 I wonder why the kata is locked at Python 2? That can really throw people off with the differences between the two versions.
Thanks for the kata! I've learnt a lot.
This comment has been hidden.
Which language please?
Python
Something strange happened. 96 people solved it without problem. I checked my solution and tests code and both give 0.0 for the probability in a different Ide.
Probability = 0.0 sum_ = 38 dice_amount = 3
Probability = 0.0 sum_ = 33 dice_amount = 4
Probability = 0.0 sum_ = 29 dice_amount = 3
I'm wondering what's going on.
To be clear, my solution was accepted, it was only the logs showing a
sum_
which was clearly not thesum_
that was being tested for. It doesn't even always do this, and the problem doesn't have to do with cases where the expected answer actually is 0. It's in the cases where it's in fact looking for something like 18 with 3 dice, it will log that it's looking for 38. But it will still accept the correct answer. So the issue, if any, is in the print function, I'd imagine.I think it's fixed. Check it and let me know. Thanks a lot.
Yep, ran it a couple of times, doesn't pop up any more.
For Ruby, the solution template is missing end keyword.
Should be:
instead of:
Thanks a lot for your observation!
Looks good now, and thanks for this kata :)
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 :)Good observation!. I was thinking about the future katas with different kinds of dice. :) It's fixed.
Thanks, confirmed and approved! :)
Thanks a lot. I'm working on the issue that you found in the ruby version of the next kata.
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
Hi finsternacht. Thanks for your feedback. The tests were prepared for efficient brute force algorithms. You gave a good idea in the possibility of further challenges related with probability. If you publish one, I'll be there to try to solve it. I haven´t seen your solution yet. :)
The JavaScript boilerplate is inconsistent with the tests.
Hi Johan. Give some additional explanations to your issue. Thanks for the feedback.
The default JavaScript is
but the tests query a function
rolldiceSumProb(sum, totalSides)
.checkMax()
appears to be a helper function from your own solution, not the main function.Yes, you were completely right. I'm very thankful for your observation. It's fixed. Thanks for your good solution and for your time invested in solving this kata.