Teaching Kids Programming: Videos on Data Structures and Algorithms
Rolling a dice three times one after another, what is the probability of having Two Sixes in a Row?
Rolling two sixes in a row can only be done in the following cases:
- First and Second Six (third one not six):
- Second and Third Six (first one not six):
- Three Sixes: the probability is
Adding these cases to desired probability:
1 2 3 | # 66? ==> (1/6)^2*(5/6) # ?66 ==> (1/6)^2*(5/6) # 666 ==> (1/6)^3 |
# 66? ==> (1/6)^2*(5/6) # ?66 ==> (1/6)^2*(5/6) # 666 ==> (1/6)^3
We can bruteforce since the total cases are 6*6*6 and then we count the cases when there are two consecutive sixes.
1 2 3 4 5 6 7 8 | num = 0 total = pow(6, 3) # 6*6*6 for first in range(1, 7): for second in range(1, 7): for third in range(1, 7): if (first == second == 6) or (second == third == 6): num += 1 print(num/total) |
num = 0 total = pow(6, 3) # 6*6*6 for first in range(1, 7): for second in range(1, 7): for third in range(1, 7): if (first == second == 6) or (second == third == 6): num += 1 print(num/total)
O(1) constant complexity for time and space since the input size is fixed to 3 dices (constant loops, 216). See also: Teaching Kids Programming – Probability of Rolling a Dice: Strictly Increasing Order One After Another
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
420 wordsloading...
Last Post: Teaching Kids Programming - Find Three Consecutive Integers That Sum to a Given Number
Next Post: Teaching Kids Programming - Probability of Rolling a Dice: Strictly Increasing Order One After Another