From a1b48933101b8054074709f1874d16dddb8d28d6 Mon Sep 17 00:00:00 2001 From: Winkel Date: Fri, 9 Oct 2020 19:14:16 +0500 Subject: [PATCH 1/2] refactor --- Morpheus.py | 78 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/Morpheus.py b/Morpheus.py index 76cc181..4c015d3 100644 --- a/Morpheus.py +++ b/Morpheus.py @@ -1,41 +1,65 @@ -import random +from random import choice +from enum import Enum +from dataclasses import dataclass +from typing import Dict, List + +RERUNS = 100000 + + +class Color(Enum): + red = 'red' + blue = 'blue' + + +class Hand(Enum): + left = 'left' + right = 'right' + + +CONFIG = { + Hand.left: { + Color.red: 3, + Color.blue: 7 + }, + Hand.right: { + Color.red: 8, + Color.blue: 5 + } +} + + +@dataclass class Pill: - def __init__(self, color, hand): + def __init__(self, color: Color, hand: Hand): self.color = color self.hand = hand -def refillArray(): - f_array = [] - for i in range(0, 7): - f_array.append(Pill("blue", "left")) - for i in range(0, 3): - f_array.append(Pill("red", "left")) +def refillArray(config: Dict[Hand, Dict[Color, int]] = CONFIG) -> List[Pill]: + return [Pill(color, hand) for hand, item in config.items() for color, num in item.items() for _ in range(num)] - for i in range(0, 5): - f_array.append(Pill("blue", "right")) - for i in range(0, 8): - f_array.append(Pill("red", "right")) +def main() -> None: + left: int = 0 + right: int = 0 - return f_array + while left + right < RERUNS: + pills_in_hands: List[Pill] = refillArray() -left = 0 -right = 0 + while True: + pill = choice(pills_in_hands) + if pill.color == Color.red: + break -while left + right < 100000: - array = refillArray() - while True: - index = random.randint(0, len(array)-1) - pill = array[index] - if pill.color == "red": - break - if pill.hand == "right": - right += 1 - else: - left += 1 + if pill.hand == Hand.right: + right += 1 + else: + left += 1 -print "Left: " + str(left) + ", " + str(float(left)*100/(left + right)) + "%. Right: " + str(right) + ", " + str(float(right)*100/(left + right)) + "%" + print( + f'Left: {left}, {100*left/(left+right)}%\nRight: {right}, {100*right/(left+right)}%') +if __name__ == "__main__": + main() From 0c4999589d84450f0c8781c241d76d7536431489 Mon Sep 17 00:00:00 2001 From: Winkel Date: Fri, 9 Oct 2020 19:14:58 +0500 Subject: [PATCH 2/2] fix --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index abd6ea1..6180137 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # MorpheusProblem + Morpheus problem programmatic solution