Difference between revisions of "CSC111 Homework 7 2018"
(→How many times each character refers to one another) |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 16:06, 25 March 2018 (EDT) | [[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 16:06, 25 March 2018 (EDT) | ||
---- | ---- | ||
− | + | <onlydft> | |
<br /> | <br /> | ||
__TOC__ | __TOC__ | ||
Line 78: | Line 78: | ||
: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. | :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. | ||
+ | <br /> | ||
+ | :Note that if Stanley says "Blanche" twice in a line, your program should count this as +2 for Stanley -> Blanche. | ||
<br /> | <br /> | ||
==Who refers to another character most?== | ==Who refers to another character most?== | ||
<br /> | <br /> | ||
− | * Your program should output the name of the character who refers to one of the other two characters the most. In this case | + | * Your program should output the name of the character who refers to one of the other two characters the most. In this case Blanche makes 63 references to Stella. In this case your program would output: |
<br /> | <br /> | ||
::<source lang="text"> | ::<source lang="text"> | ||
− | + | Blanche -Most-> Stella | |
</source> | </source> | ||
<br /> | <br /> | ||
Line 95: | Line 97: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
==Output Examples== | ==Output Examples== | ||
<br /> | <br /> | ||
Line 102: | Line 105: | ||
File name? streetcar.txt | File name? streetcar.txt | ||
− | Stanley -> Blanche = | + | Stanley -> Blanche = 18 |
− | Stanley -> Stella = | + | Stanley -> Stella = 16 |
− | Blanche -> Stella = | + | Blanche -> Stella = 63 |
− | Blanche -> Stanley = | + | Blanche -> Stanley = 15 |
− | Stella -> Stanley = | + | Stella -> Stanley = 26 |
− | Stella -> Blanche = | + | Stella -> Blanche = 58 |
− | + | Blanche -Most-> Stella | |
</source> | </source> | ||
<br /> | <br /> | ||
Line 179: | Line 182: | ||
def isInside( x, y, x1, y1, x2, y2 ): | def isInside( x, y, x1, y1, x2, y2 ): | ||
+ | '''returns True if the point (x,y) is inside the rectangle | ||
+ | defined by (x1,y1) (top-left) and (x2,y2) (bottom-right)''' | ||
return False | return False | ||
Line 225: | Line 230: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
==Submission to Moodle== | ==Submission to Moodle== | ||
<br /> | <br /> | ||
Line 232: | Line 238: | ||
− | + | </onlydft> | |
<!-- ======================================================== --> | <!-- ======================================================== --> | ||
− | <showafterdate after="20180407 00:00" before=" | + | <showafterdate after="20180407 00:00" before="20180601 00:00"> |
<br /> | <br /> | ||
=Solution Programs= | =Solution Programs= | ||
Line 269: | Line 275: | ||
# characters. | # characters. | ||
if line.find( character )!= -1 and line.find( name ) != -1: | if line.find( character )!= -1 and line.find( name ) != -1: | ||
− | count = count + | + | count = count + line.count( name ) |
# return the counter. | # return the counter. | ||
Line 281: | Line 287: | ||
# get the count of the number of references by one character to | # get the count of the number of references by one character to | ||
# the other. | # the other. | ||
− | StanleyCallsBlanche = getReferences( lines, "STANLEY", "Blanche" ) | + | StanleyCallsBlanche = getReferences( lines, "STANLEY:", "Blanche" ) |
− | StanleyCallsStella = getReferences( lines, "STANLEY", "Stella" ) | + | StanleyCallsStella = getReferences( lines, "STANLEY:", "Stella" ) |
− | BlancheCallsStella = getReferences( lines, "BLANCHE", "Stella" ) | + | BlancheCallsStella = getReferences( lines, "BLANCHE:", "Stella" ) |
− | BlancheCallsStanley = getReferences( lines, "BLANCHE", "Stanley" ) | + | BlancheCallsStanley = getReferences( lines, "BLANCHE:", "Stanley" ) |
− | StellaCallsStanley = getReferences( lines, "STELLA", "Stanley" ) | + | StellaCallsStanley = getReferences( lines, "STELLA:", "Stanley" ) |
− | StellaCallsBlanche = getReferences( lines, "STELLA", "Blanche" ) | + | StellaCallsBlanche = getReferences( lines, "STELLA:", "Blanche" ) |
# display the result. | # display the result. |
Latest revision as of 13:43, 1 June 2018
D. Thiebaut (talk) 16:06, 25 March 2018 (EDT)
<showafterdate after="20180407 00:00" before="20180601 00:00">
Solution Programs
Streetcar
# hw7sol.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 + line.count( name ) # 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()
Problem 2
# hw7_2.py # your names # solution program for Hw7 Problem 2. def medianOf( a, b, c ): if a <= b <= c or c <= b <= a: return b if b <= a <= c or c <= a <= b: return a return c def isInside( x, y, x1, y1, x2, y2 ): if medianOf( x, x1, x2 ) == x and medianOf( y, y1, y2 )==y: return True return False def isContained( list1, list2 ): for item in list1: if not item in list2: return False return True def makeAbsolute( list1 ): for i in range( len( list1 ) ): list1[i] = abs( list1[i] ) return list1 def numberCommonItems( list1, list2 ): count = 0 for item in list1: if item in list2: count = count + 1 return count def main(): print( medianOf( 10, 20, 15 ) ) print( medianOf( 10, 9, 5 ) ) print( isInside( 50, 50, 20, 20, 100, 200 ) ) print( isInside( 50, 50, 20, 100, 100, 30 ) ) print( isInside( 10, 10, 50, 50, 100, 100 ) ) print( isContained( ["Smith", 10, 20], ["College", 10, 30] ) ) print( isContained( [1, 10, 5], [1, 2, 5, 9, 10, 15] ) ) print( isContained( [1, 10], [1, 2, 5] ) ) print( isContained( [1, 10], [20, 100] ) ) print( makeAbsolute( [1, -10, 2, -100, 5, 7] ) ) print( numberCommonItems( [1, 2, 5], ["Smith", 5, 10, 2] ) ) print( numberCommonItems( [1, 2, 3], [4, 5] ) ) main()
</showafterdate>