WTP Computer Science

Problem Set 1

Due: June 29, 2010




Exercise 0: Setting Up

  1. Follow the instruction in the Introduction to Athena handout to log in to the computers with the username and password you received when you arrived.
  2. Run the following commands in the xterm window. Read the Athena handout if you can't find the window. The commands will add the lockers you need for the CS class.
    athena% add wtp-eecs
    athena% /mit/wtp-eecs/cs/env_setup.sh
    You have to press enter after each line.
  3. Log out.
  4. Log back in again and open the web browser. Go to the class web page and bookmark it. You will come back to it often. You will find links to the problem sets and solutions there.
  5. To make sure that everything is setup properly. Run the following commands:
    athena% getcopy welcome_wtp
    athena% ls      	
    You should see the file welcome_wtp listed. If you don't see it, talk to the staff.


Exercise 1: Getting started with Python and IDLE


Read section 1 through 5 in the "Getting Started with Idle" handout. Follow the instructions. You should be able to use the Python shell to print messages and make basic calculations. At the end, you should have created a file HelloWTP.py that will contain your first program.


Exercise 2: A Simple Program


Now create a new file and write a program that will print "Hello, <your name>!!!" in a box like this:

 

****************************************
* *
* Hello, Harry Potter!!! *
* *
****************************************
Hint: You will need to use more than one print command. You should put each command on a new line.

Save your file as hello.py.


Exercise 3: Comments


At the beginning, you will be writing fairly small programs and it will seem easy to remember everything you did. As the size of your programs grow, it will be more and more difficult. There will come a time when you will have to go back and look at code you have written a few months, or even a few years ago. It will feel as if someone else wrote it. Instead of having to understand what every line of code does, you can add comments to your code that explain what you were trying to do. You can add a comment using the # sign, e.g.

# Print a message to the screen

print 'Hello, WTP!!!'

The comments are ignored by the interpreter. They are only used by programmers for improving the readability of the code.

Now edit the program from the previous exercise and add a comment at the beginning of the file with the file name, your name, and a sentence about what it does. You should do that for all your future programs.

Print out your code and turn it in. Look at the How to Print handout for instructions on printing.

Then submit your code electronically by typing the following command at the athena prompt:

 athena% submit ps1 hello.py

Exercise 4: Using the Python Shell as a Calculator


In Exercise 1, we saw that we can use the Python shell as a calculator. The basic operations are:

 

Operator
Operation
+
Addition
-
Subtraction
*
Multiplication
/
Division
**
Exponentiation

Exercise 4.1 What is 5/2?

What does the Python shell output if you try to compute 5/2?



What about 5.0/2 ?



Interesting, no? You will find out why tomorrow.


Exercise 4.2 Exponentiation

Compute 2 to the power of 32. What answer does Python give you?




Exercise 4.3 Unit conversion

If you run a 5K race in 20 minutes 30 seconds, what is your average time per mile? What is your average speed in miles per hour? Use the Python shell to help you with the calculations and write down your answer.

Hint: There are 1.61 kilometers in a mile.

 






Exercise 5: Natural language processing


Note: This problem can be done at home if you run out of lab time.

Consider the following sentence:

Alice saw the boy on the hill with the telescope.

  1. Draw a sketch of what's described in this sentence.













  2. Draw a different sketch that could also be described by the sentence. What is the difference between this sketch and the one in part a?













  3. If you were writing a program that was trying to understand English, what question might you ask to clarify what the sentence means?







  4. The ambiguity illustrated by this sentence is known as "prepositional phrase attachment". We know that programming languages are designed to avoid ambiguity. Why do you think this is?










What to turn in


  1. Submit the electronic copy of the program you wrote in exercises 2 and 3. (Note: to submit type the following command at the athena prompt:)
     athena% submit ps1 hello.py
  2. Turn in your answers to exercises 4 and 5 on this sheet to one of the tutors.