Difference between revisions of "CSC111 Homework 2"

From dftwiki3
Jump to: navigation, search
Line 2: Line 2:
 
This assignment is due on 2/11/10 at midnight.  This week is a week where you are '''asked''' to work in pairs.  Please  find a partner with whom you'll apply the recommendations listed in the paper we discussed on Monday.  You will have a choice to work individually or in pair next week.
 
This assignment is due on 2/11/10 at midnight.  This week is a week where you are '''asked''' to work in pairs.  Please  find a partner with whom you'll apply the recommendations listed in the paper we discussed on Monday.  You will have a choice to work individually or in pair next week.
 
</bluebox>
 
</bluebox>
 
+
<br>
 +
<br>
 +
__TOC__
 +
<br>
 
=Problem #1: Triangles=
 
=Problem #1: Triangles=
  

Revision as of 14:54, 3 February 2010

This assignment is due on 2/11/10 at midnight. This week is a week where you are asked to work in pairs. Please find a partner with whom you'll apply the recommendations listed in the paper we discussed on Monday. You will have a choice to work individually or in pair next week.




Problem #1: Triangles

Using python in interactive mode, type the following python statements one after the other, and try to figure out what is going on:

>>> line = "!"

>>> line

>>> line = 5 * "!"

>>> line 

>>> line = 20 * "a"

>>> line

>>> n = 7

>>> line = n * "b"

>>> line

Using this new discovery, write a program (calle it hw2a.py) that uses a for-loop to display the following triangle:

*
**
***
****
*****

Every time your program runs, it just displays the same pattern: five lines. First line with one star, last line with five stars. When this test program works well, modify it so that it will first ask the user how many lines the triangle should have, and then the program displays triangle with that number of lines.

Here's an example of how your program should work (the user input is underlined):

python hw2b.py

How many lines? 7

* 
**
***
****
*****
******
*******

When your program works well, you are ready to write the final program, which is the one you will submit for this assignment.

Modify your program (or write a new one) that will prompt the user for a number and use this number to print several triangles made of stars on the screen. The number entered by the user will define the number of lines used to display the triangles.

Here is an example of how the program will work when the user enters 5:


python hw2a.py

How many lines? 5

triangle 1

*
**
***
****
***** 

triangle 2

*****
****
***
**
*

triangle 3 

    *
   **
  ***
 ****
*****

triangle 4

*****
 ****
  ***
   **
    *

Note that all the triangles are flush against the left margin, but it is okay if your solution has an extra space there, in particular for Triangles 3 and 4.

Submit your program as follows:


     submit hw2 hw2a.py

Hints

For Triangle 3 and Triangle 4, make your program output them in a different format first. This will help you figure out how to actually print them. For example, make your program output the triangles with dots and stars. When that works, then replace the dots by spaces!

triangle 3

....*
...**
..***
.****
*****

triangle 4

*****
.****
..***
...**
....*

Problem #2: Old Mac Donald's Farm revisited

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 = raw_input( "Enter the name of a friend of yours: " )
    age  = 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.

 python hw2b.py
 
 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

  1. You must use loops
  2. 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.
  3. Make sure you call your program exactly hw2b.py, all lowercase. The programs you submit are tested by a special program every week, and this special program looks for partical names each week. This week it's hw2a.py and hw2b.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 carefull, please!

Submission

Submit your program as follows:

  submit hw2 hw2b.py


A Note on Grading

Currently, good documentation and the ability to write a program that runs without errors is our most important goal.

Each program in this assignment counts for 1/2 of the assignment grade.

For each program, 1/3 to a full point will be taken off for bad documentation (i.e. from A to A-)

A 1/3 to 2/3 of a point will be taken for incorrect output (i.e. from A to B+)

A submitted program that does not work will be worth at most a C grade, depending on documentation.