text_plain_output_1.png
from collections import Counter

from collections import Counter

from itertools import permutations

from numpy.random import choice

from scipy.special import binom

import numpy as np # linear algebra

n, m = (4, 2)
def functional(m, n):
    antennas = [0 for i in range(m)] + [1 for i in range(n - m)]
    network = set(permutations(antennas))
    failure = 0
    for i in network:
        for j in range(len(i) - 1):
            if [i[j], i[j + 1]] == [0, 0]:
                failure += 1
                break
    k = len(network)
    return (k - failure) / k

colors = ['clubs' , 'diamonds' , 'hearts', 'spades']

def draw():

    # np.random.choice(colors,size=5)
    return list(zip(np.random.choice(range(1,14),size=5), np.random.choice(colors,size=5)))

d = draw()
d

def is_straight(draw):
    if len(set([i[1] for i in draw])) == 1:
        return False
    else:
        nums = np.sort([i[0] for i in draw])
        return np.all(nums == np.array((num[0] + i for i in range(5))))
is_straight(d)

# monte carlo simulation
n = 100_000
count = 0
for i in range(n):
    d = draw()
    if is_straight(d):
        count += 1
print(count/n)

'''A 5-card poker hand is said to be a full house if it consists of 3 cards of the same
denomination and 2 other cards of the same denomination (of course, different from
the first denomination). Thus, a full house is three of a kind plus a pair. What is the
probability that one is dealt a full house?'''

def full_house(draw):
    return set(Counter([i[0] for i in draw]).values()) == {3,2}
d = draw()
full_house(d)

n = 100000
count = 0
for i in range(n):
    d = np.random.choice([i % 13 for i in range(52)], replace=False, size=52).reshape(4, 13)
    if np.all([0 in d[j] for j in range(4)]):
        count += 1

n = 100_000
count = 0
for i in range(n):
    d = draw()
    
#     print(np.random.randint(0,13,size=(4,13)))
#     0 in draw[0]
    if full_house(d):
#         print(draw)
        count += 1
#         print(draw,'\n',i)
#     np.all([0 is in i[j]])
print(f'Probability of full house is  : {count/n}')

coin = list('HT')
n = 100000
for i in range(4):
    count = 0
    j = 0
    while j < n:
        toss = choice(coin, size=3)
        if Counter(toss)['H'] == i:
            count += 1
        j += 1

balls = [i for i in range(1, 21)]
choice(balls, 4, replace=False)
n = 10000
for i in range(4, 21):
    j = 0
    count = 0
    while j < n:
        if choice(balls, 4, replace=False).max() == i:
            count += 1
        j += 1
    print(f'Probability of X={i} by simulation and by formula are : {(count / n, binom(i - 1, 3) / binom(20, 4))} respectively')