Difference between revisions of "CSC111 Homework 9 Solutions 2011"

From dftwiki3
Jump to: navigation, search
(Problem #2)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 14:52, 28 November 2011 (EST)
 
--[[User:Thiebaut|D. Thiebaut]] 14:52, 28 November 2011 (EST)
 
----
 
----
 +
Please send me an email if you want a copy of the solution programs.
 +
 
==Problem #1==
 
==Problem #1==
 
<onlydft>
 
<onlydft>
Line 177: Line 179:
 
python3 elevator.py  
 
python3 elevator.py  
 
Candidates for first boarding of elevator:
 
Candidates for first boarding of elevator:
        JT (150 lbs)
+
        Lea (78 lbs)
        TJ (80 lbs)
+
      Leo (180 lbs)
         TT (120 lbs)
+
         AI (55 lbs)
         BT (170 lbs)
+
         GK (110 lbs)
  
 
Max weight? 400
 
Max weight? 400
 
--- BATCH 1 ---
 
--- BATCH 1 ---
 
3 people can board the elevator
 
3 people can board the elevator
Their combined weight is 350
+
Their combined weight is 243
The selected group includes:  JT, TJ, TT
+
The selected group includes:  AI, GK, Lea
The rejected group includes:  BT
+
The rejected group includes:  Leo
 
--- BATCH 2 ---
 
--- BATCH 2 ---
 
1 people can board the elevator
 
1 people can board the elevator
Their combined weight is 170
+
Their combined weight is 180
The selected group includes:  BT
+
The selected group includes:  Leo
 
The rejected group includes:   
 
The rejected group includes:   
  
 
"""
 
"""
 +
 
CANDIDATES = "candidates.txt"
 
CANDIDATES = "candidates.txt"
  
  
 
def getCandidates( fileName ):
 
def getCandidates( fileName ):
 +
    """gets the contents of the given file and returns all
 +
    of its lines, or simply stops the program if the file
 +
    cannot be open"""
 +
   
 
     try:
 
     try:
 
         file = open( fileName, 'r' )
 
         file = open( fileName, 'r' )
Line 204: Line 211:
 
         print( "Error opening file.  Program stopping." )
 
         print( "Error opening file.  Program stopping." )
 
         exit( 1 ) # stop on the spot!
 
         exit( 1 ) # stop on the spot!
 +
 
     lines = file.readlines()
 
     lines = file.readlines()
 
     file.close()
 
     file.close()
Line 209: Line 217:
  
 
def getMaxWeight():
 
def getMaxWeight():
 +
    """robust input of a positive number.  Function keeps on asking
 +
    until the user gets it right"""
 +
   
 
     while True:
 
     while True:
 
         try:
 
         try:
 
             maxWeight = eval( input( "Max weight? ") )
 
             maxWeight = eval( input( "Max weight? ") )
 +
           
 
             if ( maxWeight < 0 ):
 
             if ( maxWeight < 0 ):
 
                 print( "Negative weights are invalid.  Please try again" )
 
                 print( "Negative weights are invalid.  Please try again" )
 
             else:
 
             else:
 
                 return maxWeight
 
                 return maxWeight
 +
           
 
         except (NameError, SyntaxError):
 
         except (NameError, SyntaxError):
 
             print( "Invalid weight.  Please try again." )
 
             print( "Invalid weight.  Please try again." )
 +
  
 
def displayEverybody( L ):
 
def displayEverybody( L ):
 +
    """Displays all the people in L, including names and weights"""
 
     L2 = []
 
     L2 = []
 
     for weight, name in L:
 
     for weight, name in L:
Line 225: Line 240:
 
     print( "Candidates for first boarding of elevator:\n", '\n'.join( L2 ) )
 
     print( "Candidates for first boarding of elevator:\n", '\n'.join( L2 ) )
 
     print()
 
     print()
 +
  
 
def main():
 
def main():
 +
    #--- open file and get lines ---
 
     people = getCandidates( CANDIDATES )
 
     people = getCandidates( CANDIDATES )
 
     if len( people )==0:
 
     if len( people )==0:
Line 286: Line 303:
  
 
main()
 
main()
 +
 
</source>
 
</source>
 
</onlydft>
 
</onlydft>

Latest revision as of 21:04, 30 November 2011

--D. Thiebaut 14:52, 28 November 2011 (EST)


Please send me an email if you want a copy of the solution programs.

Problem #1


...


Problem #2


...