CSC111 Homework 6 2015

From dftwiki3
Revision as of 20:11, 1 March 2015 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut (talk) 16:18, 1 March 2015 (EST)


<showafterdate after="20150305 16:00" before="20150601 00:00">

This assignment is due on 3/10/15 at 11:55 p.m.


Preparation


  • Create a program called testHw6.py with the code available on this page. Save it in the same place where you will store your hw6.py program.
  • Create a second program, called hw6.py. This program will contain all the functions that you are asked to write for this assignment. Add the code below to your hw6.py code:


# hw6.py
# yourname here
# This program contains functions that solve all the problems
# of Homework 6, including a function0() that is given as a 
# bonus.


# function0: receives a string, removes leading and trailing whitespace from
# it, and capitalize all its words, and add two stars at both ends.  Return the
# new string.
def function0( name ):
    name = "**" + name.strip().title() + "**"
    return name


# ================================================
#                    M A I N
# ================================================
def main():

    # --------------------------------------------------------
    # testing function0() with 3 different strings
    for name in [ "ARTHUR", "  smith COLLEGE  ", " lei   " ]:
        newName = function0( name )
        print( name )
        print( newName )
    print()



if __name__=="__main__":
   main()


  • The last 2 lines of the program are new. Don't worry about them, and just keep them in the code. All they do is call the main function when your program runs, the same way as before. The if __name__=="__main__": statement will be useful later. (You may want to read Section 7.1 in Zelle to learn more about this statement.)
  • The hw6.py program contains 1 function called function0() that is passed a string as a parameter, and returns a new string. The new string is created by taking the parameter, removing leading and trailing whitespace characters, capitalizing all the words in it, and adding 2 stars at both ends.
  • The main() function tests function0() by
  • calling it and passing it as a parameter one of 3 different strings ("ARTHUR", " smith COLLEGE ", " lei "),
  • catching the returned value in a variable called newName, and
  • printing the parameter and the returned value.
  • Run hw6.py and see how it works. Make sure you understand how main() tests function0().
  • Switch to testHw6.py (which you can keep open in another Idle window) and run it.
  • Here is the output you should get:


Function 0 passes 4 out of 4 tests.
Function1 does not exist or crashed. 0 out of 4 points.
Function2 does not exist or crashed. 0 out of 4 points.
Function3 does not exist or crashed. 0 out of 3 points.
Function4 does not exist or crashed. 0 out of 3 points.
Function5 does not exist or crashed. 0 out of 3 points.
Function6 does not exist or crashed. 0 out of 3 points.
Function7 does not exist or crashed. 0 out of 3 points.
Function8 does not exist or crashed. 0 out of 4 points.
Function9 does not exist or crashed. 0 out of 4 points.
Function10 does not exist or crashed. 0 out of 4 points.
Function11 does not exist or crashed. 0 out of 4 points.
Function12 does not exist or crashed. 0 out of 4 points.

Your functions pass 4 tests out of 47.


  • The output is telling you that function0() passed all 4 tests that testHw6.py subjected it to.
  • testHw6.py also indicates that Function1, Function2, all the way to Function12 were not found. This is normal: your hw6.py program only contains only function0(). Your assignment for this week is to code 12 different functions. Let's get started!


As you can see, you will have 2 different ways to test your program: 1) by calling your functions with different parameters in the main() function of hw6.py, or 2) by running testHw6.py, which will automatically import your program and call its functions, one after the other, with different parameters.


Function 1


  • Add a new function to your hw6.py program called function1(), with the following properties:
  • it has 1 parameter that is an integer or float number that represents a temperature in Celsius.
  • it returns the equivalent temperature in degrees Fahrenheit, defined as degree Fahrenheit = (degrees Celsius) * 9 / 5 + 32. The returned value is a float.


  • Add a new section to the main() program of your hw6.py that calls function1( ) and passes it some known temperatures, and print the returned value. For example:


# test function1() with -31, 0, 10, and 20 degrees Celsius
for celsius in [ -32, 0, 10, 20 ]:
     fahrenheit = function1( celsius )
     print( "celsius =", celsius, " fahrenheit  =", fahrenheit )


  • When you are satisfied that your program works, run testHw6.py to verify that it, too, finds function1() to run correctly. testHw6.py will report on whether it can run your function, and whether it works correctly.
  • If your function does not pass all the tests that testHw6.py subjects it to, you need to go back to hw6.py and see how you can modify function1().


Function 2


  • Repeat the same steps you took for function1(), and this time add a new function called function2() to hw6.py with the following properties:
  • function2() works the same way as function1() does, in that it receives a temperature in Celsius, and returns a temperature in Fahrenheit, but the returned value is the closest integer value in degree Fahrenheit.


Function 3


  • Add a new function called function3() that receives a string as a parameter, and returns a new string.
  • The parameter string contains a phone number, including an area code in parentheses.
  • The returned string contains the phone number without the area code.
  • For example, if the string passed to function3() is "(413) 555 1212", then the function returns "555 1212".


Function 4


  • Add a new function called function4() to hw6.py with the following properties:
  • It receives a string as a parameter. This string contains a phone number included in parentheses.
  • The new function returns a new string that is similar to the string passed as a parameter, except that the area code of the phone number is replaced by "XXX"
  • Example: function4( "Dial (413) 585 2700 for Smith College" ) will return the string "Dial (XXX) 585 2700 for Smith College".


Function 5


  • Add a new function to hw6.py called function5().
  • The parameter passed to function5 is a string containing a student Id number, of the form 990dddddd, where d is a digit.
  • The function will anonymize the Id appearing in the string by replacing it with 990123456.
  • Examples:
  • function5( "My Id is 990111222. What is yours?" ) will return the string "My Id is 990123456. What is yours?".
  • function5( "Alka's Id is 990998800." ) will return the string "Alka's Id is 990123456."


Function 6


  • Add a new function called function6() that receives the name of a text file, and returns a string equal to the first line of the file.
  • We assume that the file will always contain at least one line.
  • Example: if the file named data.txt contains the text "Sophia Smith\nSmith College\nNorthampton\n", then calling function6( "data.txt" ) will return the string "Sophia Smith".


Function 7


  • Add a new function called function7() that works in a very similar way function6() works, except now function7() will return the last line of the file whose name is passed as a parameter.
  • We assume that the file will always contain at least one line.
  • Example: if the file named text.dat contains the text "Sophia Smith\nSmith College\nNorthampton\n", then calling function7( "text.dat" ) will return the string "Northampton".


Function 8


  • Add a new function called function8() that is passed the name of a text file. It returns the contents of the file a one long string where all the letters 'g' are replaced by "gremlin" and the letters 'G' by "Gremlin".
  • Example: if the file named story.txt contains the string "Good job\nGreat god!\n", then calling function8( "story.txt" ) will make it return the string "Gremlinood job\nGremlinreat gremlinod!\n".


Function 9


  • Open a new Idle window and create a new program with the code below:


def main():
     farm = [ "dog", "cat", "mouse", "horse", "pig" ]
     newFarm = []
     newFarm.append( farm[ 1 ] )
     newFarm.append( farm[ 4 ] )
     
     for i in range( len( newFarm ) ):
          print( newFarm[ i ] ) 

main()


  • Observe the program. It has two lists. One contains several names of animals. The other one starts empty. The two brackets [] indicate that we are creating an empty list.
  • Now, observe the output of the program. Notice how the program added two different elements of the original farm list to the empty list newFarm. The loop at the end goes through all the elements of newFarm and prints them. You will need to inspire yourself from this short program to write function9().


  • Add a new function called function9() that receives as parameter a list of strings. It returns a new list made of all the items that are located at even indexes.
  • Example: if the list farm is equal to [ "dog", "cat", "mouse", "horse", "pig" ], then calling function9( farm ) will make the function return the new list [ "dog", "mouse", "pig" ], because "dog" is at Index 0 in farm, "mouse" at Index 2, and "pig" at index 4.


Function 10


  • Add a new function called function10() that receives as parameter a list of numbers. The numbers represent temperatures.
  • The function returns an integer that is the number of temperatures in the list that are greater than or equal to 32.
  • Example: if the variable temps is equal to [ 10, 40, 5, 32, 33 ], then function10( temps ) will return 3, since there are 3 temperatures of 32 or above in the list.


Function 11


  • Add a new function called function11() that receives as parameter a list of numbers. The numbers represent temperatures here as well. The function returns a new list that contains only the temperatures that are 32 or above.
  • Example: if the variable temps is equal to [ 10, 40, 5, 32, 33 ], then function11( temps ) will return the list [40, 32, 33], since these 3 temperatures are the only ones that are 32 or above in the first list.


Function 12


  • Add a last function called function12() to your program. This function receives a number (integer or float) that represents a grade, ranging from 0 to 100, included. The function returns a string describing the grade.
  • if the grade is 90 or above, function12() returns the string "Excellent"
  • if the grade is 75 or above, but less than 90, function12() returns "Very good"
  • if the grade is 60 or above, but less than 75, function12() returns "Average"
  • if the grade is 50 or above, but less than 60, function12() returns "Below average"
  • if the grade is 40 or above, but less than 50, function12() returns "Problematic"
  • if the grade is lower than 40, function12() returns "Fail"


Moodle Submission


Submit just one program to Moodle, in the HW 6 PB 1 section.


</showafterdate>


<showafterdate after="20150311 00:00" before="20150601 00:00">

# hw6.py
# D. Thiebaut
# This program contains functions that solve all the problems
# of Homework 6, including a function0() that is given as a 
# bonus.


def function0( name ):
    name = "**" + name.strip().title() + "**"
    return name

def function1( celsius ):
    fahr = celsius * 9 / 5 + 32
    return fahr

def function2( celsius ):
    fahr = function1( celsius ) 
    return int( round( fahr ) )

def function3( phoneString ):
    phoneString = phoneString.strip()
    index = phoneString.find( "(" )
    return phoneString[ index+5: ].strip() 

def function4( phoneString ):
    index = phoneString.find( "(" )
    beginningString = phoneString[0:index]
    endString = phoneString[index+3+1+1: ]
    return beginningString + "(XXX)" + endString

def function5( line ):
    index = line.find( "990" )
    beginningString = line[0:index]
    endString       = line[index+3+6: ]
    return beginningString + "990123456" + endString

def function6( fileName ):
    file = open( fileName, "r" )
    lines = file.readlines()
    file.close()
    return lines[0].rstrip()
    
def function7( fileName ):
    file = open( fileName, "r" )
    lines = file.readlines()
    file.close()

    return lines[-1].rstrip()

def function8( fileName ):
    file = open( fileName, "r" )
    text = file.read()
    file.close()

    text = text.replace( "g", "gremlin" )
    text = text.replace( "G", "Gremlin" )

    return text

def function9( farm ):
    newFarm = []
    for i in range( 0, len( farm ), 2 ):
        newFarm.append( farm[ i ] )
    return newFarm

def function10( temps ):
    count = 0
    for temp in temps:
        if temp >= 32:
           count = count + 1
    return count

def function11( temps ):
    newTemps = []
    for temp in temps:
        if temp >= 32:
            newTemps.append( temp )

    return newTemps

def function12( grade ):
    if ( grade >= 90 ): return "Excellent" 
    if ( grade >= 75 ): return "Very good"
    if ( grade >= 60 ): return "Average"
    if ( grade >= 50 ): return "Below average" 
    if ( grade >= 40 ): return "Problematic"
    return "Fail"

def main():
    # testing function0() with 3 different strings
    for name in [ "ARTHUR", "  smith COLLEGE  ", " lei   " ]:
        newName = function0( name )
        print( name )
        print( newName )
    print()


if __name__=="__main__":
   main()




...