Write your code: Tossing Coins#

Write a function that works as a coin-flipper simulator. Then, write another function that calls the coin flipper function several times to determine the approximate probability of getting a certain result combination for 2 tosses (regardless of order).

Function definition#

Coin Flipper Simulator#

  1. Coin type: 2 faces: head and tail.

  2. Coin is fair: equal chance of flipping head or tail.

  3. The function must return a string value corresponding to the tossed face (head or tail).

  4. You can use any random function from the random module.

from random import # add function

def coin_flipper():
    
    # write your code here
  File "/tmp/ipykernel_2275/951000014.py", line 1
    from random import # add function
                                     ^
SyntaxError: invalid syntax

Probability calculator#

  1. Function that accepts two string inputs corresponding to the desired tossed faces, where:

    • The first input is necessary;

    • If no second input is given, it should should default to an empty string.

  2. Function returns a float value between 0 and 1.

  3. Avoid using any knowledge of combinatorics to solve this problem. Instead, take advantage of the fact that the machine can simulate millions of tosses (samples) in a very short time.

Since this is based on random draws, the probability will be slightly different each time the code is run. So, the greater the number of samples, the more consistent the results.

def get_probability(toss1, toss2=''):
    
    # write your code here

Testing#

Check if your function returns the expected value using the cell below.

import unittest

class UnitTests(unittest.TestCase):
    def test_coin_type(self):
        self.assertTrue(isinstance(coin_flipper(), str), 'The function should return a string')
    def test_coin_head_tail(self):
        self.assertTrue(sorted(set(coin_flipper() for _ in range(100000))) == ['head', 'tail'], 'The function should return `head` or `tail`.')
    def test_coin_fairness(self):
        counts = [coin_flipper() for _ in range(100000)].count('head')/100000
        self.assertAlmostEqual(counts, 0.5, places=2, msg=f'The side should be rolled with a probability of 0.50')

    def test_probability_type(self):
        self.assertTrue(isinstance(get_probability('head','head'), float), 'The function should return a float')
    def test_probability_two_heads(self):
        self.assertAlmostEqual(get_probability('head','head'), 1/4, places=2, msg='The function should return 0.25 for two heads.')
    def test_probability_two_tails(self):
        self.assertAlmostEqual(get_probability('tail','tail'), 1/4, places=2, msg='The function should return 0.25 for two tails.')
    def test_probability_head_tail(self):
        self.assertAlmostEqual(get_probability('head','tail'), 1/2, places=2, msg='The function should return 0.50 for one head and one tail.')
    def test_probability_tail_head(self):
        self.assertAlmostEqual(get_probability('tail','head'), 1/2, places=2, msg='The function should return 0.50 for one tail and one head.')
    def test_probability_atLeast_head(self):
        self.assertAlmostEqual(get_probability('head'), 3/4, places=2, msg='The function should return 0.75 for at least one head.')        
    def test_probability_atLeast_tail(self):
        self.assertAlmostEqual(get_probability('tail'), 3/4, places=2, msg='The function should return 0.75 for at least one tail.')        


unittest.main(argv=[''], verbosity=2,exit=False)