Difference between revisions of "CSC111 Homework 5 2015"

From dftwiki3
Jump to: navigation, search
(Problem 4)
(Problem 4)
Line 210: Line 210:
 
+---------------------------+
 
+---------------------------+
 
</source>
 
</source>
 +
<br />
 +
==Submission==
 +
* Call your program hw5_4.py and submit it to the HW 5 PB 4 section on Moodle.
 +
<br />
 +
=Problem #5=
 
<br />
 
<br />

Revision as of 10:40, 24 February 2015

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


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