diff --git a/python/__pycache__/bins.cpython-314.pyc b/python/__pycache__/bins.cpython-314.pyc new file mode 100644 index 0000000..c916ed5 Binary files /dev/null and b/python/__pycache__/bins.cpython-314.pyc differ diff --git a/python/__pycache__/dice.cpython-314.pyc b/python/__pycache__/dice.cpython-314.pyc new file mode 100644 index 0000000..d9267c3 Binary files /dev/null and b/python/__pycache__/dice.cpython-314.pyc differ diff --git a/python/__pycache__/simulation.cpython-314.pyc b/python/__pycache__/simulation.cpython-314.pyc new file mode 100644 index 0000000..1ebe60f Binary files /dev/null and b/python/__pycache__/simulation.cpython-314.pyc differ diff --git a/python/app.py b/python/app.py index e71e427..ec174fe 100644 --- a/python/app.py +++ b/python/app.py @@ -1,9 +1,101 @@ -class Bin: - pass + +import random +import unittest + + +class Bins: + def __init__(self, min_val, max_val): + self.bins = [0] * (max_val - min_val + 1) + self.min_val = min_val + + def increment_bin(self, value): + if self.min_val <= value < self.min_val + len(self.bins): + self.bins[value - self.min_val] += 1 + + def get_bin(self, value): + if self.min_val <= value < self.min_val + len(self.bins): + return self.bins[value - self.min_val] + return 0 + class Dice: - pass + def __init__(self, number_of_dice): + self.number_of_dice = number_of_dice + + def toss(self): + return [random.randint(1, 6) for _ in range(self.number_of_dice)] + + def toss_and_sum(self): + return sum(self.toss()) + class Simulation: - pass + def __init__(self, numberOfDies, numberOfTosses): + self.number_of_dice = numberOfDies + self.number_of_tosses = numberOfTosses + self.min_sum = numberOfDies + self.max_sum = numberOfDies * 6 + self.dice = Dice(numberOfDies) + self.bins = Bins(self.min_sum, self.max_sum) + + def toss(self): + rolls = self.dice.toss() + result = sum(rolls) + self.bins.increment_bin(result) + return rolls, result + + def run_simulation(self): + for _ in range(self.number_of_tosses): + self.toss() + + def print_results(self): + print('***') + print(f'Simulation of {self.number_of_dice} dice tossed for {self.number_of_tosses} times.') + print('***') + print() + + for sum_value in range(self.min_sum, self.max_sum + 1): + count = self.bins.get_bin(sum_value) + percentage = count / self.number_of_tosses + histogram_bars = '*' * int(percentage * 100) + print(f'{sum_value:2d} : {count:8d}: {percentage:.2f} {histogram_bars}') + + +class TestDice(unittest.TestCase): + def test_toss_returns_correct_number_of_rolls(self): + dice = Dice(2) + rolls = dice.toss() + + self.assertEqual(len(rolls), 2) + self.assertTrue(all(1 <= roll <= 6 for roll in rolls)) + + def test_toss_and_sum_range(self): + dice = Dice(2) + total = dice.toss_and_sum() + + self.assertTrue(2 <= total <= 12) + + +class TestBins(unittest.TestCase): + def test_increment_bin(self): + results = Bins(2, 12) + results.increment_bin(10) + + self.assertEqual(results.get_bin(10), 1) + + def test_get_bin_out_of_range(self): + results = Bins(2, 12) + + self.assertEqual(results.get_bin(1), 0) + + +def main(): + sim = Simulation(2, 10000) + sim.run_simulation() + sim.print_results() + + +if __name__ == '__main__': + main() + diff --git a/python/roll_log.txt b/python/roll_log.txt new file mode 100644 index 0000000..ce7748e --- /dev/null +++ b/python/roll_log.txt @@ -0,0 +1,3 @@ +5 +8 +5