Compsci 101, Fall 2012, Lab 1

Getting Up and Running with Python

If you haven't succeeded in getting PyDev, Eclipse, and Ambient working the UTAs will try to help you with that. However, you'll also need to get some conceptual work done that will help you understand Python.
  1. Install Python and the software we use to work with it. This will include running some programs to make sure Python is working for you.
  2. Working on problem solving with Python in a group of up to 3 students

If you have any questions about the course, post your questions to Piazza the course discussion forum. You should have been invited to Piazza on the first day, but you can add yourself -- you'll need to sign up for an account on Piazza using your Duke email address.

Turn in this page for your group

Install Python and related software

To write programs, you will need some software tools, just as writing a paper requires a wordprocessor (pretty much), writing software requires using other programs. These allow you to edit your program, translate it into a simpler representation that your computer can understand, and run it. These initial steps may seem a little overwhelming, but they only need to be done once to get things started and working together.

  1. Try to get Eclipse and Python installed before lab using the install pages as a guide. If you don't get this done in lab, find a UTA in the LINK or see one of the course instructors during office hours.

  2. Start Eclipse and create a workspace for all your CompSci work (like compsci101). You can put your workspace anywhere, but you will need to remember where it is later in the semester.
  3. Download (or "snarf") the project (code and related files) for Lab 1 through Eclipse and run it as discussed below.
    See this site for help with Snarfing. In general to snarf projects, click Browse Snarf Sites, then click Add a new project site. You should add this site:
  4. http://www.cs.duke.edu/courses/compsci101/fall12/snarf
  5. Once the project 101fall12_lab01 has been created you are ready to test your installation and experiment with Python code.

Experimenting with Python Code, Part I

To run the code, open up the project file in src called graphing.py by double-clicking on it, then use the green Play button on the toolbar. You may have to choose what kind of Python run to use depending on your Python settings (choose Python Run if you have a choice). Running the program should create and display a graph. You may get a small rocket-shaped icon representing the open graph. If that worked, everything is installed correctly!

You should see a graph that looks like the picture below.

picture

To experiment with the code, look at the file graphing.py, and try to figure out generally how it works. To direct your experimentation, do the problems below that require you to change the file graphing.py, run it to see the results, and write down your answers on the handin pages.

  1. Currently, the program plots the expression y = 3*x2 - 7x + 20 over the range, x-values, [0, 10]. Change the program so it graphs the same expression but instead between the x-values 0 and 20. This will require finding the part of the program that calls the function displayFunctionGraph and changing the first two parameters of the call.
  2. Change the program to plot the expression y = (x-4)3 over the range [0, 20].

    This will require changing the body of the function named functionToGraph. Note that to raise to a power use the ** operator, e.g., 2**5 is Python for 25.

  3. Change the program to plot a graph of y = sin(x) over the range [0,20]. To use the sin function you'll need to write math.sin(x) because sin is in the math library. How many complete cycles of the sine wave are there between 0 and 20 according to what you see?
  4. Graph a function where the y-value is chosen at random by changing the body of functionToGraph to the following:
        return random.randint(0,20)
    
    What do you see and why? Note this on the handin pages

APT

Snarf the gravity APT. Create a Python module named Gravity, in that module create a function falling with the appropriate parameters (two). Make the body of the function return 0.0 How many test cases does this pass? Note that on the handin sheet.


Python Code, Part II

Hopefully you'll have time for this. If not, no worries.

Run the program LabWalk that simulates and visualizes a one-dimensional random-walk. Random walks are used in scientific and financial simulations. A one-dimensional walk can be thought of as a bug or drunkard starting at the origin of a number line and walking left or right with equal probability, taking one step each time.

The program you're given shows a bug, with the letter 'B' in the console and prints the location of the bug and the time-step at which the location occurs in the simulation. Here's the beginning of a run, the Bug starts at the origin in the middle of the screen (that's the visualization. In the run below the bug walks right, then left (back to the origin) then right, right, and so on.

|                                        B                                       | 0 	1
|                                         B                                      | 1 	2
|                                        B                                       | 0 	3
|                                         B                                      | 1 	4
|                                          B                                     | 2 	5
|                                         B                                      | 1 	6
|                                        B                                       | 0 	7
|                                         B                                      | 1 	8
|                                        B                                       | 0 	9
|                                       B                                        | -1 	10

  1. Run the program twice. The value printed after the visualization is the final location of the bug. Write that down on the handin sheet.
  2. The simulation is done in the function walk. How many Python functions are defined in the LabWalk module? What are their names?
  3. Which line of code updates the variable pos to simulate the bug moving to the right (increasing the value of its location).
  4. Currently the function walk returns the distance of the bug from the origin at the end of the simulated walk. Create a variable zcrosses, initalize it to zero, and update it by increasing it by one whenever the bug's location is zero. zcrosses = zcrosses + 1 Put the statement above inside an if statement body that checks to see when the value of pos is zero, e.g., if pos == 0. Return the value of zcrosses instead of the bug's position. Run the program five times. What's the maximal number of times the bug is at the origin during the five runs?
  5. Uncomment the lines in walk that begin with a hashtag and have a call to plt.plot and plt.show. This means you should erase/remove the hashtags. What happens when you run the program?