Difference between revisions of "CSC111 Homework 5 2015"

From dftwiki3
Jump to: navigation, search
Line 312: Line 312:
 
* This program is a very simplified form of mail-merge, a technique where one creates a form letter with place holders for various strings, such as first name and last name, and an file containing the first names and last names of many people.  The mail-merge program creates many different copies of the form letter, replacing the place-holders with the different first and last names found in the name file.
 
* This program is a very simplified form of mail-merge, a technique where one creates a form letter with place holders for various strings, such as first name and last name, and an file containing the first names and last names of many people.  The mail-merge program creates many different copies of the form letter, replacing the place-holders with the different first and last names found in the name file.
 
<br />
 
<br />
 +
=Problem 6=
 +
<br />
 +
This problem was suggested by Dave Marshall.  Great extension of the previous problems.
 +
<br />
 +
* Write a program called hw5_6.py that reads two text files, and prints a series of messages on the screen.
 +
* Your program will not ask the user for the file names.  Instead it will always assume that there is a file called '''users.txt''' and a file called '''message.txt''' in the same directory where your program resides.
 +
* The '''users.txt''' file contains lines of text.  Each line contains 3 words separated by spaces.  The first word is a name, the second is a thing, and the third is a date.
 +
<br />
 +
:;Example
 +
<br />
 +
::<source lang="text">
 +
Dave chocolate 02152015
 +
Carl toothpaste 02152020
 +
Gru superglue 01012100
 +
</source>
 +
<br />
 +
* The '''message.txt''' file contains one or several lines of text.  In the text, there will be three special keywords prefixed with a $-sign: "$name", "$stuff", and "$date".
 +
<br />
 +
:;Example
 +
<br />
 +
::<source lang="text">
 +
Dear $name, this is a coupon for
 +
a ton of $stuff, which you can pick up
 +
in Ford Hall 241 at any time before $date.</source>
 +
<br />
 +
* Your program will read each line of '''users.txt''', and divide it into separate words.  It will take the first word and do a replacement of "$name" by this word in the message, then it will take the second word and replace "$stuff" by it in the message, and finally it will take the third word and replace "$date" by it.  So, assuming that the first line of '''users.txt''' is <tt>Dave chocolate 02152015</tt>, your program will output:
 +
<br />
 +
::<source lang="text">
 +
Dear Dave, this is a coupon for
 +
a ton of chocolate, which you can pick up
 +
in Ford Hall 241 at any time before 02152015.
 +
</source>
 +
<br />
 +
* The full output of your program, if '''users.txt''' contains the 3 lines shown above would be:
 +
<br />
 +
::<source lang="text">
 +
Dear Dave, this is a coupon for
 +
a ton of chocolate, which you can pick up
 +
in Ford Hall 241 at any time before 02152015.
 +
 +
Dear Carl, this is a coupon for
 +
a ton of toothpaste, which you can pick up
 +
in Ford Hall 241 at any time before 02152020.
 +
 +
Dear Gru, this is a coupon for
 +
a ton of superglue, which you can pick up
 +
in Ford Hall 241 at any time before 01012100.
 +
 +
</source>
 
</showafterdate>
 
</showafterdate>
 
<br />
 
<br />

Revision as of 10:02, 25 February 2015

--D. Thiebaut (talk) 09:55, 24 February 2015 (EST)




<showafterdate after="20150226 16:00" before="20150606 00:00">


This homework assignment is due on Tuesday March 3rd, at 11:55 p.m.

Problem #1


  • Write a program called hw5_1.py that asks the user for the name of a person to sing "Happy Birthday" to, and then displays the song in a box 52-characters wide.


Example output


Who should we sing for? Carl
+--------------------------------------------------+
|              Happy birthday to you!              |
|              Happy birthday to you!              |
|            Happy birthday, dear Carl!            |
|              Happy birthday to you!              |
+--------------------------------------------------+


Requirements


  • This should be the outline of your program:


# hw5_1.py
# your name
# short description
def happyBirthday():
    ...

def happyBirthdayDear( name ):
    ...

def singSong( name ):
    ...

def main():
    ...

main()


  • Make sure you use the same function names as shown above, with the same parameter name. The test script will look for the same format.


Submission to Moodle


  • Submit your program in the HW5 PB 1 section on Moodle.


Problem #2


Modify your program, while still keeping the same organization (with some modifications, see below), so that the box is tight around the song, as illustrated below:

Example output 1


Who should we sing for? Lu
+------------------------+
| Happy birthday to you! |
| Happy birthday to you! |
|Happy birthday, dear Lu!|
| Happy birthday to you! |
+------------------------+


Example output 2


Who should we sing for? Alexandra-Katarina
+----------------------------------------+
|         Happy birthday to you!         |
|         Happy birthday to you!         |
|Happy birthday, dear Alexandra-Katarina!|
|         Happy birthday to you!         |
+----------------------------------------+


Requirements


  • Your program should now have the following organization, where a new parameter is being passed to the functions. This parameter is the length of the line with the user name, inside the box.


# Hw5_2.py
# Your name
# short description

def happyBirthday( length ):
    ...

def happyBirthdayDear( name, length ):
    ...

def singSong( name, length ):
    ...

def main():
    ...

main()


Submission


  • Name your program hw5_2.py and submit it to Moodle, Section HW 5 PB 2.


Problem #3


Modify your previous program and make it accept several names as an input:

Example output


Who should we sing for? 
(you may enter several names separated by spaces)
> CARL Jorge GRU mINIONs
+--------------------------+
|  Happy birthday to you!  |
|  Happy birthday to you!  |
|Happy birthday, dear Carl!|
|  Happy birthday to you!  |
+--------------------------+

+---------------------------+
|   Happy birthday to you!  |
|   Happy birthday to you!  |
|Happy birthday, dear Jorge!|
|   Happy birthday to you!  |
+---------------------------+

+-------------------------+
|  Happy birthday to you! |
|  Happy birthday to you! |
|Happy birthday, dear Gru!|
|  Happy birthday to you! |
+-------------------------+

+-----------------------------+
|    Happy birthday to you!   |
|    Happy birthday to you!   |
|Happy birthday, dear Minions!|
|    Happy birthday to you!   |
+-----------------------------+


Requirements


  • Your program should have the same organization as that of your solution for Problem 2.


# Hw5_3.py
# Your name
# short description

def happyBirthday( length ):
    ...

def happyBirthdayDear( name, length ):
    ...

def singSong( name, length ):
    ...

def main():
    ...

main()


Submission


  • Name your program hw5_3.py and submit it to Moodle, Section HW 5 PB 3.


Problem #4


  • Variation on the same scheme. This time, your program will ask the user for the name of a text file that will always be located in the same directory where your program resides. This file will contain the names of several people, one per line. Your program will sing Happy Birthday to each one!
  • You may assume that the file will always be there, and that the user always spells it correctly. You may also assume that the file will always contain at least 1 name.
  • Note 1: you can easily create a text file with Idle. Just open a new window with Idle, enter 3 names on the top 3 lines, and save the file as "names.txt", for example. And voilà! You have a text file in your directory.
  • Note 2: I recommend that you read the contents of the file as a string, rather than a list of strings, and then .strip() this string to remove possible extra blank lines that you might have inadvertently created at the end of your text file.


Contents of File "names.txt"


Gru
Jorge


Example Output


File name? names.txt
+-------------------------+
|  Happy birthday to you! |
|  Happy birthday to you! |
|Happy birthday, dear Gru!|
|  Happy birthday to you! |
+-------------------------+

+---------------------------+
|   Happy birthday to you!  |
|   Happy birthday to you!  |
|Happy birthday, dear Jorge!|
|   Happy birthday to you!  |
+---------------------------+


Submission

  • Call your program hw5_4.py and submit it to the HW 5 PB 4 section on Moodle.


Problem #5


  • This time your program will ask for the names of two different text files.
  • The first one will contain some message, containing the string "zzzz". For example:


Congrats, zzzz, you are the best!


or


Happy birthday to you,
Happy birthday, dear zzzz!


  • The second file will contain a list of names, as in Problem 4. One name per line.
  • Your program will repeat the message stored in the first file for every body whose name is in the name file.


Output Example 1


Example of Message File


Happy birthday to you,
Happy birthday, dear zzzz!


Example of Name File


Gru
Dave


Example of Output


Message file? message.txt
Name file? names.txt

Happy birthday to you,
Happy birthday, dear Gru!

Happy birthday to you,
Happy birthday, dear Dave!



Output Example 2


Example of Message File


Hip hip, Hurray!  zzzz is the best!


Example of Name File


Gru
Dave


Example of Output


Message file? message.txt
Name file? names.txt

Hip hip, Hurray!  Gru is the best!

Hip hip, Hurray!  Dave is the best!


Requirements


  • You are required to use a main() function, and you are free to create additional functions or not.


Submission


  • Name your program hw5_5.py, and submit it to the Moodle HW 5 PB 5 section.


Note


  • This program is a very simplified form of mail-merge, a technique where one creates a form letter with place holders for various strings, such as first name and last name, and an file containing the first names and last names of many people. The mail-merge program creates many different copies of the form letter, replacing the place-holders with the different first and last names found in the name file.


Problem 6


This problem was suggested by Dave Marshall. Great extension of the previous problems.

  • Write a program called hw5_6.py that reads two text files, and prints a series of messages on the screen.
  • Your program will not ask the user for the file names. Instead it will always assume that there is a file called users.txt and a file called message.txt in the same directory where your program resides.
  • The users.txt file contains lines of text. Each line contains 3 words separated by spaces. The first word is a name, the second is a thing, and the third is a date.


Example


Dave chocolate 02152015
Carl toothpaste 02152020
Gru superglue 01012100


  • The message.txt file contains one or several lines of text. In the text, there will be three special keywords prefixed with a $-sign: "$name", "$stuff", and "$date".


Example


Dear $name, this is a coupon for
a ton of $stuff, which you can pick up
in Ford Hall 241 at any time before $date.


  • Your program will read each line of users.txt, and divide it into separate words. It will take the first word and do a replacement of "$name" by this word in the message, then it will take the second word and replace "$stuff" by it in the message, and finally it will take the third word and replace "$date" by it. So, assuming that the first line of users.txt is Dave chocolate 02152015, your program will output:


Dear Dave, this is a coupon for
a ton of chocolate, which you can pick up
in Ford Hall 241 at any time before 02152015.


  • The full output of your program, if users.txt contains the 3 lines shown above would be:


Dear Dave, this is a coupon for
a ton of chocolate, which you can pick up
in Ford Hall 241 at any time before 02152015.

Dear Carl, this is a coupon for
a ton of toothpaste, which you can pick up
in Ford Hall 241 at any time before 02152020.

Dear Gru, this is a coupon for
a ton of superglue, which you can pick up
in Ford Hall 241 at any time before 01012100.

</showafterdate>
<showafterdate after="20150304 00:00" before="20150601 00:00">

Solution Programs


# --------------------------------------------------------------------------------------
# Solutions for Hw #5 PB #1
# D. Thiebaut


def happyBirthday():
    length = 50
    print( "|" + "Happy birthday to you!".center( length ) + "|" )

def happyBirthdayDear( name ):
    length = 50
    line = "Happy birthday, dear " + name +"!"
    print( "|" + line.center( length ) + "|"  )

def singSong( name ):
    length = 50
    bar = "+" + "-"*length + "+"
    print( bar )
    happyBirthday()
    happyBirthday()
    happyBirthdayDear( name )
    happyBirthday()
    print( bar )

def main():
    name = input( "Who should we sing for? " )
    singSong( name )

main()

# --------------------------------------------------------------------------------------
# Solutions for Hw #5
# D. Thiebaut


def happyBirthday( length ):
    print( "|" + "Happy birthday to you!".center( length ) + "|" )

def happyBirthdayDear( name, length ):
    line = "Happy birthday, dear " + name +"!"
    print( "|" + line.center( length ) + "|"  )

def singSong( name, length ):
    bar = "+" + "-"*length + "+"
    print( bar )
    happyBirthday( length )
    happyBirthday( length )
    happyBirthdayDear( name, length )
    happyBirthday( length )
    print( bar )

def main():
    name = input( "Who should we sing for? " )
    longLine = "Happy birthday, dear " + name + "!"
    length   = len( longLine )
    singSong( name, length )

main()

# --------------------------------------------------------------------------------------
# Solutions for Hw #5 PB #3
# D. Thiebaut


def happyBirthday( length ):
    print( "|" + "Happy birthday to you!".center( length ) + "|" )

def happyBirthdayDear( name, length ):
    line = "Happy birthday, dear " + name +"!"
    print( "|" + line.center( length ) + "|"  )

def singSong( name, length ):
    bar = "+" + "-"*length + "+"
    print( bar )
    happyBirthday( length )
    happyBirthday( length )
    happyBirthdayDear( name, length )
    happyBirthday( length )
    print( bar )

def main():
    print( "Who should we sing for? " )
    names = input( "(you may enter several names separated by spaces)\n> " )

    for name in names.split( " " ):
        longLine = "Happy birthday, dear " + name + "!"
        length   = len( longLine )
        singSong( name, length )
        print()

main()

# --------------------------------------------------------------------------------------
# Solutions for Hw #5 Pb #4
# D. Thiebaut


def happyBirthday( length ):
    print( "|" + "Happy birthday to you!".center( length ) + "|" )

def happyBirthdayDear( name, length ):
    line = "Happy birthday, dear " + name +"!"
    print( "|" + line.center( length ) + "|"  )

def singSong( name, length ):
    bar = "+" + "-"*length + "+"
    print( bar )
    happyBirthday( length )
    happyBirthday( length )
    happyBirthdayDear( name, length )
    happyBirthday( length )
    print( bar )

def main():
    fileName = input( "File name? " )

    # get the contents of the file as a string.
    file = open( fileName, "r" )
    text = file.read()
    file.close

    # strip any blank lines that may be at the front
    # or end of the text.
    text = text.strip()

    
    for name in text.split( "\n" ):
        longLine = "Happy birthday, dear " + name + "!"
        length   = len( longLine )
        singSong( name, length )
        print()

main()

# --------------------------------------------------------------------------------------
# Solutions for Hw #5 Pb #5
# D. Thiebaut

def createMessageAndNameFiles():
    message1 = "Hip hip, Hurray! zzzz is the best!\n"
    file = open( "message.txt", "w" )
    file.write( message1 )
    file.close()

    names = "Gru\nDave\n"
    file = open( "names.txt", "w" )
    file.write( names )
    file.close()

def main():
    # create the 2 different files.  This is not really what
    # you had to do, but this way I don't have to create separate
    # files by hand...
    createMessageAndNameFiles()
    
    # get the names of the two files
    messageFile= input( "Message file name? " )
    namesFile  = input( "Name file?         " )
    
    
    # get the message 
    file = open( messageFile, "r" )
    text = file.read()
    file.close

    # get the names
    file = open( namesFile, "r" )
    names = file.read()
    file.close()

    # strip any blank lines that may be at the front
    # or end of the text.
    names = names.strip()
    names = names.split( "\n" )

    # iterate through the names and print the message
    # for each name found.
    for name in names:
        print( text.strip().replace( "zzzz", name ) )
        print()

main()

</showafterdate>


...