Solution to: [Day 3: Cards of the Same Suit](https://www.hackerrank.com/challenges/s10-mcq-5/problem)

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

- Table of Contents
- Notes
    - Permutations
    - Combinations
- Math Solution
- Monte Carlo Simulation
    - Imports
    - Constants
    - Auxiliary functions
    - Main


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


    <IPython.core.display.Javascript object>


# Notes
Permutations (when order matters) and combinations (when order doesn't matter).

## Permutations
An ordered arrangement of **r** objects from a set, **A**, of **n** objects (where 0 < r <= n) is called an r-element permutation of N. You can also think of this as a permutation of A's elements taken r at a time. 

The number of r-element permutations of an n-object set is denoted by the following formula:

\begin{equation}
\large
nPr = \frac
{n!}{(n - r)!}
\end{equation}


Note: We define 0! to be 1; otherwise, r = n would result in 0 division error


## Combinations
An unordered arrangement of **r** objects from a set, **A**, of **n** objects (where 0 < r <= A) is called an r-element combination of A. You can also think of this as a combination of A's elements taken r at a time.

Because the only difference between permutations and combinations is that combinations are unordered, we can easily find the number of r-element combinations by dividing out the permutations (r!):

\begin{equation}
\large
nCr = \frac
{nPr} {r!}
\end{equation}

\begin{equation}
\large
= \frac
{n!}
{r! * (n - r)!}
\end{equation}


When we talk about combinations, we're talking about the number of subsets of size r that can be made from a set of size A. 



# Math Solution
You draw 2 cards from a standard 52-card deck without replacing them.  What is the probability that both cards are of the same suit?


This solution is simple - but very easy to overthink. I *originally* thought the answer was a product of dependent events
\begin{equation}
= \frac{13}{52} * \frac{12}{51}
\end{equation}

However, the first event doesn't matter. You are only interested that the suit of the second card matches the arbitrary suit of the first card.

Thus, the math is:

\begin{equation}
\large
\frac{52}{52} * \frac{12}{51} = \frac{12}{51}
\end{equation}

# Monte Carlo Simulation

## Imports


```python
import random
import copy
from typing import List
```

## Constants


```python
DECK_SIZE = 52
NUM_SUITS = 4
```

## Auxiliary functions


```python
def create_deck(size: int, suits: int) -> List[int]:
	"""Returns a list of ints representing the different number of suites.
	"""
	assert not size % suits		# Check evenly divides

	deck, suit = [], 0
	for i in range(1, DECK_SIZE + 1):
		deck.append(suit)

		if not i % (size / suits):	# increment suit
			suit += 1

	return deck
```


```python
def get_ratio(deck: list, iterations: int) -> float:
	"""Returns ratio of times that suit chosen from deck is the same"""
	def get_rand_index(deck_size: int) -> int:
		"""Helper func to return random index of deck size"""
		return random.randint(0, deck_size - 1)
	

	same_suit = 0
	for _ in range(iterations):
		temp_deck = copy.deepcopy(deck)

		ind_1, ind_2 = get_rand_index(len(temp_deck)), get_rand_index(len(temp_deck) - 1)
		suit_1 = temp_deck[ind_1]
		del temp_deck[ind_1]
		suit_2 = temp_deck[ind_2]

		if suit_1 == suit_2:
			same_suit += 1
	
	return same_suit / iterations
```

## Main


```python
def main():
	iterations = 100000
	deck = create_deck(DECK_SIZE, NUM_SUITS)
	print(deck)
	ratio = get_ratio(deck, iterations)
	print(ratio)
```


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

    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
    0.23649


5157ecc1f101cda967215f07627b82d135227464
ipynb
Jupyter Notebook
statistics/10_days/10_day3cardsofthesamesuit.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
statistics/10_days/10_day3cardsofthesamesuit.ipynb
jaimiles23/hacker_rank
0580eac82e5d0989afabb5c2e66faf09713f891b
Apache-2.0
statistics/10_days/10_day3cardsofthesamesuit.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