Question: In the following Python Code, I need to give a detailed description of how each function works: from functools import * def Perm(n,r): Implements
In the following Python Code, I need to give a detailed description of how each function works:
from functools import * def Perm(n,r): """ Implements P(n,r) or n P r. Precondition: n >= r, n >= 0, r >= 0. """ assert(n >= r), "Error: Fed n < r" return reduce(lambda x,y: x*y, range(n, n-r, -1), 1) # Returns 1 when n = 0 def Fact(n): """ Factorial n. Builds on Perm. Can also be Perm(n,n-1) below. """ return Perm(n,n) def Comb(n,r): """ Implements C(n,r) or n C r. Precondition: n >= r, n >= 0, r >= 0. """ return Perm(n,r) // Fact(r) def PascTri(N): """ Return Pascal's Triangle from 0 C i thru N C i for 0 <= i <= N. """ for i in range(N+1): print ([ Comb(n,i) for n in range(i,i+1) for i in range(n+1) ]) def bdayColl(n): """ Given a subset of n people in a room, return the probability that all have distinct birthdays. Obtained as 356 P n / 365^n, where: the numerator represents the size of the event that all n of them have distinct birthdays; and 365^n is the size of the sample space. 365 P n realized using reduction. """ return ( float(Perm(365,n)) / (365. ** n) ) def plotBdayColl(N): """ Invoke bdayColl N times and plot the decreasing probability as N increases. """ for i in range(1,N+1): print(str(i) + " : " + str(bdayColl(i))) #-- Poker-hand probabilities: From http://www.math.hawaii.edu/~ramsey/Probability/PokerHands.html def singlePairProb(): return ( Comb(13,1) * Comb(4,2) * Comb(12,3) * (4*4*4) ) / float( Comb(52,5) ) #--end
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
