CSC111 Homework 9 Solutions

From dftwiki3
Revision as of 13:08, 14 April 2010 by Thiebaut (talk | contribs) (Problem 2)
Jump to: navigation, search

Problem 1

# hw9a.py
# Kristina Fedorenko
# (edited by D. Thiebaut)
# 111c - aq
#
#--- This program calculates how many people from a given list can
#--- fit into the elevator. Elevator's weight limit is set by
#--- the user. The program outputs the max number of people,
#--- their total weight and their names.
#

#--- prints a line of "-" ---
def line():
    print "-"*40

#--- asks user for the weight limit. Rejects anything --- 
#---           not within 0-1000 range                ---
def getTotalWeight():
    n = 1001
    while not 0<=n<=1000:
        try:
            n = input("Please enter an elevator weight limit (0 - 1000): ")
            if not 0<=n<=1000:
                print "The number you entered is not within allowed range."
        except SyntaxError:
            print "Invalid input."
            continue
        except NameError:
            print "Invalid input."
            continue
    return n

#--- MAIN FUNCTION ---
def main():
    people = [ ("JT", 150), ("TJ", 80), ("TT", 120), ("BT", 170), ("Mike", 12),
              ("AF", 27), ("Lea", 78), ("Leo", 180), ("Al", 55), ("GK", 110),
              ("JR", 111), ("VB", 76)]
    print
    #--- get the limit from user ---
    allowedWeight = getTotalWeight()


    #--- sort people by weight ---
    inElev = []
    for name, weight in people:
        inElev.append([weight, name])
    inElev.sort()

    #--- to fit max number of people, people with the lowest 
    #--- weight should be chosen
    
    
    #--- weight all people and if more than limit, remove the heaviest ---
    TotalWeight = 0
    names = []
    for weight, name in inElev:  #--- recalculates weight
        TotalWeight += weight
        names.append( name )
        if TotalWeight > allowedWeight:
            TotalWeight -= weight
            names.pop()          #--- remove last name added
	    break
    
        
    #--- prints out the results ---
    line()
    print "%-55s %-5d" %\
    ("Maximum number of people that can fit in the elevator:", len(names))
    print
    print "%-55s %-5d" %\
    ("Total weight of the people who fit in the elevator:", TotalWeight)
    print
    if len( names ) > 0:
        print "Names of the people in the elevator:"
        for name in names:
            print "   -", name
    line()
    print

main()


Problem 2

 
# hw9b.sol.py
# D. Thiebaut
# Computes e, the base of the natural log to 11 decimal
# places by summing up terms of the form 1/n!.  
# 
sum = 1.0
denom = 1
count = 1
print "-"*50
print "denom = ", denom
print "term = ", 0
print "sum = ", sum
print "count = ", count

#                         71828182845
while sum*100000000000 < 271828182845:
    print "-"*50
    print "denom = ", denom
    term = 1.0 / denom
    print "term = ", term
    sum = sum + term
    print "sum = %1.20f" % sum
    count += 1
    denom = denom * count
    print "count = ", count

print "final sum = %1.20f " % sum
print "count = ", count

# the output of the program is shown below:
"""
--------------------------------------------------
denom =  1
term =  0
sum =  1.0
count =  1
--------------------------------------------------
denom =  1
term =  1.0
sum = 2.00000000000000000000
count =  2
--------------------------------------------------
denom =  2
term =  0.5
sum = 2.50000000000000000000
count =  3
--------------------------------------------------
denom =  6
term =  0.166666666667
sum = 2.66666666666666651864
count =  4
--------------------------------------------------
denom =  24
term =  0.0416666666667
sum = 2.70833333333333303727
count =  5
--------------------------------------------------
denom =  120
term =  0.00833333333333
sum = 2.71666666666666634100
count =  6
--------------------------------------------------
denom =  720
term =  0.00138888888889
sum = 2.71805555555555544700
count =  7
--------------------------------------------------
denom =  5040
term =  0.000198412698413
sum = 2.71825396825396836675
count =  8
--------------------------------------------------
denom =  40320
term =  2.48015873016e-05
sum = 2.71827876984127003723
count =  9
--------------------------------------------------
denom =  362880
term =  2.7557319224e-06
sum = 2.71828152557319224769
count =  10
--------------------------------------------------
denom =  3628800
term =  2.7557319224e-07
sum = 2.71828180114638451315
count =  11
--------------------------------------------------
denom =  39916800
term =  2.50521083854e-08
sum = 2.71828182619849290091
count =  12
--------------------------------------------------
denom =  479001600
term =  2.08767569879e-09
sum = 2.71828182828616871092
count =  13
--------------------------------------------------
denom =  6227020800
term =  1.60590438368e-10
sum = 2.71828182844675936281
count =  14
--------------------------------------------------
denom =  87178291200
term =  1.14707455977e-11
sum = 2.71828182845823018710
count =  15
final sum =  2.71828182845823018710
count =  15

"""