CSC111 Homework 5 2015

From dftwiki3
(Redirected from CSC111 Hw 5 2015)
Jump to: navigation, search

--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 everybody 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, and combines nicely many Python constructs we have seen recently.

First Step


  • Write a program called hw5_6.py that asks the user for the names of two files, reads the contents of both text files, and then prints a series of messages on the screen.
  • The first file that your program should prompt the user for, will contain the information about several people, on several lines of text. Each line contains 3 words separated by spaces. The first word is the name of a person, the second is a thing related to that person (maybe something she ordered on line), and the third is a date (for example, a delivery date).


Example


Dave chocolate 02152015
Carl toothpaste 02152020
Gru superglue 01012100


  • The second file that your program will prompt the user for, contains one or several lines of text. This text is a message that will could be a message sent to the user, or maybe some 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.  
If you do not claim the $stuff before $date, it will be put on sale on eBay.


  • Your program will read each line of the file containing user information, and will divide each line into separate words. This should be a list of 3 words. It will take the first word and will replace the string "$name" by this word in the message. Then it will take the second word and replace "$stuff" by this word in the message. Finally it will take the third word and replace "$date" by it. So, assuming that the first line of the user file 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.  
If you do not claim the chocolate before 02152015, it will be put on sale on eBay.


  • The full output of your program, if users.txt contains 3 lines of information for 3 different people (as 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.  
If you do not claim the chocolate before 02152015, it will be put on sale on eBay.

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.  
If you do not claim the toothpaste before 02152020, it will be put on sale on eBay.

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.  
If you do not claim the superglue before 01012100, it will be put on sale on eBay.


Second Step


  • Modify your program so that it formats the date nicely, with the date formatted as dd mmm yyyy. For example, 02152020 would be printed as 15 Feb 2020.


Example of the output for Person Carl


Dear Carl, this is a coupon for
a ton of toothpaste, which you can pick up
in Ford Hall 241 at any time before 15 Feb 2020.  
If you do not claim the toothpaste before 15 Feb 2020, it will be put on sale on eBay.


Requirements


  • Your program should have at least a main() function that is the entry point of the program. You may include additional functions as well.
  • If the message file contains 3 lines of text, your program should also print 3 lines of text. Do not make your program

wrap lines, or output all 3 lines as a single line.

  • The number of blank lines printed along with the message is unimportant. Extra blank lines will be skipped by the testing script.


Submission


  • Submit your hw5_6.py program to the HW 5 PB 6 section on Moodle.




</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 Problem 2
# 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 Problem #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 Problem #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 Problem #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()

# --------------------------------------------------------------------------------------
# Solutions for Hw #5 Problem #5
# hw5_6.py
# D. Thiebaut


def mailMerge():
    """
    open( "users.txt", "w" ).write(
         "Dave chocolate 02152015\n"
        +"Carl toothpaste 02152020\n"
        +"Gru superglue 01012100\n" )
    open( "message.txt", "w" ).write(
         "Dear $name, this is a coupon for\n"
         +"a ton of $stuff, which you can pick up\n"
         +"in Ford Hall 241 at any time before $date.\n" )
    """
    months = ["dummy", "Jan", "Feb", "Mar", "Apr", "May",
              "Jun", "Jul", "Aug", "Sep", "Oct",
              "Nov", "Dec" ]
    message = open( "message2.txt", "r" ).read()
    users   = open( "users2.txt", "r" ).readlines()

    for user in users:
        words = user.split( ' ' )
        day   = int( words[2][2:4] )
        month = int( words[2][0:2] )
        month = months[ month ]
        year  = words[2][4:]
        date  = "{0:1} {1:1} {2:1}".format( day, month, year )
        date  = date.strip()
        userMessage = message.replace( "$name", words[0] )
        userMessage = userMessage.replace( "$stuff", words[1] )
        userMessage = userMessage.replace( "$date", date )
        print( userMessage )

       
def main():
    mailMerge()

main()

</showafterdate>


...