-
Notifications
You must be signed in to change notification settings - Fork 183
/
11 - Day 3 - Drawing Marbles.py
40 lines (30 loc) · 1.1 KB
/
11 - Day 3 - Drawing Marbles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# ========================
# Information
# ========================
# Direct Link: https://www.hackerrank.com/challenges/s10-mcq-6/problem
# Difficulty: Easy
# Max Score: 10
# Language: Python
# Multiple Choice Question - No code required but checked with code
# ========================
# Solution
# ========================
from itertools import permutations
from fractions import Fraction
# 1 for Red Marbles
# 0 for Blue Marbles
RED_MARBLES = [1, 1, 1]
BLUE_MARBLES = [0, 0, 0, 0]
# All combinations, excluded first blue
FIRST_DRAW = list(filter(lambda m: m[0] == 1, permutations(RED_MARBLES + BLUE_MARBLES, 2)))
# All combinations with second blue
MARBLES_REMAINING = list(filter(lambda m: m[1] == 0, FIRST_DRAW))
# Result is 2/3
print(Fraction(len(MARBLES_REMAINING), len(FIRST_DRAW)))
# ========================
# Explanation
# ========================
# A bag contains 3 red marbles and 4 blue marbles
# After drawing a red marble, the bag has now 2 red and 4 blue marbles (total of 6 marbles)
# Therefore, the probability of getting a blue marble is 4/6, simplified to 2/3
# >>> 2/3