Difference between revisions of "CSC111 Homework 4 Solution"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- ==Program 1== <source lang="python"> . #hw4a.py #111c-ap #Alecia M. D'Entremont #(Edited by D. Thiebaut) #Last edited 2.25.10 #This program calculates the final #g…')
 
(Program 1)
Line 75: Line 75:
 
     summing(grades, weights, midterm, final, num1)
 
     summing(grades, weights, midterm, final, num1)
  
 +
 +
main()
 +
.
 +
</source>
 +
 +
==Program 2==
 +
 +
<source lang="python">
 +
.
 +
#hw4b.py                                                                                                   
 +
#111c-ap                                                                                                   
 +
#Alecia M. D'Entremont                                                                                     
 +
#Last Edited 2.25.10                                                                                       
 +
 +
#This program will let the user know the last time                                                         
 +
#any CS department professors logged in                                                                   
 +
 +
import os
 +
 +
def main():
 +
    ## list of classes, two semesters                                                                     
 +
    classes = ["111", "112", "220", "231", "240",
 +
              "249", "250", "270", "260", "352"]
 +
    semester = 'ab'
 +
 +
    ## print login info for all profs, first fall semester then spring semester                                               
 +
    for letter in semester:
 +
        for course in classes:
 +
          os.system("lastlog -u " + course + letter)
  
 
main()
 
main()
 
.
 
.
 
</source>
 
</source>

Revision as of 11:21, 3 March 2010

--D. Thiebaut 16:17, 3 March 2010 (UTC)


Program 1

.

#hw4a.py
#111c-ap
#Alecia M. D'Entremont
#(Edited by D. Thiebaut)
#Last edited 2.25.10
#This program calculates the final
#grade for a student based on
#grades and the weighting of those grades

### get hw grades from user, add to list, sort, and return
def getGrades( num ):
    print "Please enter the grades of the homework assignments",
    print "below, one per line."
    print
    group = []
    for i in range( num ):
        grade = input("> ")
        group.append( grade )
        group.sort()
    return group

### print number of assignments, find lowest    
def assign(group,num):
    print "Number of assignments:   ", num
    print "Grade of low assignment: ", group[0]

###sum and average and weighted average of assignments (sans lowest    
def summing( group1, group2, var1, var2 , num ):
    summ = 0
    for grade in group1[1:]:
        summ = summ + grade
    print "sum of all assignments (without lowest):", summ
    avg = summ / ( len(group1) - 1 )

    print "Average of homework assignments:", avg
    wtavg = avg*group2[0] + var1*group2[1] + var2*group2[2]
    print "Weighted average of all homework assignments and exams:", wtavg

###get the weights of hw and exams
def weighting( ):
    group = []
    for i in ["What is the overall weight of the homework assignments? ", 
              "What is the weight of the midterm? ", 
              "What is the weight of the final exam? "]:
        group.append(input( i ))
    return group
    
###put it all together!
def main():
    # get number of hw
    num1    = input( "How many homework assignments this semester? ")

    # get weights
    weights = weighting( )

    # get the grades
    grades  = getGrades( num1)
 
    # get midterm and final grades
    print "Please enter the midterm grade,", \
            " followed by the final grade below, one per line:"
    midterm = input( "> " )
    final = input( "> ")

    # compute final grade
    assign(grades, num1)
    summing(grades, weights, midterm, final, num1)


main()
.

Program 2

.
#hw4b.py                                                                                                    
#111c-ap                                                                                                    
#Alecia M. D'Entremont                                                                                      
#Last Edited 2.25.10                                                                                        

#This program will let the user know the last time                                                          
#any CS department professors logged in                                                                     

import os

def main():
    ## list of classes, two semesters                                                                       
    classes = ["111", "112", "220", "231", "240",
               "249", "250", "270", "260", "352"]
    semester = 'ab'

    ## print login info for all profs, first fall semester then spring semester                                                 
    for letter in semester:
        for course in classes:
           os.system("lastlog -u " + course + letter)

main()
.