Soluție HackerRank pentru Random number generator, subdomeniul Probability, în Python 3. Include cerința formatată, exemple, explicația pașilor și cod…

  • Problemă: Random number generator
  • Domeniu: Probability
  • Limbaj: Python 3

Challenge: Random number generator

Subdomeniu: Probability (probability)

Scor cont: 5.0 / 5

Submission status: Accepted

Submission score: 1.0

Submission ID: 464719144

Limbaj: python3

Link challenge: https://www.hackerrank.com/challenges/random-number-generator/problem

Cerință

Random number generator

There is an ideal random number generator, which given a positive integer M can generate any real number between 0 to M, and [probability density function](http://en.wikipedia.org/wiki/Probability_density_function) is uniform in [0, M].

Given two numbers A and B and we generate x and y using the random number generator with uniform probability density function [0, A] and [0, B] respectively, what's the probability that x + y is less than C? where C is a positive integer.

Input Format

The first line of the input is an integer N, the number of test cases.

N lines follow. Each line contains 3 positive integers A, B and C.

Constraints

All the integers are no larger than 10000.

Output Format

For each output, output a fraction that indicates the probability. The greatest common divisor of each pair of numerator and denominator should be 1.

Sample Input

3
1 1 1
1 1 2
1 1 3

Sample Output

1/2
1/1
1/1

Cod sursă

from fractions import Fraction as F

def postprocess(n):
    if n.denominator == 1:
        return str(n.numerator) + '/1'
    else:
        return n
    
for i in range(int(input())):
    A, B, C = list(map(int,input().split()))
    p, q = max([A, B]),min([A, B])
    
    if A + B < C:
        result = F(1, 1)
    elif C <= A and C <= B:
        result = F(C ** 2, 2 * A * B)
    elif q <= C <= p:
        result = F((2 * C - q) * q, 2 * A * B)
    else:
        result = F(1) - F((A + B - C) ** 2, 2 * A * B)
        
    result = postprocess(result)
    print(result)
HackerRank Probability – Random number generator