Difference between revisions of "CSC111 Homework 9 Solutions"
(Created page with '<onlydft> ==Problem 2== <source lang="python"> sum = 1.0 denom = 1 count = 1 print "sum = ", sum # 71828182845 while sum*100000000000 < 271828182845: …') |
|||
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | --[[User:Thiebaut|D. Thiebaut]] 18:11, 14 April 2010 (UTC) | ||
+ | ---- | ||
+ | |||
+ | __TOC__ | ||
<onlydft> | <onlydft> | ||
+ | ==Problem 1== | ||
+ | |||
+ | <source lang="python"> | ||
+ | # 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() | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
==Problem 2== | ==Problem 2== | ||
<source lang="python"> | <source lang="python"> | ||
+ | # 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 | sum = 1.0 | ||
denom = 1 | denom = 1 | ||
count = 1 | count = 1 | ||
+ | series = "series = 1" | ||
+ | print "-"*50 | ||
+ | print "denom = ", denom | ||
+ | #print "term = ", 0 | ||
+ | print series, " = ", sum | ||
+ | print "number of terms = ", count | ||
− | |||
# 71828182845 | # 71828182845 | ||
while sum*100000000000 < 271828182845: | while sum*100000000000 < 271828182845: | ||
Line 14: | Line 116: | ||
print "denom = ", denom | print "denom = ", denom | ||
term = 1.0 / denom | term = 1.0 / denom | ||
− | print "term = ", term | + | print "new term = ", term |
sum = sum + term | sum = sum + term | ||
− | |||
count += 1 | count += 1 | ||
+ | series = series + "+1/%d" % denom | ||
+ | print series | ||
+ | print "series = %1.20f" % sum | ||
+ | print "number of terms = ", count | ||
denom = denom * count | denom = denom * count | ||
Line 26: | Line 131: | ||
# the output of the program is shown below: | # the output of the program is shown below: | ||
""" | """ | ||
− | |||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 1 | denom = 1 | ||
− | term = 1.0 | + | series = 1 = 1.0 |
− | + | number of terms = 1 | |
+ | -------------------------------------------------- | ||
+ | denom = 1 | ||
+ | new term = 1.0 | ||
+ | series = 1+1/1 | ||
+ | series = 2.00000000000000000000 | ||
+ | number of terms = 2 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 2 | denom = 2 | ||
− | term = 0.5 | + | new term = 0.5 |
− | + | series = 1+1/1+1/2 | |
+ | series = 2.50000000000000000000 | ||
+ | number of terms = 3 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 6 | denom = 6 | ||
− | term = 0.166666666667 | + | new term = 0.166666666667 |
− | + | series = 1+1/1+1/2+1/6 | |
+ | series = 2.66666666666666651864 | ||
+ | number of terms = 4 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 24 | denom = 24 | ||
− | term = 0.0416666666667 | + | new term = 0.0416666666667 |
− | + | series = 1+1/1+1/2+1/6+1/24 | |
+ | series = 2.70833333333333303727 | ||
+ | number of terms = 5 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 120 | denom = 120 | ||
− | term = 0.00833333333333 | + | new term = 0.00833333333333 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120 | |
+ | series = 2.71666666666666634100 | ||
+ | number of terms = 6 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 720 | denom = 720 | ||
− | term = 0.00138888888889 | + | new term = 0.00138888888889 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720 | |
+ | series = 2.71805555555555544700 | ||
+ | number of terms = 7 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 5040 | denom = 5040 | ||
− | term = 0.000198412698413 | + | new term = 0.000198412698413 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040 | |
+ | series = 2.71825396825396836675 | ||
+ | number of terms = 8 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 40320 | denom = 40320 | ||
− | term = 2.48015873016e-05 | + | new term = 2.48015873016e-05 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320 | |
+ | series = 2.71827876984127003723 | ||
+ | number of terms = 9 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 362880 | denom = 362880 | ||
− | term = 2.7557319224e-06 | + | new term = 2.7557319224e-06 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880 | |
+ | series = 2.71828152557319224769 | ||
+ | number of terms = 10 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 3628800 | denom = 3628800 | ||
− | term = 2.7557319224e-07 | + | new term = 2.7557319224e-07 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880+1/3628800 | |
+ | series = 2.71828180114638451315 | ||
+ | number of terms = 11 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 39916800 | denom = 39916800 | ||
− | term = 2.50521083854e-08 | + | new term = 2.50521083854e-08 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880+1/3628800+1/39916800 | |
+ | series = 2.71828182619849290091 | ||
+ | number of terms = 12 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 479001600 | denom = 479001600 | ||
− | term = 2.08767569879e-09 | + | new term = 2.08767569879e-09 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880+1/3628800+1/39916800+1/479001600 | |
+ | series = 2.71828182828616871092 | ||
+ | number of terms = 13 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 6227020800 | denom = 6227020800 | ||
− | term = 1.60590438368e-10 | + | new term = 1.60590438368e-10 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880+1/3628800+1/39916800+1/479001600+1/6227020800 | |
+ | series = 2.71828182844675936281 | ||
+ | number of terms = 14 | ||
-------------------------------------------------- | -------------------------------------------------- | ||
denom = 87178291200 | denom = 87178291200 | ||
− | term = 1.14707455977e-11 | + | new term = 1.14707455977e-11 |
− | + | series = 1+1/1+1/2+1/6+1/24+1/120+1/720+1/5040+1/40320+1/362880+1/3628800+1/39916800+1/479001600+1/6227020800+1/87178291200 | |
+ | series = 2.71828182845823018710 | ||
+ | number of terms = 15 | ||
final sum = 2.71828182846 | final sum = 2.71828182846 | ||
count = 15 | count = 15 | ||
+ | |||
""" | """ | ||
</source> | </source> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | ==Problem 3, Optional and Extra Credit== | ||
+ | |||
+ | <source lang="python"> | ||
+ | # solution program for Hw9, Part 3 (optional and extra credit) | ||
+ | # D. Thiebaut | ||
+ | # The program reads a file formatted as follows: | ||
+ | # | ||
+ | # name Jane Moore | ||
+ | # semester fall 2010 | ||
+ | # MWF 9 GEO 370 | ||
+ | # TR 9 CSC 111 | ||
+ | # MW 10 GOV 228 | ||
+ | # MWF 13 DAN 113 | ||
+ | # TR 13 FRN 254 | ||
+ | # F 15 ESS 940 | ||
+ | # F 14 ESS 940 | ||
+ | # | ||
+ | # and outputs the same information into an html file, reformatted | ||
+ | # to look nice and organized. | ||
+ | # | ||
+ | |||
+ | def getFiles(): | ||
+ | """gets the name of files from the user""" | ||
+ | inputFile = outputFile = None | ||
+ | |||
+ | #--- ask for a valid file name --- | ||
+ | while True: | ||
+ | inputFile = raw_input( "Input file name? " ) | ||
+ | try: | ||
+ | open( inputFile, 'r' ) | ||
+ | break | ||
+ | except: | ||
+ | print "Invalid file name. Does not exit. Please reenter!" | ||
+ | |||
+ | #--- ask for an output file name with .htm or .html at the end --- | ||
+ | while True: | ||
+ | outputFile = raw_input( "Output file name? " ) | ||
+ | if outputFile[-5:] != ".html" and outputFile[-4:] != ".htm": | ||
+ | print "Invalid file name. Make sure it ends in .htm or .html!" | ||
+ | else: | ||
+ | break | ||
+ | |||
+ | return inputFile, outputFile | ||
+ | |||
+ | |||
+ | def getInput( inputFile ): | ||
+ | """reads the input file and returns the name, semester and | ||
+ | schedule lines to the calling program""" | ||
+ | |||
+ | lines = open( inputFile ).readlines() | ||
+ | name = lines[0].strip() | ||
+ | semester = lines[1].strip() | ||
+ | return name, semester, lines[2:] | ||
+ | |||
+ | def parse( lines ): | ||
+ | """parses the schedule lines and returns 5 arrays containing | ||
+ | the various courses in the right slot""" | ||
+ | |||
+ | #--- create empty lists for each day of the week --- | ||
+ | M = [] | ||
+ | T = [] | ||
+ | W = [] | ||
+ | R = [] | ||
+ | F = [] | ||
+ | |||
+ | #--- fill the arrays with which is a blank space in html --- | ||
+ | for i in range( 17 ): # index 8 corresponds to 8:00 am. | ||
+ | M.append( " " ) | ||
+ | T.append( " " ) | ||
+ | W.append( " " ) | ||
+ | R.append( " " ) | ||
+ | F.append( " " ) | ||
+ | |||
+ | #--- read each line and extract information --- | ||
+ | for line in lines: | ||
+ | #print "line = ", line | ||
+ | words = line.split( ) | ||
+ | #print "words = ", words | ||
+ | #--- skip badly formatted lines --- | ||
+ | if len( words ) != 4: | ||
+ | continue | ||
+ | #--- distribute courses in arrays for each day --- | ||
+ | Days, time, dept, num = words | ||
+ | time = int( time ) | ||
+ | if Days.find( "M" )!= -1: | ||
+ | M[ time ] = dept+num | ||
+ | if Days.find( "T" )!= -1: | ||
+ | T[ time ] = dept+num | ||
+ | if Days.find( "W" )!= -1: | ||
+ | W[ time ] = dept+num | ||
+ | if Days.find( "R" )!= -1: | ||
+ | R[ time ] = dept+num | ||
+ | if Days.find( "F" )!= -1: | ||
+ | F[ time ] = dept+num | ||
+ | #print "M = ", M | ||
+ | |||
+ | return M, T, W, R, F | ||
+ | |||
+ | def generateHtml( outputFile, name, semester, M, T, W, R, F ): | ||
+ | """Generate the html code and stores it into the output file""" | ||
+ | MM = TT = WW = RR = FF = "" | ||
+ | for i in range (8, 17): # index 8 is 8:00 a.m. | ||
+ | MM = MM + "<td>%s</td>" % M[i] | ||
+ | TT = TT + "<td>%s</td>" % T[i] | ||
+ | WW = WW + "<td>%s</td>" % W[i] | ||
+ | RR = RR + "<td>%s</td>" % R[i] | ||
+ | FF = FF + "<td>%s</td>" % F[i] | ||
+ | |||
+ | file = open( outputFile, 'w' ) | ||
+ | file.write( """<html> | ||
+ | <title>%s's Schedule</title> | ||
+ | <body> | ||
+ | <h1>J%s's Schedule</h1> | ||
+ | |||
+ | <h2>%s<h2> | ||
+ | |||
+ | <table border="1"> | ||
+ | <tr><td>Day</td><td>8:00</td><td>9:00</td><td>10:00</td><td>11:00</td> | ||
+ | <td>12:00</td><td>13:00</td><td>14:00</td><td>15:00</td><td>16:00</td></tr> | ||
+ | <tr><td>Monday</td>%s</tr> | ||
+ | <tr><td>Tuesday</td>%s</tr> | ||
+ | <tr><td>Wednesday</td>%s</tr> | ||
+ | <tr><td>Thursday</td>%s</tr> | ||
+ | <tr><td>Friday</td>%s</tr> | ||
+ | </table> | ||
+ | </body> | ||
+ | </html> | ||
+ | """ % ( name, name, semester, MM, TT, WW, RR, FF ) ) | ||
+ | file.close() | ||
+ | |||
+ | |||
+ | # --------------------------------------------------------------------------------- | ||
+ | # MAIN PROGRAM | ||
+ | # --------------------------------------------------------------------------------- | ||
+ | |||
+ | def main(): | ||
+ | # get file names from user | ||
+ | inputFile, outputFile = getFiles() | ||
+ | |||
+ | # parse information into fields. | ||
+ | name, semester, lines = getInput( inputFile ) | ||
+ | |||
+ | # create lists for every day of the week | ||
+ | M, T, W, R, F = parse( lines ) | ||
+ | |||
+ | # generate the html code | ||
+ | generateHtml( outputFile, name, semester, M, T, W, R, F ) | ||
+ | |||
+ | main() | ||
+ | |||
+ | </source> | ||
</onlydft> | </onlydft> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC111]][[Category:Homework]][[Category:Python]] |