CSC111 Homework 8 2015b

From dftwiki3
Revision as of 10:55, 27 October 2015 by Thiebaut (talk | contribs)
Jump to: navigation, search

--D. Thiebaut (talk) 10:55, 27 October 2015 (EDT)


<showafterdate after="20151029 13:00" before="20151231 00:00">

Streetcar



</showafterdate>


<showafterdate after="20151107 00:00" before="20151231 00:00">

Solution Programs


Streetcar


# hw8sol.py
# D. Thiebaut
# This program reads a file containing a portion (or all)
# of the dialogs from the play Streetcar named Desire, by
# Tennessee Williams, and outputs statistics about how often
# the main three characters refer to each other.

def getLinesFromFile( fileName ):
    """Open the file whose name is passed as a parameter,
    and returns the lines it contains.
    """
    return open( fileName, "r" ).readlines()

def getReferences( lines, character, name ):
    """
    Given the lines containing the dialog, the character
    of interest (BLANCHE, STANLEY, or STELLA), and the
    character they might be calling or talking about (Blanche,
    Stanley, or Stella), return the number of times such
    references occur.
    """
    count = 0
    # for each line of text
    for line in lines:
        # if STANLEY is the character, and Stella appears in his
        # line, increment the counter.  Same for the other
        # characters.
        if line.find( character )!= -1 and line.find( name ) != -1:
            count = count + 1

    # return the counter.
    return count
         
def main():
    # get the name of the file from the user.
    fileName = input( "File name? " )
    lines = getLinesFromFile( fileName )

    # get the count of the number of references by one character to
    # the other.
    StanleyCallsBlanche = getReferences( lines, "STANLEY", "Blanche" )
    StanleyCallsStella  = getReferences( lines, "STANLEY", "Stella" )
    BlancheCallsStella  = getReferences( lines, "BLANCHE", "Stella" )
    BlancheCallsStanley = getReferences( lines, "BLANCHE", "Stanley" )
    StellaCallsStanley  = getReferences( lines, "STELLA", "Stanley" )
    StellaCallsBlanche  = getReferences( lines, "STELLA", "Blanche" )

    # display the result.
    print( "Stanley -> Blanche = ", StanleyCallsBlanche )
    print( "Stanley -> Stella  = ", StanleyCallsStella )
    print( "Blanche -> Stella  = ", BlancheCallsStella )
    print( "Blanche -> Stanley = ", BlancheCallsStanley )
    print( "Stella -> Stanley  = ", StellaCallsStanley )
    print( "Stella -> Blanche  = ", StellaCallsBlanche )

    # create a list of the 6 counters and sort them in
    # increasing order.
    list = [StanleyCallsBlanche, StanleyCallsStella, BlancheCallsStella,
            BlancheCallsStanley, StellaCallsStanley, StellaCallsBlanche]
    list.sort()

    # The highest counter is at the end of the list
    most = list[-1]

    # display the character to references another the most,
    # or a special message if no references were found.
    if most == 0:
        print( "No references" )
    elif StanleyCallsBlanche==most:
        print( "Stanley -Most-> Blanche" )
    elif StanleyCallsStella==most:
        print( "Stanley -Most-> Stella" )
    elif BlancheCallsStella==most:
        print( "Blanche -Most-> Stanley" )
    elif BlancheCallsStanley==most:
        print( "Blanche -Most-> Stanley" )
    elif StellaCallsStanley==most:
        print( "Stella -Most-> Stanley" )
    elif StellaCallsBlanche==most:
        print( "Stella -Most-> Blanche" )
    
        
main()

</showafterdate>