Difference between revisions of "CSC111 Homework 5 2015b"

From dftwiki3
Jump to: navigation, search
(Problem #4: Gremlins in your Computer!)
Line 5: Line 5:
 
</bluebox>
 
</bluebox>
  
 +
<br />
 +
<showafterdate after="2015107 12:00" before "20151231 00:00">
 
<br />
 
<br />
 
=Problem 1=
 
=Problem 1=
Line 172: Line 174:
 
* Submit your program in the HW 5 PB 5 section on Moodle.
 
* Submit your program in the HW 5 PB 5 section on Moodle.
 
<br />
 
<br />
 +
=Problem 6: Mail-Merge=
 
<br />
 
<br />
 +
* Write a program called '''hw5_6.py''' that contains 1 function called '''mailMerge(...)''', the behavior of which is illustrated in the example below.  First the code:
 
<br />
 
<br />
 +
::<source lang="python">
 +
def main():
 +
    msg1 = """Dear %%NAME%%,
 +
Your order for %%QUANTITY%% %%ITEM%% has been received.
 +
We will ship you the %%ITEM%% in the next 24 hours."""
 +
 +
    msg2 = """The order for %%QUANTITY%% %%ITEM%%
 +
ordered by %%NAME%% has just been shipped."""
 +
 +
    msg3 = """The quick red fox jumped over the dog"""
 +
   
 +
    mailMerge( msg1, "Alma", "a ton of", "chocolate" )
 +
    mailMerge( msg2, "Paul", "2000", "paper airplanes" )
 +
    mailMerge( msg3, "wolf", "10", "rabbits" )
 +
   
 +
main()
 +
 +
</source>
 +
<br />
 +
::and the output:
 +
<br />
 +
::<source lang="text">
 +
Dear Alma,
 +
Your order for a ton of chocolate has been received.
 +
We will ship you the chocolate in the next 24 hours.
 +
 +
The order for 2000 paper airplanes
 +
ordered by Paul has just been shipped.
 +
 +
The quick red fox jumped over the dog
 +
 +
</source>
 +
</showafterdate>
 
<br />
 
<br />
 +
* Submit your program to the '''HW 5 PB 6''' section on Moodle.
 
<br />
 
<br />
 
<br />
 
<br />

Revision as of 11:51, 4 October 2015

--D. Thiebaut (talk) 09:37, 4 October 2015 (EDT)


This assignment should be done in pairs, and is due on 10/8/15, at 11:55 p.m.


<showafterdate after="2015107 12:00" before "20151231 00:00">

Problem 1


This problem is super easy, but will get you to see the structure of the type of programs you will have to write for this homework. All the programs will have the same structure, and will be tested in a similar way.

  • Write a program called hw5_1.py that has contains a function called happyBirthday1Line(...), that receives one parameter, which will contain the name of a person (say, "Elena"), and that will print the following message:


Happy birthday to you, dear Elena


Note that the function only prints one line. No extra blank lines.


  • Add a main function to your program that will call your happyBirthday1Line() function. For example:


def main():
     happyBirthday1Line( "Elena" )
     happyBirthday1Line( "Maria" )
     happyBirthday1Line( "Rui" )

main()


  • When you run your program, the output should be:



Happy birthday to you, dear Elena 
Happy birthday to you, dear Maria 
Happy birthday to you, dear Rui


  • Submit your program to the HW 5 PB 1 section on Moodle.


Note 1: that Moodle will remove the main function from your program and replace it with another main() function, that will call your function with different names. This will ensure that you write your function correctly, independently of your main function. So, do not be surprised if you see the output of your program on Moodle not corresponding to your output. It's normal!


Note 2: Another reminder about the way Moodle shows you differences between your program and the solution program. Below is an example of the type of output you might get.

 + Happy birthday to you, dear Alfred
 - Happy birthday to you, dear ALFred
 ?                             --
 + Happy birthday to you, dear Fred
 - Happy birthday to you, dear FRED
 - Happy birthday to you, dear naomi
 ?                             ^
 + Happy birthday to you, dear Naomi
 ?                             ^

The lines starting with a + represent the solution output. The lines starting with - represent the output of your program. You see, above, that the solution program is capitalizing the first names correctly, while the submitted program does not.


Problem 2


  • Write a program called hw5_2.py that contains 2 functions, the first one is the one from Problem 1, happyBirthday1Line(), and the second one is a function called happyBirthday2(...).
  • Here is an example of a main() program that calls happyBirthday2(...), and its output:


def main():
      happyBirthday2( "Alexandra" )
      happyBirthday2( "Mickey MOUSE" )
      happyBirthday2( "OLGA" )

main()


and the output:


----------------------------------------
Happy birthday to you, dear Alexandra
----------------------------------------
 
----------------------------------------
Happy birthday to you, dear Mickey Mouse
----------------------------------------

----------------------------------------
Happy birthday to you, dear Olga
----------------------------------------


  • Make your function happyBirthday2() call happyBirthday1Line(), since this second function is already written and works well.
  • Submit your program in the Moodle section called HW 5 PB 2.
  • Note: Moodle will test that your output matches exactly the output of the solution program. This includes the horizontal bars. Make sure they have the same lengths!


Problem #3


  • Here is a main program that calls 2 different functions, one called reformatDate() and the other one called convertTemperature().


def main():
    # display 3 dates in digit format, and in easier to read format
    for date in [ "01012000", "12311900", "01011000" ]:
        print( date, "-->", reformatDate( date ) )

    print()
    
    # display 3 temperatures in Fahrenheit and in Celsius
    for temp in [32, 64, 100]:
        print( "{0:1} Fahrenheit = {1:1.2f} Celsius"
               .format( temp, convertTemperature( temp ) ) )


main()


  • Here is the output of the program:


01012000 --> Jan 1 2000
12311900 --> Dec 31 1900
01011000 --> Jan 1 1000

32 Fahrenheit = 0.00 Celsius
64 Fahrenheit = 17.78 Celsius
100 Fahrenheit = 37.78 Celsius


  • Write a program called hw5_3.py that contains the code for the two functions reformatDate() and convertTemperature().
  • Note: Your functions will be tested on Moodle with a main program that is different from yours
  • Submit your program in the HW 5 PB 3 section.


Also, I enabled the debug button for this problem in Moodle. If you click it instead of evaluate, it will show the output of the grader in a console window (black window), which might be easier to read and figure out than where the evaluate button puts its messages. However, the debug button doesn't store your grade in my grade book. Only evaluate does! So be sure to click on evaluate when your program runs well!


Problem #4: Gremlins in your Computer!


  • Write a program called hw5_5.py which contains a function, called addGremlins( ... ), that, when passed a string of characters, returns a new string where all the g or G letters are replaced by "gremlin" or "GREMLIN".
  • Here is an example of how your function could be called from a main program, and its corresponding output:


def main():
	for line in [ "good great", "Not so bad!", "Good and Great" ]:
		print( line, addGremlins( line ), sep="\n" )
		print( )

main()


and the output:


good great
gremlinood gremlinreat

Not so bad!
Not so bad!

Good and Great
GREMLINood and GREMLINreat


  • Submit your program in the HW 5 PB 5 section on Moodle.


Problem 6: Mail-Merge


  • Write a program called hw5_6.py that contains 1 function called mailMerge(...), the behavior of which is illustrated in the example below. First the code:


def main():
    msg1 = """Dear %%NAME%%,
Your order for %%QUANTITY%% %%ITEM%% has been received.
We will ship you the %%ITEM%% in the next 24 hours."""

    msg2 = """The order for %%QUANTITY%% %%ITEM%% 
ordered by %%NAME%% has just been shipped."""

    msg3 = """The quick red fox jumped over the dog"""
    
    mailMerge( msg1, "Alma", "a ton of", "chocolate" )
    mailMerge( msg2, "Paul", "2000", "paper airplanes" )
    mailMerge( msg3, "wolf", "10", "rabbits" )
    
main()


and the output:


Dear Alma,
Your order for a ton of chocolate has been received.
We will ship you the chocolate in the next 24 hours.

The order for 2000 paper airplanes 
ordered by Paul has just been shipped.

The quick red fox jumped over the dog

</showafterdate>

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