Add probability calculator

This commit is contained in:
CherryKitten 2022-11-27 16:23:12 +01:00
parent 03d4c5f43c
commit 99c34a5e62
Signed by: sammy
GPG key ID: 0B696A86A853E955
5 changed files with 100 additions and 0 deletions

View file

@ -0,0 +1,2 @@
language = "python3"
run = "python main.py"

View file

@ -0,0 +1,3 @@
# Probability Calculator
This is the boilerplate for the Probability Calculator project. Instructions for building your project can be found at https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/probability-calculator

View file

@ -0,0 +1,16 @@
# This entrypoint file to be used in development. Start by reading README.md
import prob_calculator
from unittest import main
prob_calculator.random.seed(95)
hat = prob_calculator.Hat(blue=4, red=2, green=6)
probability = prob_calculator.experiment(
hat=hat,
expected_balls={"blue": 2,
"red": 1},
num_balls_drawn=4,
num_experiments=3000)
print("Probability:", probability)
# Run unit tests automatically
main(module='test_module', exit=False)

View file

@ -0,0 +1,43 @@
import copy
import random
# Consider using the modules imported above.
class Hat:
def __init__(self, **kwargs):
self.contents = []
for arg in kwargs:
for i in range(0, kwargs[arg]):
self.contents.append(arg)
def draw(self, amount):
temp = copy.copy(self.contents)
drawn = []
if amount >= len(temp):
return temp
for i in range(0, amount):
rand = random.choice(temp)
drawn.append(rand)
temp.pop(temp.index(rand))
self.contents = temp
return drawn
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
success = 0
for i in range(0, num_experiments):
print("experiment:", i)
test = copy.deepcopy(hat)
balls = test.draw(num_balls_drawn)
counts = {}
match = True
for ball in balls:
counts[ball] = counts.get(ball, 0) + 1
for color in expected_balls:
if counts.get(color, 0) < expected_balls[color]:
match = False
if match:
success = success + 1
return success / num_experiments

View file

@ -0,0 +1,36 @@
import unittest
import prob_calculator
prob_calculator.random.seed(95)
class UnitTests(unittest.TestCase):
maxDiff = None
def test_hat_class_contents(self):
hat = prob_calculator.Hat(red=3,blue=2)
actual = hat.contents
expected = ["red","red","red","blue","blue"]
self.assertEqual(actual, expected, 'Expected creation of hat object to add correct contents.')
def test_hat_draw(self):
hat = prob_calculator.Hat(red=5,blue=2)
actual = hat.draw(2)
expected = ['blue', 'red']
self.assertEqual(actual, expected, 'Expected hat draw to return two random items from hat contents.')
actual = len(hat.contents)
expected = 5
self.assertEqual(actual, expected, 'Expected hat draw to reduce number of items in contents.')
def test_prob_experiment(self):
hat = prob_calculator.Hat(blue=3,red=2,green=6)
probability = prob_calculator.experiment(hat=hat, expected_balls={"blue":2,"green":1}, num_balls_drawn=4, num_experiments=1000)
actual = probability
expected = 0.272
self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.')
hat = prob_calculator.Hat(yellow=5,red=1,green=3,blue=9,test=1)
probability = prob_calculator.experiment(hat=hat, expected_balls={"yellow":2,"blue":3,"test":1}, num_balls_drawn=20, num_experiments=100)
actual = probability
expected = 1.0
self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.')
if __name__ == "__main__":
unittest.main()