Soluție HackerRank pentru Binomial Distribution #1, subdomeniul Probability, în Python 3. Include cerința formatată, exemple, explicația pașilor și cod…

  • Problemă: Binomial Distribution #1
  • Domeniu: Probability
  • Limbaj: Python 3

Challenge: Binomial Distribution #1

Subdomeniu: Probability (probability)

Scor cont: 5.0 / 5

Submission status: Accepted

Submission score: 1.0

Submission ID: 464716729

Limbaj: python3

Link challenge: https://www.hackerrank.com/challenges/binomial-distribution-1/problem

Cerință

A blindfolded marksman finds that on the average he hits the target 4 times out of 5. If he fires 4 shots, what is the probability of

(a) more than 2 hits?

(b) at least 3 misses?

Submission Modes and Output Format

Your output should be two floating point/decimal numbers rounded to a scale of 3 decimal places (i.e., 1.234 format). There are two submission options:

1. Complete the challenge manually using pen and paper. Select *Plain Text* from the editor's language drop-down. Put the answer to question *(a)* on the first line and the answer to question *(b)* on the second line.

2. Hard-code the given parameters into a *Python* or *R* program that solves the probem, printing the solution to *(a)* on the first line and *(b)* on the second line.

Your answer should resemble something like:
<pre>
0.123
0.456
</pre>
(This is NOT the answer, just a demonstration of what the answering format should resemble).

Cod sursă

p = 4/5
n = 4


def binom(k, n, p):
    res = 1
    a = 1
    for i in range(1, n+1):
        a *= i
    b = 1
    for i in range(1, n-k+1):
        b *= i
    c = 1
    for i in range(1, k+1):
        c *= i
    return a/b/c * p**k * (1-p)**(n-k)


def prob(k):
    return binom(k, n, p)


if __name__ == '__main__':
    result = prob(3) + prob(4)
    print('%.3f' % result)

    result = prob(0) + prob(1)
    print('%.3f' % result)
HackerRank Probability – Binomial Distribution #1