Difference between revisions of "CSC111 Homework 8 2015b"
(13 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
<showafterdate after="20151029 13:00" before="20151231 00:00"> | <showafterdate after="20151029 13:00" before="20151231 00:00"> | ||
<br /> | <br /> | ||
− | =Streetcar= | + | __TOC__ |
+ | <br /> | ||
+ | <bluebox> | ||
+ | This assignment is due on Thursday night, 11/5/15, at 11:55 p.m. | ||
+ | </bluebox> | ||
+ | <br /> | ||
+ | =Problem 1: Streetcar= | ||
<br /> | <br /> | ||
Write a program called '''hw8_1.py''' that will: | Write a program called '''hw8_1.py''' that will: | ||
Line 14: | Line 20: | ||
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. | 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. | ||
<br /> | <br /> | ||
− | ==How many times each character | + | ==How many times each character refers to the other characters== |
<br /> | <br /> | ||
* 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. | * 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: | * 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: | ||
− | + | <br /> | |
::<source lang="text"> | ::<source lang="text"> | ||
Stanley -> Blanche = 16 | Stanley -> Blanche = 16 | ||
Line 28: | Line 34: | ||
Stella -> Blanche = 56 | Stella -> Blanche = 56 | ||
</source> | </source> | ||
+ | <br /> | ||
: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. | ||
Line 34: | Line 41: | ||
<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 Stella makes 56 references to Blanche. In this case your program would output: | * 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: | ||
+ | <br /> | ||
::<source lang="text"> | ::<source lang="text"> | ||
Stella -Most-> Blanche | Stella -Most-> Blanche | ||
</source> | </source> | ||
+ | <br /> | ||
+ | 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: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | No references | ||
+ | </source> | ||
+ | <br /> | ||
+ | ==Output Examples== | ||
+ | <br /> | ||
+ | ===Processing the Whole Play=== | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | 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 | ||
+ | </source> | ||
+ | <br /> | ||
+ | ===Processing A Sample of the Lines=== | ||
+ | <br /> | ||
+ | Processing a different text file containing only the following lines: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | STELLA: Don't holler at me like that. Hi, Mitch. | ||
+ | STANLEY: Bowling! | ||
+ | BLANCHE: At Elysian Fields? | ||
+ | </source> | ||
+ | <br /> | ||
+ | yields the following interaction: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | File name? streetcar0.txt | ||
+ | Stanley -> Blanche = 0 | ||
+ | Stanley -> Stella = 0 | ||
+ | Blanche -> Stella = 0 | ||
+ | Blanche -> Stanley = 0 | ||
+ | Stella -> Stanley = 0 | ||
+ | Stella -> Blanche = 0 | ||
+ | No references | ||
+ | </source> | ||
+ | <br /> | ||
+ | ==Moodle Submission== | ||
+ | <br /> | ||
+ | Submit your program on Moodle, in the HW 8 PB 1 section. | ||
+ | <br /> | ||
+ | ==Video Hints== | ||
+ | <br /> | ||
+ | <center> | ||
+ | <videoflash>ka2Ic2dTxWw</videoflash> | ||
+ | </center> | ||
+ | <br /> | ||
+ | <!-- ======================================================== --> | ||
+ | =Problem 2= | ||
+ | <br /> | ||
+ | * Write a program called '''hw8_2.py''' that contains 5 functions, that compute the following quantities: | ||
+ | ::* Function 1: '''medianOf( a, b, c )''' | ||
+ | ::: returns the median of 3 numbers, i.e. the number that is between the other 2. For example, the median of 3, 1, 5 is 3. The median of 1, 20, 5 is 5. The median of 2, 2, 4 is 2. | ||
+ | <br /> | ||
+ | ::* Function 2: '''isInside( x, y, x1, y1, x2, y2 )''' | ||
+ | ::: returns True if the graphics point defined by the position x, y, is '''inside''' the rectangle defined by two corners positions x1, y1 for one of them, and x2, y2 for the other. It returns False otherwise. The two points can be the top-left and bottom-right corners, or the bottom-left and top-right corners. | ||
+ | <br /> | ||
+ | ::* Function 3: '''isContained( list1, list2 )''' | ||
+ | ::: returns True if '''all''' the elements of list1 can be found in list2. It returns False otherwise. | ||
+ | <br /> | ||
+ | ::* Function 4: '''makeAbsolute( list1 )''' | ||
+ | ::: We assume that the list it receives will always be a list of numbers. It returns the list where the negative numbers have been made positive. | ||
+ | <br /> | ||
+ | ::* Function 5: '''numberCommonItems( list1, list2 )''' | ||
+ | ::: returns the number of items list1 and list2 have in common. | ||
+ | <br /> | ||
+ | ==Skeleton Program== | ||
+ | <br /> | ||
+ | * You may want to start with the skeleton below, and fill in the functions. Note that the functions receive all the variables they need through the parameters that are passed between parentheses, and not via input statement. There should be '''no input statements''' in your program. | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # hw8_2.py | ||
+ | # your names | ||
+ | # Skeleton program for Hw8 Problem 2. | ||
+ | # Replace the body of the functions with code that will make | ||
+ | # them compute the various quantities they're supposed to compute. | ||
+ | |||
+ | def medianOf( a, b, c ): | ||
+ | return a | ||
+ | |||
+ | def isInside( x, y, x1, y1, x2, y2 ): | ||
+ | return False | ||
+ | |||
+ | def isContained( list1, list2 ): | ||
+ | return True | ||
+ | |||
+ | def makeAbsolute( list1 ): | ||
+ | return list1 | ||
+ | |||
+ | def numberCommonItems( list1, list2 ): | ||
+ | return 0 | ||
+ | |||
+ | 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() | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | * If your functions are written correctly, the output of your program should be: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | 15 | ||
+ | 9 | ||
+ | True | ||
+ | True | ||
+ | False | ||
+ | False | ||
+ | True | ||
+ | False | ||
+ | False | ||
+ | [1, 10, 2, 100, 5, 7] | ||
+ | 2 | ||
+ | 0 | ||
+ | </source> | ||
+ | <br /> | ||
+ | ==Submission to Moodle== | ||
+ | <br /> | ||
+ | * Submit your code to Moodle, in the Section HW 8 PB 2 section. | ||
+ | * Note that Moodle will remove the main() function from your program and replace with a different main function, that will test your function with cases different from the one shown here. | ||
+ | <br /> | ||
</showafterdate> | </showafterdate> | ||
Line 133: | Line 281: | ||
+ | main() | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | =Problem 2= | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | # hw8_2.py | ||
+ | # your names | ||
+ | # solution program for Hw8 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() | main() | ||
Latest revision as of 12:49, 2 November 2015
--D. Thiebaut (talk) 10:55, 27 October 2015 (EDT)
<showafterdate after="20151029 13:00" before="20151231 00:00">
This assignment is due on Thursday night, 11/5/15, at 11:55 p.m.
Problem 1: 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
Moodle Submission
Submit your program on Moodle, in the HW 8 PB 1 section.
Video Hints
Problem 2
- Write a program called hw8_2.py that contains 5 functions, that compute the following quantities:
- Function 1: medianOf( a, b, c )
- returns the median of 3 numbers, i.e. the number that is between the other 2. For example, the median of 3, 1, 5 is 3. The median of 1, 20, 5 is 5. The median of 2, 2, 4 is 2.
- Function 2: isInside( x, y, x1, y1, x2, y2 )
- returns True if the graphics point defined by the position x, y, is inside the rectangle defined by two corners positions x1, y1 for one of them, and x2, y2 for the other. It returns False otherwise. The two points can be the top-left and bottom-right corners, or the bottom-left and top-right corners.
- Function 3: isContained( list1, list2 )
- returns True if all the elements of list1 can be found in list2. It returns False otherwise.
- Function 4: makeAbsolute( list1 )
- We assume that the list it receives will always be a list of numbers. It returns the list where the negative numbers have been made positive.
- Function 5: numberCommonItems( list1, list2 )
- returns the number of items list1 and list2 have in common.
Skeleton Program
- You may want to start with the skeleton below, and fill in the functions. Note that the functions receive all the variables they need through the parameters that are passed between parentheses, and not via input statement. There should be no input statements in your program.
# hw8_2.py # your names # Skeleton program for Hw8 Problem 2. # Replace the body of the functions with code that will make # them compute the various quantities they're supposed to compute. def medianOf( a, b, c ): return a def isInside( x, y, x1, y1, x2, y2 ): return False def isContained( list1, list2 ): return True def makeAbsolute( list1 ): return list1 def numberCommonItems( list1, list2 ): return 0 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()
- If your functions are written correctly, the output of your program should be:
15 9 True True False False True False False [1, 10, 2, 100, 5, 7] 2 0
Submission to Moodle
- Submit your code to Moodle, in the Section HW 8 PB 2 section.
- Note that Moodle will remove the main() function from your program and replace with a different main function, that will test your function with cases different from the one shown here.
</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()
Problem 2
# hw8_2.py # your names # solution program for Hw8 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>