Day 2: Basic Probability | 10 Days Of Statistics | HackerRank Solution

Hello coders, today we are going to solve Day 2: Basic Probability HackerRank Solution which is a Part of 10 Days of Statistics Series.

Day 2: Basic Probability

Objective

In this challenge, we practice calculating probability.

Task

In a single toss of 2 fair (evenly-weighted) six-sided dice, find the probability that their sum will be at most 9.

Solution – Basic Probability Solution

Python

dice1 = [1,2,3,4,5,6]
dice2 = [1,2,3,4,5,6]
total = len(dice1) * len(dice2)
total_possibilities = 0

# Verify possibilities
for i in range(len(dice1)):
    for j in range(len(dice2)):
        if (dice1[j] + dice1[i]) <= 9:
            total_possibilities = total_possibilities + 1
            print("{0} + {1} = {2}".format(dice1[i], dice2[j], (dice1[j] + dice1[i])))

# Get probability
probability = total_possibilities / total
print("Probability: {0}/{1} = {2}".format(total_possibilities,total,probability))

The Correct answer is <p>5 / 6</p>.

Disclaimer: The above Problem (Basic Probability) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *