CSC111 Homework 2 2011
--D. Thiebaut 13:29, 20 September 2011 (EDT)
Contents
Problem #1
Problem #2
I have written a Python program (hw2a.py) and have executed it several times in Idle. I'm including below the output of the program captured from the shell window:
>>> ================================ RESTART ================================
>>>
How many lines do you want printed? 0
>>> ================================ RESTART ================================
>>>
How many lines do you want printed? 1
*
>>> ================================ RESTART ================================
>>>
How many lines do you want printed? 2
*
*... **
>>> ================================ RESTART ================================
>>>
How many lines do you want printed? 3
*
*... **
**...... -***
>>> ================================ RESTART ================================
>>>
How many lines do you want printed? 10
*
*... **
**...... -***
***......... --****
****............ ---*****
*****............... ----******
******.................. -----*******
*******..................... ------********
********........................ -------*********
*********........................... --------**********
>>>
Your assignment is to write a program that behaves exactly, or as close to the program I have written as possible. In particular, notice that when I entered 0, it didn't print anything. When I entered 1, it printed one line. When I entered 2 it printed 2 lines. 3, 3 lines; 10, 10 lines.
You do not have to worry about the extra space at the beginning of the first line. Your program will be be good even if it prints the first star of the first line in the left most column.
Submission
- Submit your program on beowulf or emmy as follows:
submit hw2 hw2b.py
Problem #3
Same idea as for Problem #2. Reconstruct a program I have written (called hw2c.py) which will behave the same way as a program whose output is shown below.
>>> ================================ RESTART ================================
>>>
Enter a positive number less than 20: 5
Printing a diamond of 5 lines
*
***
*****
*******
*********
*******
*****
***
*
>>> ================================ RESTART ================================
>>>
Enter a positive number less than 20: 4
Printing a diamond of 4 lines
*
***
*****
*******
*****
***
*
>>> ================================ RESTART ================================
>>>
Enter a positive number less than 20: 0
Printing a diamond of 0 lines
>>> ================================ RESTART ================================
>>>
>>>
Enter a positive number less than 20: 1
Printing a diamond of 1 lines
*
>>>
Problem #4: Old Mac Donald's Farm (in Python!)
For this problem, I am asking you to stretch learn new features that are logically the extension of constructs we have seen, and to apply them in a logical way. I am asking you to use your intuitive knowledge of Python to build a program out of separate parts that you haven't seen before, but that should make sense once you have studied them and played with them.
First, Play!
Write and play with the following program.
# hw2test.py
# D. Thiebaut
# this is an undocumented program to get you started on Homework #2
#
def main():
pair1 = [ "Alex", 20 ]
pair2 = [ "Joe", 21 ]
pair3 = [ "Max", 40 ]
friends = [ pair1, pair2, pair3 ]
name = input( "Enter the name of a friend of yours: " )
age = eval( input ( "What is the age of this friend? " ) )
friends.append( [ name, age ] )
print "Here is the list of your friends: ",
print friends
for friendName, age in friends:
print "your friend", friendName, "is", age, "years old"
main()
Feel free to modify it to see how it works. Modification of code is a good way to understand how a programming language works.
At the heart of the program is a list, friends, which contains pairs. A pair is a list of two items. Each pair contains a string (the friend's name) and an integer (the friend's age). So the friends list is a list of lists.
The for-loop takes each item from the friends list and, since each item is a pair, breaks the pair into two values that get assigned to two variables, friendName and age.
Your assignment
Use this new feature of for-loops and lists, and write a program that will ask the user for 3 animals and the noise they make, and will generate part of the Old MacDonald song.
Here is an example of how your program should work. The user input is underlined.
Welcome to old MacDonald's farm Please enter 3 animals and the noise they make: animal? cow noise? moo-moo animal? hen noise? cluck-cluck animal? sheep noise? baa-baa Old MacDonald had a farm, E-I-E-I-O And on his farm he had some cows, E-I-E-I-O And a moo-moo here, and a moo-moo there, everywhere a moo-moo! Old MacDonald had a farm, E-I-E-I-O And on his farm he had some hens, E-I-E-I-O And a cluck-cluck here, and a cluck-cluck there, everywhere a cluck-cluck! Old MacDonald had a farm, E-I-E-I-O And on his farm he had some sheeps, E-I-E-I-O And a baa-baa here, and a baa-baa there, everywhere a baa-baa!
Requirements
- You must use at least one loop (you do not have to use a loop to input the 3 animals from the user).
- The name of the animal should be entered in the singular form. It's the program that adds an 's' at the end of the name of the animal, in the lyrics.
- If you enter the same 3 animals and noises as in the example above, your program should output the same series of lines as shown above. No extra spaces in the lines are allowed. Remember that you can concatenate (glue) strings together with the + operator.
- Make sure you call your program exactly hw2d.py, all lowercase. The programs you submit are tested by a special program every week, and this special program looks for specific names each week. This week it's hw2b.py, hw2c.py and hw2d.py. If you submit a program spelled differently (for example using uppercase letters), the grading program will miss your program and it will not be graded! So be careful, please!
Submission
Submit your program as follows:
submit hw2 hw2d.py