Difference between revisions of "CSC111 Homework 2"

From dftwiki3
Jump to: navigation, search
(A Note on Grading)
(Problem #1: Triangles)
 
(11 intermediate revisions by the same user not shown)
Line 61: Line 61:
 
Here is an example of how the program will work when the user enters 5:
 
Here is an example of how the program will work when the user enters 5:
  
 
+
<source lang="text">
 
  <u>python hw2a.py</u>
 
  <u>python hw2a.py</u>
 
   
 
   
Line 98: Line 98:
 
     *
 
     *
 
   
 
   
 +
</source>
 +
<br />
 
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.
 
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.
  
 
==Submission==
 
==Submission==
Submit your program as follows:
+
Make sure your program is called exactly hw2a.py.  Not HW2a.py, or homework2a.py.  The exact name is important.  Submit your program as follows:
  
  
Line 190: Line 192:
  
 
==Requirements==
 
==Requirements==
# You must use loops
+
# 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.
 
# 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 '''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!
 
# 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!
Line 201: Line 204:
  
 
=A Note on Grading=
 
=A Note on Grading=
 +
 +
Document well!  Use the [[NicelyDocumentedProgram.py| demo program]] we saw in class as an example of good documentation.
  
 
Currently, '''good documentation''' and the ability to write a '''program that runs without errors''' are our most important goals.
 
Currently, '''good documentation''' and the ability to write a '''program that runs without errors''' are our most important goals.
Line 206: Line 211:
 
Each program in this  assignment counts for 1/2 of the assignment grade.
 
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-)
+
For each program, 1/3 to a full point will be taken off for bad documentation (i.e. from A to A-, B+, or B)
  
A 1/3 to 2/3 of a point will be taken for incorrect output (i.e. from A to B+)
+
A 1/3 to 2/3 of a point will be taken for incorrect output (i.e. from A to A-, or B+)
  
 
A submitted program that does not work will be worth at most a C grade, depending on documentation.
 
A submitted program that does not work will be worth at most a C grade, depending on documentation.
 +
 +
=Transferring files to your partner's account=
 +
 +
You can use the Linux sftp command to transfer a file to your partner's account.  Your partner needs to be present as he/she will need to input his/her password at some point.
 +
 +
 +
* Here I assume that ''you'' have the file to transfer, and ''your partner'' does not.
 +
 +
* At the beowulf prompt, type:
 +
 +
<u>sftp 111c-xx@beowulf.csc.smith.edu</u>
 +
 +
:where xx are the two letters of your partner's account. Beowulf will prompt you for the password for account 111c-xx:
 +
 +
  111c-xx@beowulf.csc's password:
 +
 +
* Note: When you type your password, nothing appears on the screen. You will receive the sftp prompt and you will put the file into the other account:
 +
 +
 +
sftp> <u>put hw2a.py</u>
 +
 +
 +
* The sftp response looks something like this:
 +
 +
 +
hw2a.py                      100%  266  146.1KB/s  00:00
 +
 +
* When the transfer is over, exit from sftp:
 +
 +
 +
sftp> <u>quit</u>
 +
 +
* Now, have your partner log in to his/her account and type the command below.  He/she should see the hw2a.py file.
  
  
 +
  ls                ''(that's ell ess)''
  
  
Line 230: Line 269:
  
  
[[Category:CSC111]][[Category:Homework]]
+
[[Category:CSC111]][[Category:Homework]][[Category:Python]]

Latest revision as of 22:18, 25 February 2014

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:

 <u>python hw2a.py</u>
 
 How many lines? <u>5</u>
 
 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.

Submission

Make sure your program is called exactly hw2a.py. Not HW2a.py, or homework2a.py. The exact name is important. 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 at least one loop (you do not have to use a loop to input the 3 animals from the user).
  2. 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.
  3. 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.
  4. 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

Document well! Use the demo program we saw in class as an example of good documentation.

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

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-, B+, or B)

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

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

Transferring files to your partner's account

You can use the Linux sftp command to transfer a file to your partner's account. Your partner needs to be present as he/she will need to input his/her password at some point.


  • Here I assume that you have the file to transfer, and your partner does not.
  • At the beowulf prompt, type:
sftp 111c-xx@beowulf.csc.smith.edu
where xx are the two letters of your partner's account. Beowulf will prompt you for the password for account 111c-xx:
 111c-xx@beowulf.csc's password:
  • Note: When you type your password, nothing appears on the screen. You will receive the sftp prompt and you will put the file into the other account:


sftp> put hw2a.py


  • The sftp response looks something like this:


hw2a.py                       100%  266   146.1KB/s   00:00 
  • When the transfer is over, exit from sftp:


sftp> quit
  • Now, have your partner log in to his/her account and type the command below. He/she should see the hw2a.py file.


 ls                (that's ell ess)