Difference between revisions of "CSC111 Homework 5 2015b"
(14 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
---- | ---- | ||
<bluebox> | <bluebox> | ||
− | This assignment should be done in pairs, and is due on 10/ | + | This assignment should be done in pairs, and is due on 10/15/15, at 11:55 p.m.<br /> |
+ | ''Note:'' I activated the "debug" feature for these problems on Moodle. If you click "Debug" instead of "Evaluate," you will see the output of Moodle in a console/terminal window, where the output from the auto-grader will be formatted differently. Hopefully, this should help you figure out why your output might not match that of the solution program. Be careful to click on "Evaluate" to record your grade, though! | ||
</bluebox> | </bluebox> | ||
<br /> | <br /> | ||
− | <showafterdate after=" | + | <showafterdate after="20151007 12:00" before "20151231 00:00"> |
<br /> | <br /> | ||
=Problem 1= | =Problem 1= | ||
Line 174: | Line 175: | ||
* 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= | + | =Problem 5: Old MacDonald had a Farm= |
+ | <br /> | ||
+ | * Write a program called '''hw5_5.py''' that contains 2 functions, called '''singOneAnimal(...)''' and '''singFarm(...)''', that are illustred in the code below: | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | def singOneAnimal(... ): | ||
+ | ... | ||
+ | |||
+ | def singFarm( ... ): | ||
+ | ... | ||
+ | |||
+ | def main(): | ||
+ | singOneAnimal( "PIG" ) | ||
+ | |||
+ | singFarm( ["dog", "CAT", "ROACH" ] ) | ||
+ | |||
+ | farm = [ "mouse", "duck", "worm" ] | ||
+ | singFarm( farm ) | ||
+ | |||
+ | main() | ||
+ | </source> | ||
+ | <br /> | ||
+ | * The output of the program above, once the functions are completed correctly, will be the following: | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | Old MacDonald had a farm, E-I-E-I-O | ||
+ | And on his farm he had a pig E-I-E-I-O | ||
+ | Here a pig, there a pig, everywhere a pig! | ||
+ | |||
+ | Old MacDonald had a farm, E-I-E-I-O | ||
+ | And on his farm he had a dog E-I-E-I-O | ||
+ | Here a dog, there a dog, everywhere a dog! | ||
+ | |||
+ | Old MacDonald had a farm, E-I-E-I-O | ||
+ | And on his farm he had a cat E-I-E-I-O | ||
+ | Here a cat, there a cat, everywhere a cat! | ||
+ | |||
+ | Old MacDonald had a farm, E-I-E-I-O | ||
+ | And on his farm he had a roach E-I-E-I-O | ||
+ | Here a roach, there a roach, everywhere a roach! | ||
+ | |||
+ | Old MacDonald had a farm, E-I-E-I-O | ||
+ | And on his farm he had a mouse E-I-E-I-O | ||
+ | Here a mouse, there a mouse, everywhere a mouse! | ||
+ | |||
+ | Old MacDonald had a farm, E-I-E-I-O | ||
+ | And on his farm he had a duck E-I-E-I-O | ||
+ | Here a duck, there a duck, everywhere a duck! | ||
+ | |||
+ | Old MacDonald had a farm, E-I-E-I-O | ||
+ | And on his farm he had a worm E-I-E-I-O | ||
+ | Here a worm, there a worm, everywhere a worm! | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | * Submit your program in the HW 5 PB 5 section. Note that the main function of your program will be replaced by a different main() program that will call your functions and pass them its own strings. | ||
+ | <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: | * 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: | ||
Line 210: | Line 268: | ||
</source> | </source> | ||
− | + | ||
<br /> | <br /> | ||
* If you study the strings that are passed to the function '''mailMerge()''', and the output it generates, you will find that it will have ''substituted'' the name of the people for the string "%%NAME%%" in the message passed. It does the same for the strings "%%QUANTITY%%" and "%%ITEM%%", that get substituted for some amount of stuff, and the name of the stuff. | * If you study the strings that are passed to the function '''mailMerge()''', and the output it generates, you will find that it will have ''substituted'' the name of the people for the string "%%NAME%%" in the message passed. It does the same for the strings "%%QUANTITY%%" and "%%ITEM%%", that get substituted for some amount of stuff, and the name of the stuff. | ||
* Submit your program to the '''HW 5 PB 6''' section on Moodle. | * Submit your program to the '''HW 5 PB 6''' section on Moodle. | ||
+ | * (Added --~~~~): <font color="magenta">When your program is tested by Moodle, its main() function will be removed, and one similar to the one shown above will be used. This way your function '''must''' work exactly as described here.</font> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
Line 291: | Line 350: | ||
DNA length = 30 | DNA length = 30 | ||
Sequence marked by AGGA = 'GAAAATTTTAAA' with length 12 | Sequence marked by AGGA = 'GAAAATTTTAAA' with length 12 | ||
− | Sequence marked by ATTA = ' | + | Sequence marked by ATTA = '' with length 0 |
− | |||
</source> | </source> | ||
<br /> | <br /> | ||
− | * | + | * Once again, your program will be tested using a different main() function, that will call the two functions defined above. Make sure they work well. |
+ | * Test your program well, with different types of DNA strings and markers. | ||
+ | * Submit your program to Moodle, Section HW 5 PB 8. | ||
<br /> | <br /> | ||
+ | </showafterdate> | ||
<br /> | <br /> | ||
+ | =Solution Programs= | ||
<br /> | <br /> | ||
+ | <showafterdate after="20151016 07:00" before="20151231 00:00"> | ||
+ | ::<source lang="python"> | ||
+ | |||
+ | # -------------------------------------------------------------------------------------- | ||
+ | # Solutions for Hw #5 | ||
+ | # D. Thiebaut | ||
+ | # These are collections of functions that were used in 8 different problems. | ||
+ | # | ||
+ | |||
+ | def happyBirthday1Line( minion ): | ||
+ | """ Sings just one line of happy birthday to a minion. """ | ||
+ | print( "Happy birthday to you, dear", minion ) | ||
+ | |||
+ | def happyBirthday2( minion ): | ||
+ | """ sings a 1-line happy birthday, sandwiched in 2 lines. """ | ||
+ | bar = "-"*40 | ||
+ | print( bar ) | ||
+ | happyBirthday1Line( minion ) | ||
+ | print( bar ) | ||
+ | |||
+ | def reformatDate( date ): | ||
+ | """gets a date in the form mmddyyyy and returns | ||
+ | a string in the form "dd MMM yyyy", where MMM | ||
+ | is a 3-character abbreviation of the month.""" | ||
+ | |||
+ | months = ["XXX", "Jan", "Feb", "Mar", "Apr", | ||
+ | "May", "Jun", "Jul", "Aug", "Sep", | ||
+ | "Oct", "Nov", "Dec" ] | ||
+ | |||
+ | # extract the information from the date | ||
+ | year = date[4:] | ||
+ | day = int( date[2:4] ) | ||
+ | month = int( date[0:2] ) | ||
+ | |||
+ | # create a new date formatted nicely | ||
+ | newDate = "{0:1} {1:1} {2:1}".format( months[month], day, year ) | ||
+ | |||
+ | # return the nicely formatted date. | ||
+ | return newDate | ||
+ | |||
+ | def convertTemperature( Fahr ): | ||
+ | """A simple function returning the Celsius version of a temperature in Fahrenheit""" | ||
+ | return (Fahr-32) * 5/9 | ||
+ | |||
+ | def addGremlins( sentence ): | ||
+ | """ returns the string sentence where all the gs are replaced by "gremlin" and | ||
+ | Gs by "GREMLIN" strings""" | ||
+ | |||
+ | newSentence = sentence.replace( 'g', 'gremlin' ).replace( 'G', 'GREMLIN' ) | ||
+ | return newSentence | ||
+ | |||
+ | |||
+ | def singOneAnimal( animal ): | ||
+ | """prints the song for Old-MacDonald's farm for a given animal""" | ||
+ | |||
+ | print( "Old MacDonald had a farm, E-I-E-I-O" ) | ||
+ | print( "And on his farm he had a", animal, "E-I-E-I-O" ) | ||
+ | print( "Here a ", animal, ", there a ", animal, ", everywhere a ", animal, "!", sep=" " ) | ||
+ | print() | ||
+ | |||
+ | def singFarm( farm ): | ||
+ | """ sings the song Old MacDonald for several animals in the list farm """ | ||
+ | for animal in farm: | ||
+ | singOneAnimal( animal ) | ||
+ | |||
+ | |||
+ | def mailMerge( message, name, quantity, order ): | ||
+ | """ Gets a message and 3 strings, and substitutes them in the message, | ||
+ | name is substituted wherever %%NAME%% appears, | ||
+ | quantity is substituted wherever %%QUANTITY%% appears, | ||
+ | order is substituted wherever %%ORDER%% appears in the string. | ||
+ | """ | ||
+ | |||
+ | newMessage = message.replace( "%%NAME%%", name ) | ||
+ | newMessage = newMessage.replace( "%%QUANTITY%%", quantity ) | ||
+ | newMessage = newMessage.replace( "%%ORDER%%", order ) | ||
+ | print( newMessage ) | ||
+ | |||
+ | def finalGrade( grades ): | ||
+ | """we assume that first 5 numbers in the list are homework grades, | ||
+ | and count for 40% of the final grade. The 6th and 7th numbers are | ||
+ | exams grades, which count for 30% both.""" | ||
+ | total = sum( grades[0:5] )*0.4 + grades[5]*0.3 + grades[6]*0.3 | ||
+ | return round( total, 2 ) | ||
+ | |||
+ | def DNAcut( DNA, marker ): | ||
+ | DNA = DNA.upper() | ||
+ | marker = marker.upper() | ||
+ | DNA = DNA.replace( marker, 'Z' ) | ||
+ | index1 = DNA.find( 'Z' ) | ||
+ | index2 = DNA.find( 'Z', index1+1 ) | ||
+ | seq = DNA[index1:index2] | ||
+ | return seq[1: ] | ||
+ | |||
+ | def DNALength( DNA ): | ||
+ | return len( DNA ) | ||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
+ | </showafterdate> | ||
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 09:22, 16 October 2015
--D. Thiebaut (talk) 09:37, 4 October 2015 (EDT)
This assignment should be done in pairs, and is due on 10/15/15, at 11:55 p.m.
Note: I activated the "debug" feature for these problems on Moodle. If you click "Debug" instead of "Evaluate," you will see the output of Moodle in a console/terminal window, where the output from the auto-grader will be formatted differently. Hopefully, this should help you figure out why your output might not match that of the solution program. Be careful to click on "Evaluate" to record your grade, though!
<showafterdate after="20151007 12:00" before "20151231 00:00">
Contents
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.
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 5: Old MacDonald had a Farm
- Write a program called hw5_5.py that contains 2 functions, called singOneAnimal(...) and singFarm(...), that are illustred in the code below:
def singOneAnimal(... ): ... def singFarm( ... ): ... def main(): singOneAnimal( "PIG" ) singFarm( ["dog", "CAT", "ROACH" ] ) farm = [ "mouse", "duck", "worm" ] singFarm( farm ) main()
- The output of the program above, once the functions are completed correctly, will be the following:
Old MacDonald had a farm, E-I-E-I-O And on his farm he had a pig E-I-E-I-O Here a pig, there a pig, everywhere a pig! Old MacDonald had a farm, E-I-E-I-O And on his farm he had a dog E-I-E-I-O Here a dog, there a dog, everywhere a dog! Old MacDonald had a farm, E-I-E-I-O And on his farm he had a cat E-I-E-I-O Here a cat, there a cat, everywhere a cat! Old MacDonald had a farm, E-I-E-I-O And on his farm he had a roach E-I-E-I-O Here a roach, there a roach, everywhere a roach! Old MacDonald had a farm, E-I-E-I-O And on his farm he had a mouse E-I-E-I-O Here a mouse, there a mouse, everywhere a mouse! Old MacDonald had a farm, E-I-E-I-O And on his farm he had a duck E-I-E-I-O Here a duck, there a duck, everywhere a duck! Old MacDonald had a farm, E-I-E-I-O And on his farm he had a worm E-I-E-I-O Here a worm, there a worm, everywhere a worm!
- Submit your program in the HW 5 PB 5 section. Note that the main function of your program will be replaced by a different main() program that will call your functions and pass them its own strings.
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
- If you study the strings that are passed to the function mailMerge(), and the output it generates, you will find that it will have substituted the name of the people for the string "%%NAME%%" in the message passed. It does the same for the strings "%%QUANTITY%%" and "%%ITEM%%", that get substituted for some amount of stuff, and the name of the stuff.
- Submit your program to the HW 5 PB 6 section on Moodle.
- (Added --~~~~): When your program is tested by Moodle, its main() function will be removed, and one similar to the one shown above will be used. This way your function must work exactly as described here.
Problem 7
- Write a program called hw5_7.py that contains 1 function called finalGrade(...) and that receives as parameter a list of 7 numbers representing grades. The first 5 grades are homework assignments, and count for 40% of the final grade. The last 2 numbers represent exams, and each exam counts for 30% of the final grade. The function returns the integer value of the final grade, computed as 40% of the average of the first 5 grades, + 30% of the 6th grade, + 30% of the last number.
- Example:
def main(): grades = [100, 100, 100, 90, 80, 51, 95 ] print( "Grades: " , grades ) print( "Total: " , finalGrade( grades ) ) print() grades = [100, 100, 100, 100, 100, 100, 100 ] print( "Grades: " , grades ) print( "Total: " , finalGrade( grades ) ) print() grades = [100, 100, 100, 100, 100, 0, 0 ] print( "Grades: " , grades ) print( "Total: " , finalGrade( grades ) ) print()
- and the output:
Grades: [100, 100, 100, 90, 80, 51, 95] Total: 81 Grades: [100, 100, 100, 100, 100, 100, 100] Total: 100 Grades: [100, 100, 100, 100, 100, 0, 0] Total: 40
- Submit your program in the HW 5 PB 7 section of Moodle.
Problem #8: DNA
- Write a program called hw5_8.py that processes DNA strings, containing symbols A, C, G, and T, and sometimes - and N, and that extracts a substring that appears between specific markers. An example will illustrate what is going on.
DNA string = ACGTAAGGAGAAAATTTTAAAAGGATTTTT Marker = AGGA DNA substring between AGGA markers = GAAAATTTTAAA
- We assume that the marker will always appear twice, or none at all.
- Your program should contain 2 different functions:
- DNAcut( ... ): this function receives a string of DNA, and a marker string, and returns the string that can be found between the first two occurrences of the marker string. If the marker string does not appear in the DNA string, then the empty string "" is returned.
- DNALength( ... ): this function receives a string of DNA and returns the number of symbols it contains.
- Example of a main() function that would call these two functions:
def main(): DNA = "ACGTAAGGAGAAAATTTTAAAAGGATTTTT" print( "DNA = ", DNA ) print( "DNA length = ", DNALength( DNA ) ) DNAseq = DNAcut( DNA, "AGGA" ) print( "Sequence marked by AGGA = '", DNAseq, "' with length ", DNALength( DNAseq ), sep="" ) DNAseq = DNAcut( DNA, "ATTA" ) print( "Sequence marked by ATTA = '", DNAseq, "' with length ", DNALength( DNAseq ), sep="" ) main()
- and the output:
DNA = ACGTAAGGAGAAAATTTTAAAAGGATTTTT DNA length = 30 Sequence marked by AGGA = 'GAAAATTTTAAA' with length 12 Sequence marked by ATTA = '' with length 0
- Once again, your program will be tested using a different main() function, that will call the two functions defined above. Make sure they work well.
- Test your program well, with different types of DNA strings and markers.
- Submit your program to Moodle, Section HW 5 PB 8.
</showafterdate>
Solution Programs
<showafterdate after="20151016 07:00" before="20151231 00:00">
# -------------------------------------------------------------------------------------- # Solutions for Hw #5 # D. Thiebaut # These are collections of functions that were used in 8 different problems. # def happyBirthday1Line( minion ): """ Sings just one line of happy birthday to a minion. """ print( "Happy birthday to you, dear", minion ) def happyBirthday2( minion ): """ sings a 1-line happy birthday, sandwiched in 2 lines. """ bar = "-"*40 print( bar ) happyBirthday1Line( minion ) print( bar ) def reformatDate( date ): """gets a date in the form mmddyyyy and returns a string in the form "dd MMM yyyy", where MMM is a 3-character abbreviation of the month.""" months = ["XXX", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ] # extract the information from the date year = date[4:] day = int( date[2:4] ) month = int( date[0:2] ) # create a new date formatted nicely newDate = "{0:1} {1:1} {2:1}".format( months[month], day, year ) # return the nicely formatted date. return newDate def convertTemperature( Fahr ): """A simple function returning the Celsius version of a temperature in Fahrenheit""" return (Fahr-32) * 5/9 def addGremlins( sentence ): """ returns the string sentence where all the gs are replaced by "gremlin" and Gs by "GREMLIN" strings""" newSentence = sentence.replace( 'g', 'gremlin' ).replace( 'G', 'GREMLIN' ) return newSentence def singOneAnimal( animal ): """prints the song for Old-MacDonald's farm for a given animal""" print( "Old MacDonald had a farm, E-I-E-I-O" ) print( "And on his farm he had a", animal, "E-I-E-I-O" ) print( "Here a ", animal, ", there a ", animal, ", everywhere a ", animal, "!", sep=" " ) print() def singFarm( farm ): """ sings the song Old MacDonald for several animals in the list farm """ for animal in farm: singOneAnimal( animal ) def mailMerge( message, name, quantity, order ): """ Gets a message and 3 strings, and substitutes them in the message, name is substituted wherever %%NAME%% appears, quantity is substituted wherever %%QUANTITY%% appears, order is substituted wherever %%ORDER%% appears in the string. """ newMessage = message.replace( "%%NAME%%", name ) newMessage = newMessage.replace( "%%QUANTITY%%", quantity ) newMessage = newMessage.replace( "%%ORDER%%", order ) print( newMessage ) def finalGrade( grades ): """we assume that first 5 numbers in the list are homework grades, and count for 40% of the final grade. The 6th and 7th numbers are exams grades, which count for 30% both.""" total = sum( grades[0:5] )*0.4 + grades[5]*0.3 + grades[6]*0.3 return round( total, 2 ) def DNAcut( DNA, marker ): DNA = DNA.upper() marker = marker.upper() DNA = DNA.replace( marker, 'Z' ) index1 = DNA.find( 'Z' ) index2 = DNA.find( 'Z', index1+1 ) seq = DNA[index1:index2] return seq[1: ] def DNALength( DNA ): return len( DNA )
</showafterdate>