
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to Find Out the Probability of Having n or Fewer Points in Python
Suppose we are playing a unique game and we have three values n, k, and h. We start from 0 points, then we can select a number randomly between 1 and h (inclusive) and we will get that many points. We stop when we have scored a minimum of k points. We have to find the probability that we have n or fewer points. Here any number can be chosen randomly and the outcomes all have the same probability.
So, if the input is like n = 2, k = 2, h = 10, then the output will be 0.11.
To solve this, we will follow these steps −
Define a function dp() . This will take path.
if path is same as k − 1, thens
return (minimum of n − k + 1 and h) / h
if path > n, then
return 0
if path >= k, then
return 1
return dp(path + 1) − (dp(path + h + 1) − dp(path + 1)) / h
From the main function, do the following −
if k is zero, then
return 1
if n < k , then
return 0
return dp(0)
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, n, k, h): if not k: return 1 if n < k: return 0 def dp(path): if path == k− 1: return min((n− k + 1), h) / h if path > n: return 0 if path >= k: return 1 return dp(path + 1)− (dp(path + h + 1)− dp(path + 1)) / h return dp(0) ob = Solution() print(ob.solve(2,2,10))
Input
2,2,10
Output
0.11
- Related Articles
- Program to Find Out the Maximum Points From Removals in Python
- Program to Find Out the Points Achievable in a Contest in Python
- Program to find out the maximum points collectable in a game in Python
- Find the probability of reaching all points after N moves from point N in C++
- Program to find out the position of a ball after n reversals in Python
- Program to find out the number of integral coordinates on a straight line between two points in Python
- Program to find out if k monitoring stations are enough to monitor particular points in Python
- Program to find out the number of accepted invitations in Python
- Program to Find Out the Minimal Submatrices in Python
- Program to Find Out the Strings of the Same Size in Python
- Program to find out is a point is reachable from the current position through given points in Python
- Python program to get all subsets having sum s\n
- Program to Find Out Currency Arbitrage in Python
- Python Program to find out the sum of values in hyperrectangle cells
- Program to find out the conversion rate of two currencies in Python
