CSC111 Programming Examples Week 6

From dftwiki3
Revision as of 13:45, 4 March 2015 by Thiebaut (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 12:43, 4 March 2015 (EST)


# exercises.py
# D. Thiebaut
# Solution functions for Class Exercises
# (see lecture notes for Week 6
#
def stripState( townState ):

    """
    # SOLUTION #1
    # take townState, and remove the state part
    # Solution 1
    townNoState = townState.split( ',' )
    #print( "townNoState = ", townNoState )
    townNoState = townNoState[ 0 ]
    return townNoState
    """
    
    
    #SOLUTION 2
    # find where the comma is located
    index = townState.find( "," )

    # slice the string and keep only the part up to, but not including the
    # comma
    townNoState = townState[ 0: index ]
    
    return townNoState

def anonymizeBox( address ):
    #find the open-bracket character [
    index1 = address.find( "[" )

    #find the close-bracket character ]
    index2 = address.find( "]" )
    
    """
    # SOLUTION 1
    # capture number
    
    boxNumber = address[index1+1:index2]
    #print( "boxNumber = ", boxNumber )

    # replace box number by "XXXX"
    newAddress = address.replace( boxNumber, "XXXX" )
    """

    
    """
    # SOLUTION 2
    firstPart = address[0:index1+1]
    lastPart  = address[index2: ]
    newAddress = firstPart + "XXXX" + lastPart
    
    return newAddress
    """
    
    """
    # SOLUTION 3
    newAddress = address.replace( "0", "X" ).replace( "1", "X" )
    newAddress = newAddress.replace( "2", "X" ).replace( "3", "X" )
    newAddress = newAddress.replace( "4", "X" ).replace( "5", "X" )
    newAddress = newAddress.replace( "6", "X" ).replace( "7", "X" )
    newAddress = newAddress.replace( "8", "X" ).replace( "9", "X" )

    return newAddress
    """

    """
    # SOLUTION 4
    newAddress = address
    for digit in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]:
        newAddress = newAddress.replace( digit, "X" )
        
    return newAddress
    """

    # SOLUTION 5
    for char in address:
        if char in "0123456789":
            address = address.replace( char, "X" )
    return address


# firstAndLast: assumes that the line that is passed as a parameter
# contains at least one comma.  It splits the line on the commas, and
# returns a list made of the first and last words only.
def firstAndLast( line ):
    list = line.split( "," )
    lineFirstLast = [ list[0], list[-1] ]

    return lineFirstLast

def writePoem( fileName ):
    file = open( fileName, "w" )
    file.write( "The quick\nred fox\njumped over\nthe brown\nsleeping\ndog.\n" )
    file.close()

# given a file name, opens it, reads it as a list of lines,
# and returns the last line only.
def lastLine( fileName ):
    file = open( fileName, "r" )
    lines = file.readlines()
    file.close()

    return lines[-1]

# given a list of animals, returns 3 of them, that are at
# Indexes 0, 2, and 4.
#
def get024( animals ):
    newFarm = []
    for i in range( 0, 6, 2 ):
        #print( i )
        newFarm.append( animals[i] )
        
    return newFarm


def main():
    # test stripState 
    print( stripState( "northampton, Massachusetts" ) )
    print( stripState( "Amherst, VA" ) )
    town = stripState( "LA, CA" )
    print( "town = ", town )

    # test anonymizeBox
    student = anonymizeBox( "Maria Clara, Box [3949], Smith, Northampton" )
    print( student )

    # test lastLine
    writePoem( "poem.txt" )
    print( "last line of poem.txt =", lastLine( "poem.txt" ) )

    # test firtAndLast
    line = "Knox, Maria, Smith College, 2017"
    words = firstAndLast( line )
    print( words )
    print( words[0], words[1] )
    
    # test get024
    farm = ["dog", "cat", "horse", "fly", "goat", "pig" ]
    newFarm = get024( farm )    
    print( newFarm )

    
main()