Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 51 additions & 27 deletions Morpheus.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# MorpheusProblem

Morpheus problem programmatic solution