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
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.
compsci101
). You can put your workspace anywhere, but you will need to remember where it is later in the semester.http://www.cs.duke.edu/courses/compsci101/fall12/snarf
101fall12_lab01
has been created you are ready to test your installation and experiment with Python code.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!
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.
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.
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.
[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?
functionToGraph
to the following:
return random.randint(0,20)What do you see and why? Note this on the handin pages
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
walk
. How many
Python functions are defined
in the LabWalk
module? What are their names?
pos
to simulate
the bug moving to the right (increasing the value of its
location).
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.
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?
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?