본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 109

반응형

Q>

You're on your way to a board game convention.

(보드 게임 컨벤션으로가는 중입니다.)

Chances are there’ll be some stiff competition, so you decide to learn more about dice probabilities since you suspect you'll be rolling a lot of them soon.

(기회가 있기 때문에 치열한 경쟁이있을 수 있으므로 곧 많은 돈을 굴릴 것으로 의심되므로 주사위 확률에 대해 자세히 알아보기로 결정했습니다.)

Typically, when using multiple dice, you simply roll them and sum up all the result.

(일반적으로 여러 주사위를 사용하는 경우 주사위를 굴려 모든 결과를 요약합니다.)

To get started with your investigation of dice probability, write a function that takes the number of dice, the number of sides per die and a target number and returns the probability of getting a total roll of exactly the target value.

(주사위 확률에 대한 조사를 시작하려면 주사위 수, 주사위 당 측면 수 및 목표 수를 취하고 정확히 목표 값을 얻는 확률을 반환하는 함수를 작성하십시오.)

The result should be given with four digits precision as ±0.0001.

(결과는 ± 0.0001로 4 자리 정밀도로 주어져야합니다.)

For example, if you roll 2 six-sided dice, the probability of getting exactly a 3 is 2/36 or 5.56%, which you should return as ≈0.0556.

(예를 들어 육면 주사위 2 개를 돌릴 경우 정확하게 3을 얻는 확률은 2/36 또는 5.56 %로, 다시 0,555로 반환해야합니다.)

For each test, assume all the dice are the same and are numbered from 1 to the number of sides, inclusive. So a 4-sided die (D4) would have an equal chance of rolling a 1, 2, 3 or 4. A 20-sided die (D20) would have an equal chance of rolling any number from 1 to 20.

(각 테스트에 대해 모든 주사위가 같고 1에서 측면 수까지 번호가 매겨 졌다고 가정합니다. 따라서 양면 다이 (D4)는 1, 2, 3 또는 4를 굴리는 동일한 기회를 갖습니다. 20면 다이 (D20)는 1에서 20까지 임의의 숫자를 굴릴 가능성이 동일합니다.)

 

Tips: Be careful if you want to use a brute-force solution -- you could have a very, very long wait for edge cases.

       (무차별 대용 솔루션을 사용하려면주의해야합니다. 아주 오래 기다릴 수도 있습니다.)

 

Input: Three arguments. The number of dice, the number of sides per die and the target number as integers.

        (세 가지 인수. 주사위 개수, 주사위 당 측면 수 및 대상 숫자입니다.)

 

Output: The probability of getting exactly target number on a single roll of the given dice as a float.

           (주어진 주사위의 단일 롤에서 정확히 대상 숫자를 플로트로 가져올 확률입니다.)

 

Example:

probability(2, 6, 3) == 0.0556  # 2 six-sided dice have a 5.56% chance of totalling 3
probability(2, 6, 4) == 0.0833
probability(2, 6, 7) == 0.1667
probability(2, 3, 5) == 0.2222  # 2 three-sided dice have a 22.22% chance of totalling 5
probability(2, 3, 7) == 0       # The maximum you can roll on 2 three-sided dice is 6
probability(3, 6, 7) == 0.0694
probability(10, 10, 50) == 0.0375

 

How it is used: This task illustrates some of the basics of probability.

                     (이 작업은 확률의 기본 사항 중 일부를 보여줍니다.)

                     Many events can be described as the combination of other events.

                     (많은 이벤트가 다른 이벤트의 조합으로 설명 될 수 있습니다.)

                     In this case you're combining several dice into one total to crit the Orc King for massive damage.

                     (이 경우 여러 주사위를 하나의 합계로 결합하여 오크 왕에게 엄청난 피해를 입 힙니다.)

 

Preconditions:
1 ≤ dice_number ≤ 10
2 ≤ sides ≤ 20
0 ≤ target < 1000

 

A>

# factorial : 팩토리얼은 1부터 양의 정수 n까지 모두 곱한 것
from math import factorial

def choose(a, b):
    if b == 0 or a == b:
        return 1
    else:
        numer = factorial(a)
        denom = factorial(b) * factorial(a - b)
        return numer // denom

def probability(n, s, p):
    # 예외 처리 사용(try / except)
    k_max = (p - n) // s
    try:
        return 1 / (s ** n) * sum((-1) ** k * choose(n, k) * choose(p - s * k - 1, n - 1) for k in range(0, k_max + 1))
    except ValueError:
        return 0

if __name__ == '__main__':
    # These are only used for self-checking and are not necessary for auto-testing
    def almost_equal(checked, correct, significant_digits=4):
        precision = 0.1 ** significant_digits
        return correct - precision < checked < correct + precision


    assert (almost_equal(probability(2, 6, 3), 0.0556)), "Basic example"
    assert (almost_equal(probability(2, 6, 4), 0.0833)), "More points"
    assert (almost_equal(probability(2, 6, 7), 0.1667)), "Maximum for two 6-sided dice"
    assert (almost_equal(probability(2, 3, 5), 0.2222)), "Small dice"
    assert (almost_equal(probability(2, 3, 7), 0.0000)), "Never!"
    assert (almost_equal(probability(3, 6, 7), 0.0694)), "Three dice"
    assert (almost_equal(probability(10, 10, 50), 0.0375)), "Many dice, many sides"

 

O>

Your result: 0.00011842471300586074

Pass: probability(9,19,142)

 

S>

https://py.checkio.org/

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 111  (0) 2019.07.02
Python Learn the basics Quiz 110  (0) 2019.07.02
Python Learn the basics Quiz 108  (0) 2019.06.27
Python Learn the basics Quiz 107  (0) 2019.06.27
Python Learn the basics Quiz 106  (0) 2019.06.27