probability problem, zhu 2023-05-04

(1) Five people enter the elevator on the first floor of a 10-floor building. Each of them chooses a floor to leave the elevator at random, and each floor is equally likely to be chosen, independently of all the others. Set up a box model first.

teacher's solution

# solution to (a)
print( 1 - (8/9)**5 )
# 0.4450710426933565

# solution to (b)
print( (8/9)**5 + 5 * (1/9) * (8/9)**4 )
# 0.9017595556232958

# solution to (c)

print( (1/9)**5 * 9 )

# 0.00015241579027587253

python solution

# 2023-05-04

# number of floors
nflr = 9

xoutcomes = [(x1, x2, x3, x4, x5 )
for x1  in range(1, nflr+1)
for x2  in range(1, nflr+1)
for x3  in range(1, nflr+1)
for x4  in range(1, nflr+1)
for x5  in range(1, nflr+1)
]

print(len(xoutcomes) )
print( 9**5 == 59049)

def f_has_floor(xlist, xfr):
    """list of all outcomes that contains floor xfr"""
    return filter(lambda x: xfr in x, xlist)

def f_has_no_floor(xlist, xfr):
    """list of all outcomes that does not contains floor xfr"""
    return filter(lambda x: xfr not in x, xlist)

def f_occur(xx):
    """tuple xx to hash. return a hash.
key is floor, value is num of occurrence.
eg
(3,4,2,3)
return
{3: 2, 4: 1, 2: 1}
"""
    xh = {}
    for v in xx:
        if v in xh:
            xh[v] = xh[v] +1
        else:
            xh[v] = 1
    return xh

# HHHH---------------------------------------------------

# print( "nobody picked floor 2" )
# xx= list (f_has_no_floor(xoutcomes, 2))
# for v in xx: print( v)
# print(len (xx) )
# # 32768
# print( 1 - len (xx)/ len(xoutcomes) )
# # 0.4450710426933564

# HHHH---------------------------------------------------

# def xfilterF(xx, xflr):
#     """xx is tuple. xflr is a floor number. return true if 0 or 1 picked xflor"""
#     xh = f_occur(xx)
#     if (xflr in xh):
# 	    # return (xh[xflr] == 0)
# 	    return (xh[xflr] == 1) or (xh[xflr] == 0)
#     else:
# 	    return True

# print( )
# print( "0 or 1 person picked floor 3" )
# xx = list(filter( lambda x: xfilterF(x,3) , xoutcomes))
# for v in xx: print( v)
# print( len (xx) )
# # 53248
# print( len (xx)/ len(xoutcomes) )
# # 0.9017595556232959

# HHHH---------------------------------------------------

def xfilf2(xx):
    """return true is all entry in tuple xx is same"""
    xh = f_occur(xx)
    return len(xh) == 1

print( )
print( "all picked same floor" )
xx = list(filter( xfilf2 , xoutcomes))
for v in xx: print( v)
print( len (xx) )
#
print( len (xx)/ len(xoutcomes) )
#

# for v in xoutcomes: print( v)

# (1, 1, 1, 1, 1)
# (1, 1, 1, 1, 2)
# (1, 1, 1, 1, 3)
# (1, 1, 1, 2, 1)
# (1, 1, 1, 2, 2)
# (1, 1, 1, 2, 3)
# (1, 1, 1, 3, 1)
# (1, 1, 1, 3, 2)
# (1, 1, 1, 3, 3)
# (1, 1, 2, 1, 1)
# (1, 1, 2, 1, 2)
# (1, 1, 2, 1, 3)
# (1, 1, 2, 2, 1)
# (1, 1, 2, 2, 2)
# (1, 1, 2, 2, 3)
# (1, 1, 2, 3, 1)
# (1, 1, 2, 3, 2)
# (1, 1, 2, 3, 3)
# (1, 1, 3, 1, 1)
# (1, 1, 3, 1, 2)
# (1, 1, 3, 1, 3)
# (1, 1, 3, 2, 1)
# (1, 1, 3, 2, 2)
# (1, 1, 3, 2, 3)
# (1, 1, 3, 3, 1)
# (1, 1, 3, 3, 2)
# (1, 1, 3, 3, 3)
# (1, 2, 1, 1, 1)
# (1, 2, 1, 1, 2)
# (1, 2, 1, 1, 3)
# (1, 2, 1, 2, 1)
# (1, 2, 1, 2, 2)
# (1, 2, 1, 2, 3)
# (1, 2, 1, 3, 1)
# (1, 2, 1, 3, 2)
# (1, 2, 1, 3, 3)
# (1, 2, 2, 1, 1)
# (1, 2, 2, 1, 2)
# (1, 2, 2, 1, 3)
# (1, 2, 2, 2, 1)
# (1, 2, 2, 2, 2)
# (1, 2, 2, 2, 3)
# (1, 2, 2, 3, 1)
# (1, 2, 2, 3, 2)
# (1, 2, 2, 3, 3)
# (1, 2, 3, 1, 1)
# (1, 2, 3, 1, 2)
# (1, 2, 3, 1, 3)
# (1, 2, 3, 2, 1)
# (1, 2, 3, 2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