Difference between revisions of "CSC111 Homework 2 2011"

From dftwiki3
Jump to: navigation, search
(Problem #2)
(Problem #3)
Line 223: Line 223:
 
>>>  
 
>>>  
 
Enter a positive number less than 20: 5
 
Enter a positive number less than 20: 5
Printing a diamond of 5 lines
+
 
 
     *
 
     *
 
   ***
 
   ***
Line 236: Line 236:
 
>>>  
 
>>>  
 
Enter a positive number less than 20: 4
 
Enter a positive number less than 20: 4
Printing a diamond of 4 lines
+
 
 
   *
 
   *
 
   ***
 
   ***
Line 247: Line 247:
 
>>>  
 
>>>  
 
Enter a positive number less than 20: 0
 
Enter a positive number less than 20: 0
Printing a diamond of 0 lines
+
 
 
>>> ================================ RESTART ================================
 
>>> ================================ RESTART ================================
 
>>>  
 
>>>  
 
>>>  
 
>>>  
 
Enter a positive number less than 20: 1
 
Enter a positive number less than 20: 1
Printing a diamond of 1 lines
+
 
 
*
 
*
 
>>>  
 
>>>  

Revision as of 15:30, 20 September 2011

--D. Thiebaut 13:29, 20 September 2011 (EDT)


Page under construction!
UnderConstruction.jpg

Problem #1

The text below shows you two columns of text. The column on the left is a short story from Fulghum's book "All I really need to know I learned in Kindergarten." You already had a taste of it in the lab. Now you'll have to straighten out the whole story! The right column contains pointers that will help you correct the text on the left.

  • The first thing to do is to get a copy of the file to edit. Get it using "getcopy," as follows:


  [111a-xx@beowulf ~]$ getcopy fulghum.long
  • Check your directory to make sure that the file is there. To do so, use the ls (ell ess) command as follows:


  [111a-bz@beowulf ~]$ ls      (the two letters are ell and ess)
  • . Once you see the file fulghum.long in your directory, edit it with emacs:
  [111a-bz@beowulf ~]$ emacs fulghum.long
  • Using the information that is provided to you in the right column below, modify the file. If at some point you really mess up and you want to start over again, you can get a brand new copy of the file by retyping the getcopy command. Don't hesitate to work in the lab during one of our Teaching Assistants weekly hours, so that they can help you getting acquainted with Emacs.
Now                                                 |Join the lines together
    let                                             |(c-e) (c-k)
        me                                          |
tell you about Larry Walters, my hero.  Walters is  |
is is is is is is is is a truck driver,             |
thirty-three years old.  He is sitting in his lawn  |
chair in his backyard, wishing he could fly.  For   |
as long as he could remember, he wishing he go up.  |
as long as he could remember, he wishing he go up.  |< several copies of the
as long as he could remember, he wishing he go up.  |  same line here. Delete
as long as he could remember, he wishing he go up.  |  them with c-k
as long as he could remember, he wishing he go up.  |
To be able to just rise right up in the air and     |
see for a long way.  The time, money, education,    |
and opportunity to be a pilot were not his.  Hang   |
gliding was too ~!@#$%^&*()_+ dangerous, and any    |< Extra characters here!
good                                                |  remove them! (c-d)
place                                               |< Here again, Join lines
for                                                 |  together
gliding                                             |
was too far away.  So he he he he he he he          |< "he" repeated too many
spent a lot of summer afternoons sitting in his     |  times.  Use Esc-d
backyard in his ordinary old aluminum lawn          |
chair--the kind with the webbing and nails.  Just   |< Change word "nails"
like the one you've got in your backyard.           |  and replace by "rivets"
                                                    |
THE NEXT CHAPTER IN THIS STORY is carried by the    |< delete words and retype
newspapers and television.  There's old Larry       |  them in lower case
Walters up in the air over Los Angeles.  Flying at  |
last.  Really getting UP there.  Still sitting in   |
his aluminum lawn chair, but it's hooked on to      |
forty-five helium-filled surplus weather balloons.  |  In the lines below, find
Larry has a parachute on, a CB radio, a six-pack    |  every occurrence of ??
of beer, some peanut butter and jelly sandwiches,   |  and replace it with the
and a BB gun to pop some of the balloons to come    |  word indicated in the
down.  And instead of being just a couple of        |  right column.
hundred feet over his neighborhood, he shot up      |  Use c-s to search for
eleven thousand feet, right through the approach    |  ??
corridor to the ?? International                    |> ?? --> Los Angeles
Airport                                             |
                                                    |
Walters is a ?? man.  When asked by the             |> ?? --> taciturn
press why he did it, he said: "You can't just sit   |
there." When asked if he was scared, he answered:   |
"Wonderfully so." When asked if he would do it      |
??, he said: "nope." And asked if he was glad       |> ?? --> again
that he did it, he grinned from ear to ear and      |
said: "Oh, yes." The human race sits in its ??.     |> ?? --> chair
On the once hand is the message that says there's   |
nothing left to do.  And the Larry Walterses of     |
the earth are busy tying balloons to their chairs,  |
directed by dreams and imagination to do their      |
thing.  The human race sits in its ??.  On the      |> ?? --> chair
one hand is the message that the human situation    |
is hopeless.  And the Larry Walterses of the earth  |
soar upward knowing anything is possible, sending   |
THIS WORDS SHOULD BE DELETED back the message from  |> Delete 5 words
eleven thousand feet: "I did it, I really did it.   |
I'm FLYING!"                                        |
                                                    |
It's the spirit here thAt counts.  The time mAy be  |> In this last chapter
long, the vehicle mAy be strAnge or unexpected.     |  all lower case letters
But if the dreAm is held close to the heArt, And    |  "a" have been replaced by
imAginAtion is Applied to whAt there is close At    |  uppercase letters "A".
hAnd, everything is still possible.  But wAit!      |  To change all of them at
Some cynic from the edge of the crowd insists thAt  |  once, position the
humAn beings still cAn't reAlly fly.  Not like      |  cursor on the first line
birds, AnywAy.  True.  But somewhere in some        |  and type the command:
little gArAge, some mAniAc with A gleAm in his eye  |
is scArfing vitAmins And minerAl supplements, And   |  ESC-X replace-string
prActicing flApping his Arms fAster And fAster.     |  (enter) A (enter) a
                                                    |
Robert Fulghum.                                     |
  • When you are finished, leave the editor (c-x c-c), and you are done! Make sure you have reconstructed the text to what its original form should be. You will be given a tool during the week to compare your file to the original. More details to come in class!


  • Note: You can test your file against the solution file by typing the following command at the prompt:
 [111a-xx@beowulf ~]$  testhw1 fulghum.long
The checking program will compare your file against the original story, and will show you where they are different. Here is an example:
 [...]
 THE next chapter in this story is carried by the              | The next chapter in this story is carried by the
 newspapers and television.  There's old Larry Walters up in   | newspapers and television.  There's old Larry
 the air over Los Angeles.  Flying at                          | Walters up in the air over Los Angeles.  Flying at
 last.  Really getting UP there.  Still sitting in               last.  Really getting UP there.  Still sitting in
 his aluminum lawn chair, but it's hooked on to                  his aluminum lawn chair, but it's hooked on to
 forty-five helium-filled surplus weather balloons.              forty-five helium-filled surplus weather balloons.
 
 [...]
The output of the program testhw1 compares your file (on the left) to the original text (on the right), and flags the line or lines that differ with a |-sign (vertical bar) or a >-sign (greater than). This should help you figure out where you have more editing to do.
Once the output does not contain any vertical-bar or greater-than signs, you are done.


  • Submit your file for it to be graded as follows:


        submit hw2 fulghum.long
  • This will create a copy of your file in your instructor's computer account. You can submit a file several times, as long as it is done before the deadline!

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 as possible, to the program I have written. In particular, notice that when I entered 0, it didn't print anything. Your program should do the same. When I entered 1, it printed one line. When I entered 2 it printed 2 lines. 3, 3 lines; 10, 10 lines. Your program should work the same way.

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.

Program Header

  • Make sure you include a header at the top of your program. Here is an example:



# hw2b.py
# 111a-xx   Jane Hill
# 111a-yy   Jill Hane    (working in pair programming)
# this program prompts the user for a number and displays
# a number of lines corresponding to that number.
# Example of user interaction:
# How many lines do you want printed? 3
# *
# *... **
# **...... -***
#
 
def main():
    ...
    ...

main()


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

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
>>> ================================ RESTART ================================
>>> 
Enter a positive number less than 20: 4

   *
  ***
 *****
*******
 *****
  ***
   *
>>> ================================ RESTART ================================
>>> 
Enter a positive number less than 20: 0

>>> ================================ RESTART ================================
>>> 
>>> 
Enter a positive number less than 20: 1

*
>>> 


Some Hints

Play with the following two loops... they might be useful to you...


for i in range( 7 ):
   print( i )
print( '-' * 10 )
for i in range( 7, 0, -1):
   print( i )

Submission

  • Submit your program on beowulf or emmy as follows:
 submit hw2 hw2c.py

Problem #4: Old Mac Donald's Farm (in Python!)

For this problem, I am asking you to stretch your logical abilities, and to learn new features that are the extension of constructs we have seen, and to apply them in a logical way in a situation we have not seen yet. 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

  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 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