<jupyter_start><jupyter_text>**Artificial Intelligence - MSc**CS6462 - PROBABILISTIC AND EXPLAINABLE AI SEM2 2021/2CS6462_Lab_2.3Enrique Naredo Calculate Exact Card Probabilities Numerically We will learn how we can get the exact probability using Python. * This is not always applicable but let’s try to solve the questions of Part 1.* The logic here is to generate all the possible combinations and then to calculate the ratio.<jupyter_code># importing modules
import itertools
# make a deck of cards
deck = list(itertools.product(
    ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'],
    ['Spade','Heart','Diamond','Club'])
)

# show deck
deck<jupyter_output><empty_output><jupyter_text>**Question 1:*** How we can get all the possible 5-hand cards of a 52-card deck?* Combinations: $$ {52\choose 5} = 2,598,960 $$<jupyter_code>from itertools import combinations

# get the (52 5) combinations
all_possible_by_5_combinations = list(combinations(deck,5))

# show the number of combinations
len(all_possible_by_5_combinations)<jupyter_output><empty_output><jupyter_text>**Question 2:**What is the probability that when two cards are drawn from a deck of cards without a replacement that both of them will be Ace?<jupyter_code># event 
Aces = 0

# get the (52 2) combinations
all_possible_by_2_combinations = list(combinations(deck,2))

# verify the event occurs in every hand
for card in all_possible_by_2_combinations:
    # count faces in each 2-card hand
    if [d[0] for d in card].count('A') == 2:
        # add one event if there are two aces
        Aces+=1

# compute the probability of the event
prob = Aces / len(all_possible_by_2_combinations)

# show the probability
prob<jupyter_output><empty_output><jupyter_text>**Question 3:**What is the probability of two Aces in 5-card hand without replacement?<jupyter_code># event 
Aces = 0

# verify the event occurs in every hand
for card in all_possible_by_5_combinations:
    # count faces in each 2-card hand
    if [d[0] for d in card].count('A') == 2:
        # add one event if there are two aces
        Aces+=1

# compute the probability of the event        
prob = Aces / len(all_possible_by_5_combinations)

# show the probability
prob<jupyter_output><empty_output><jupyter_text>**Question 4:**What is the probability of being dealt a flush (5 cards of all the same suit) from the first 5 cards in a deck?<jupyter_code># event 
flushes  = 0

# verify the event occurs in every hand
for card in all_possible_by_5_combinations:
    # count suits in each 5-card hand
    flush = [d[1] for d in card]
    # add one event if all 5 cards of all the same suit
    if len(set(flush))== 1:
        flushes+=1

# compute the probability of the event 
prob = flushes / len(all_possible_by_5_combinations)

# show the probability
prob<jupyter_output><empty_output><jupyter_text>**Question 5:**  What is the probability of being dealt a royal flush from the first 5 cards in a deck?<jupyter_code># event
royal_flushes = 0

# verify the event occurs in every hand
for card in all_possible_by_5_combinations:
    # count suits in each 5-card hand
    flush = [d[1] for d in card]
    # and then faces
    face = [d[0] for d in card]
    # add one event if the 5 cards dealt a royal flush 
    if len(set(flush))== 1 and sorted(['A','J', 'Q', 'K', '10'])==sorted(face):
        royal_flushes +=1

# compute the probability of the event        
prob = royal_flushes  / len(all_possible_by_5_combinations)

# show the probability
prob<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09399773499433749, 'fraction_numerical': 0.026330690826727066, 'mean_word_length': 4.385670731707317, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '265881', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1136, 'n_words': 430, 'path': 'Weeks_1-2/Labs/CS6462_Lab_2_3.ipynb', 'repo_name': 'goksuturann/CS6462_SEM2_2021-2', 'size': '3532'}