Difference between revisions of "CSC111 Homework 7 2018"

From dftwiki3
Jump to: navigation, search
(Who refers to another character most?)
Line 105: 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 />

Revision as of 13:18, 28 March 2018

D. Thiebaut (talk) 16:06, 25 March 2018 (EDT)




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.
In this assignment, you will write a program that processes the play A Street Car Name Desire, by Tennessee Williams.


Problem 1: Streetcar


Getting a copy of Streetcar


Create the following program in a folder of your choice.

# 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()


  • 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.

Your Assignment

Write a program called hw7_1.py that will:

  1. ask the user for a file name (the file should reside in the same directory where your program is saved).
  2. print a blank line after receiving the file name.
  3. open the text file with that name (we'll assume that both the program and the file reside in the same folder).
  4. read the file and compute various quantities (see below).
  5. 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 one another


  • 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 = 18
Stanley -> Stella  = 16
Blanche -> Stella  = 63
Blanche -> Stanley = 15
Stella -> Stanley  = 26
Stella -> Blanche  = 58


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.


Note that if Stanley says "Blanche" twice in a line, your program should count this as +2 for Stanley -> Blanche.


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 Blanche makes 63 references to Stella. In this case your program would output:


Blanche -Most-> Stella


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 = 18
Stanley -> Stella  = 16
Blanche -> Stella  = 63
Blanche -> Stanley = 15
Stella -> Stanley  = 26
Stella -> Blanche  = 58
Blanche -Most-> Stella


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 7 PB 1 section.

Video Hints



Problem 2


  • Write a program called hw7_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.


# hw7_2.py
# your names
# Skeleton program for Hw7 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 7 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 after="20180407 00:00" before="20180606 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 + 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


# 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>