Difference between revisions of "CSC111 Homework 8 2015b"
Line 70: | Line 70: | ||
STELLA: Don't holler at me like that. Hi, Mitch. | STELLA: Don't holler at me like that. Hi, Mitch. | ||
STANLEY: Bowling! | STANLEY: Bowling! | ||
− | BLANCHE:At Elysian Fields? | + | BLANCHE: At Elysian Fields? |
</source> | </source> | ||
<br /> | <br /> |
Revision as of 17:24, 27 October 2015
--D. Thiebaut (talk) 10:55, 27 October 2015 (EDT)
<showafterdate after="20151029 13:00" before="20151231 00:00">
Contents
Streetcar
Write a program called hw8_1.py that will:
- ask the user for a file name (the file should reside in the same directory where your program is saved).
- print a blank line after receiving the file name.
- open the text file with that name (we'll assume that both the program and the file reside in the same folder).
- read the file and compute various quantities (see below).
- display the quantities computed.
For simplicity, for this assignment, the file will be either the full contents of the play A Streetcar Named Desire, or will be a selection of lines from this file. This means that you can be sure that Blanche, Stella, and Stanley will be some of the characters. It is possible that the title is included in the file, in which case some lines may not start with the name of a character.
How many times each character refers to the other characters
- One way to study the dynamics between the characters, besides reading the play, is to generate some analytics for the play. In this assignment we will count how many times each character refers to the other characters. In particular, your program should count how many times Stella mentions Blanche, and how many times she mentions Stanley. Similarly, your program will count how many times Blanche mentions Stella, and how many times she mentions Stanley. And the same for Stanley, counting how many times he says the word Blanche, or the word Stella.
- Your output should look exactly like what is shown below, but, of course, the numbers might be different, depending on whether the file is the whole play, or just a few lines from the play:
Stanley -> Blanche = 16 Stanley -> Stella = 15 Blanche -> Stella = 47 Blanche -> Stanley = 14 Stella -> Stanley = 26 Stella -> Blanche = 56
- In the output shown above, the first line indicates that Stanley says the word "Blanche" 16 times in the file that was provided to the program. Stanley says "Stella" 15 times. Stella pronounces "Blanche"'s name 56 times.
Who refers to another character most?
- Your program should output the name of the character who refers to one of the other two characters the most. In this case Stella makes 56 references to Blanche. In this case your program would output:
Stella -Most-> Blanche
Note that if the lines given to your program do not contain references from one character to another, then your program should output 0 for the 6 counters, and the line "No references" to indicate that nobody is referencing more than anybody else:
No references
Output Examples
Processing the Whole Play
File name? streetcar.txt Stanley -> Blanche = 16 Stanley -> Stella = 15 Blanche -> Stella = 47 Blanche -> Stanley = 14 Stella -> Stanley = 26 Stella -> Blanche = 56 Stella -Most-> Blanche
Processing A Sample of the Lines
Processing a different text file containing only the following lines:
STELLA: Don't holler at me like that. Hi, Mitch. STANLEY: Bowling! BLANCHE: At Elysian Fields?
yields the following interaction:
File name? streetcar0.txt Stanley -> Blanche = 0 Stanley -> Stella = 0 Blanche -> Stella = 0 Blanche -> Stanley = 0 Stella -> Stanley = 0 Stella -> Blanche = 0 No references
</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>