Difference between revisions of "CSC111 Homework 7 2018"

From dftwiki3
Jump to: navigation, search
(Created page with "~~~~ ---- <br /> __TOC__ <br /> <bluebox> This assignment is due on Thursday night, 4/5/18, at 11:55 p.m. </bluebox> <br /> =Problem 1: Streetcar= <br /> Write a program call...")
 
 
(11 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__
 
<br />
 
<br />
 
<bluebox>
 
<bluebox>
This assignment is due on Thursday night, 4/5/18, at 11:55 p.m.
+
This assignment is due on Thursday night, 4/5/18, at 11:55 p.m.  You can work on it individually, or in a programming pair.  If you do work
 +
in pair, make sure both of you and your partner submit a copy of the program, and that the header contains your two names at the top.
 +
<br />
 +
In this assignment, you will write a program that processes the play ''A Street Car Name Desire,'' by Tennessee Williams.  
 
</bluebox>
 
</bluebox>
 
<br />
 
<br />
 
=Problem 1: Streetcar=
 
=Problem 1: Streetcar=
 
<br />
 
<br />
 +
==Getting a copy of Streetcar==
 +
<br />
 +
Create the following program in a folder of your choice.
 +
<br />
 +
::<source lang="python">
 +
# getStreetCar.py
 +
# D. Thiebaut
 +
# Fetches a text file from a Web site and save it to file
 +
 +
from urllib.request import urlopen  # library for fetching
 +
                                    # Web pages
 +
   
 +
def main():
 +
    # the URL of the text file
 +
    URL = "http://cs.smith.edu/~dthiebaut/classes/111_2015s/streetcarShort.txt"
 +
 +
    # open the URL
 +
    response = urlopen(URL)
 +
 +
    # get its contents, and store it in a string called text
 +
    text = response.read().decode('utf-8')
 +
 +
    # save the string to file
 +
    open( "streetcarShort.txt", "w" ).write( text )
 +
 +
 +
main()
 +
</source>
 +
<br />
 +
* Run the program.  Verify that after it has run, you have a new file called streetcarShort.txt in your folder.
 +
* Look at the file with Notepad or TextEdit, depending on your operating system.
 +
* Verify that you read the dialog from a play, involving Blanche and Stella, two of the main protagonists.
 +
* It is much simpler to debug your program on a shorter version of the play than on the real play, which is much longer.
 +
* At the end, when you have fully debugged your program, you may want to debug it on the full play.  To obtain a copy of it,
 +
replace "streetcarShort.txt" by "streetcar.txt" in the two python strings of the program above, and run it again.  You will
 +
end up with a new file in your folder, named ''streetcar.txt'' that will contain the whole play.
 +
<br />
 +
==Your Assignment==
 
Write a program called '''hw7_1.py''' that will:
 
Write a program called '''hw7_1.py''' that will:
 
:# ask the user for a file name (the file should reside in the same directory where your program is saved).
 
:# ask the user for a file name (the file should reside in the same directory where your program is saved).
Line 20: Line 61:
 
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 refers to the other characters==
+
==How many times each character refers to one another==
 
<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.
Line 27: Line 68:
 
<br />
 
<br />
 
::<source lang="text">
 
::<source lang="text">
Stanley -> Blanche = 16
+
Stanley -> Blanche = 18
Stanley -> Stella  = 15
+
Stanley -> Stella  = 16
Blanche -> Stella  = 47
+
Blanche -> Stella  = 63
Blanche -> Stanley = 14
+
Blanche -> Stanley = 15
Stella -> Stanley  = 26
+
Stella -> Stanley  = 26
Stella -> Blanche  = 56
+
Stella -> Blanche  = 58
 
</source>
 
</source>
 
<br />
 
<br />
Line 38: Line 79:
 
: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 />
 
<br />
 +
:Note that if Stanley says "Blanche" twice in a line, your program should count this as +2 for Stanley -&gt; Blanche.
 +
<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 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 Blanche makes 63 references to Stella.  In this case your program would output:
 
<br />
 
<br />
  
 
::<source lang="text">
 
::<source lang="text">
Stella -Most-> Blanche
+
Blanche -Most-> Stella
 
</source>
 
</source>
 
<br />
 
<br />
Line 53: Line 97:
 
</source>
 
</source>
 
<br />
 
<br />
 +
 
==Output Examples==
 
==Output Examples==
 
<br />
 
<br />
Line 60: Line 105:
 
File name? streetcar.txt
 
File name? streetcar.txt
  
Stanley -> Blanche = 16
+
Stanley -> Blanche = 18
Stanley -> Stella  = 15
+
Stanley -> Stella  = 16
Blanche -> Stella  = 47
+
Blanche -> Stella  = 63
Blanche -> Stanley = 14
+
Blanche -> Stanley = 15
Stella -> Stanley  = 26
+
Stella -> Stanley  = 26
Stella -> Blanche  = 56
+
Stella -> Blanche  = 58
Stella -Most-> Blanche
+
Blanche -Most-> Stella
 
</source>
 
</source>
 
<br />
 
<br />
Line 137: 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 183: Line 230:
 
</source>
 
</source>
 
<br />
 
<br />
 +
 
==Submission to Moodle==
 
==Submission to Moodle==
 
<br />
 
<br />
Line 188: Line 236:
 
* 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.
 
* 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 />
 
<br />
</showafterdate>
 
  
  
 +
</onlydft>
 
<!-- ======================================================== -->
 
<!-- ======================================================== -->
<showafterdate after="20151107 00:00" before="20151231 00:00">
+
<showafterdate after="20180407 00:00" before="20180601 00:00">
 
<br />
 
<br />
 
=Solution Programs=
 
=Solution Programs=
Line 227: 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 + 1
+
             count = count + line.count( name )
  
 
     # return the counter.
 
     # return the counter.
Line 239: 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>