Solution to: [Day 2: More Dice](https://www.hackerrank.com/challenges/s10-mcq-2/problem)

<h1 id="tocheading">Table of Contents</h1>
<div id="toc"></div>

- Table of Contents
- Sample question
- Math Solution
- Monte Carlo Solution
    - Imports
    - Probability of unique dice rolls at target
    - Main


```javascript
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')
```


    <IPython.core.display.Javascript object>


# Sample question 
Find the probability of getting 1 head and 1 tail when 2 fair coins are tossed.


Given the above question, we can extract the following:
- Experiment: tossing 2 coins.
- Sample space (S): The possible outcomes for the toss of 1 coin are {H, T}. 
    
As our experiment tosses 2 coins, we have to consider all possible toss outcomes by finding the Cartesian Product of the possible outcomes for each coin: 

\begin{equation} 
\normalsize
P(S) = \{H, T\} * \{H * T\}
\end{equation}

\begin{equation}
\normalsize
P(S) = \text{{(H, H), (H, T), (T, H), (T, T)}} = 4
\end{equation}


- Event (A n B): that the outcome of 1 toss will be H, and the outcome of the other toss will be T (i.e., P(A) = {(H, T), (T, H)}}

\begin{equation}
\normalsize
P(A) = \frac
{\text{number of favorable events}}
{\text{probabiliy space}}
\end{equation}

\begin{equation}
\normalsize
= \frac{P(A)}{P(S)} = \frac{2}{4} = \frac{1}{2}
\end{equation}

# Math Solution
- Experiment: Roll 2 die and find the probability that sum == 6 with different die faces
- Sample Space: possible outcomes == 36
- Event: P( (A != B) n (A + B == 6))
- Formula: 

\begin{equation}
\large
\frac{P(\text{number favorable events})}{P(\text{total number of events})} = \frac{P(A)}{P(S)}
\end{equation}


\begin{equation}
\large
P(A) = 
\frac{P(1,5) + P(2, 4) + P(4, 2) + P(1, 5)}
{P(S)}
\end{equation}


\begin{equation}
\large
= \frac{1 + 1 + 1 + 1}{36}
\end{equation}

\begin{equation}
\large
= \frac{4}{36}
\end{equation}


\begin{equation}
\large
= \frac{1}{9}
\end{equation}

\begin{equation}
\large
= 0.111
\end{equation}

# Monte Carlo Solution
 

## Imports


```python
from typing import List
```

## Probability of unique dice rolls at target


```python
def get_all_possible_combos(num_dice: int, dice_face: int, target: int) -> List[List[int]]:
	"""Returns Cartesian product of all possible dice rolls.
	
	Uses two helper functions:
		- get_possible_rolls, generator func to yield all die rolls.
		- recurse_roll_combos, helper recursion to create combinations for `num_dice`
	"""
	def get_possible_rolls(dice_face: int) -> int:
		"""Generator funct to yield possible rolls."""
		for i in range(1, dice_face + 1): yield i
	

	def recurse_roll_combos(processed_dice: int, all_roll_combos: list) -> List[List[int]]:
		"""Helper func to recurse through all dice."""
		if processed_dice == num_dice:		## Base case
			return all_roll_combos
		
		new_roll_combos = []
		for roll in all_roll_combos:
			roll_combo = [roll + [val] for val in get_possible_rolls(dice_face)]
			new_roll_combos += (roll_combo)
		
		processed_dice += 1
		return recurse_roll_combos(processed_dice= processed_dice, all_roll_combos = new_roll_combos)

	
	first_rolls = [[val] for val in get_possible_rolls(dice_face)]
	roll_combinations = recurse_roll_combos( 1, first_rolls)


	assert (len(roll_combinations) == dice_face ** num_dice)
	return roll_combinations
```

## Main


```python
def main():
	num_dice = 2
	dice_face = 6
	target = 6

	## Get Combinations
	roll_combos = get_all_possible_combos(num_dice, dice_face, target)

	## Count unique combos in target
	num_at_target = 0
	for combo in roll_combos:

		flag_unique = True
		for c in combo:
			if combo.count(c) > 1:
				flag_unique = False
				break
		
		if flag_unique and sum(combo) == target:
			num_at_target += 1
	
	## Return P(A) / P(S)
	prob = num_at_target / len(roll_combos)
	print( round(prob, 3))
```


```python
if __name__ == "__main__":
	main()
```

    0.111


0b573d34d5122b18916cb2fc26b0ff062114d9b2
ipynb
Jupyter Notebook
statistics/10_days/07_day2moredice.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
statistics/10_days/07_day2moredice.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
statistics/10_days/07_day2moredice.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
2021-09-22T11:06:58.000Z
2022-01-25T09:29:24.000Z
Qwen/Qwen-72B
1. YES
2. YES