Problem Set 1: Getting Started
Assigned: Tuesday January 19, 2016
Due: Tuesday January 26, 2016
Points: 6 Points
System Setup
This part of the problem set requires you to set up your
computer for CS101.
- If you haven't done so already, you'll want to
bookmark the
course homepage. You'll also find it useful to
bookmark the page for
Python's
Standard Libraries.
- Create a new folder cs101 in some convenient
place. This is where you'll store the Python code that
you create in this course.
- In this class we'll be writing our programs using
Python version 2.7.8 together with the standard IDLE
development environment
(Note that there is a "newer" version of Python, Python
3.3.2, but we will not be using Python version 3.3 in this
course, we will be using Python 2.7.8)
Version 2.7.11 of Python/IDLE for either Mac or Windows
can be
downloaded from
here. Once you've found the right version and downloaded
it, double-click it to install it.
If you're using Windows (7 or 8), the installer should place the
Python installation on your C drive in a new
folder C:/Python27.
If you're on a Mac using OS X, you'll find a new Python
2.7 folder in your Applications folder. You'll
probably want to open that Python2.7 folder and drag
the IDLE icon to your Dock.
Fire up the IDLE application. You should see the
Python Shell:
Try typing a few expressions in the shell to see how Python
evaluates them.
- We'll be using several custom libraries of code this
semester. The first, a graphics library called
stddraw.py, was written by TA staffer from last
spring.
Download stddraw.py
leaving it on your desktop for the time being.
Python programs access libraries by using an import
command as in:
import stddraw
When Python executes this command it will look for the file
stddraw.py in several folders. Among the folders that it
looks in is the default folder called site-packages.
On a Mac, use the finder to navigate to
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/
You should see the site-packages folder. Drag the
stddraw.py file from your desktop to this
site-packages folder.
On Windows, you should find the installation
folder Python27 at C:/Python27. Move
the stddraw.py file from your desktop to
Python27/Lib/site-packages.
Try evaluating import stddraw in the Python Shell. If
you get an error, something is wrong with your installation of
the stddraw.py library. If you get no error, you're all
set.
Problems
Each problem is worth 1.5 points.
Navigate to your cs101 folder and create a new folder
named with your surname followed by the digit 1. (My
folder would be called Muller1.)
Return to the Python Shell. Under the File menu item
select New Window (on a Mac, you can use the Cmd-N
shortcut. On Windows you can use Ctrl-N.) The idea here is that
you are creating a text file containing the definitions of some
Python functions. The .py distinguishes this text file
from other kinds of text files. Type your functions
definitions. When you think your functions are good to go, you
can save the file and then use the F5 key to ship your
definitions to Python to be evaluated. Once Python has
successfully evaluated the definitions, you can try them out
in the Python Shell.
The first 4 lines of your file should be:
# file: problemset1.py
# author: your name
# date: the date
#
Every .py file that you create in this course should start with
4 lines like these. The next two lines of your program should be
exactly:
import math
from stddraw import *
These lines inform Python to retrieve the math and
stddraw libraries.
Under the file menu, Save the file under the name
problemset1.py in the folder that you just created.
(I would save my problemset1.py file in the folder
Muller1.)
Finally to some actual progamming!
- Write a python function helloWorld. The
helloWorld function accepts no arguments
and simply return
the string 'Hello World!'. Note well, your function
should return the string 'Hello World!' using
the return statement. It should
not print the string, just return it.
Once you've written the code, save it to the file then
ship your function definition to the Python Shell by
hitting the F5 key. Try your function out by
calling it from the shell prompt as in:
>>> hellowWorld()
Hello World!
- The golden
ratio is computed by
(1 +
√ 5
) / 2.0.
Write a python function goldenRatio that accepts
no arguments and returns the golden ratio. The function
math.sqrt can be used in computing the result.
For example,
>>> goldenRatio()
1.618033988749895
- Write a python function hypotenuse that
accepts a two floating point numbers base
and height giving the base and height of
a right-triangle and uses the Pythagorean Theorem to
return the length of the hypotenuse.
- Cut and paste the Python function yellowCircle
below into your problemset1.py file.
The yellowCircle function accepts a floating
point number radius between 0 and .5 and
uses the stddraw library to draw a circle
in the graphics window. The function is currently
misbehaving, it is drawing a green circle. (Try it!)
Fix it by making it draw a yellow one instead.
def yellowCircle(radius):
picture = Picture()
red = 0 # All colors can be made with red, green and blue,
green = 255 # with values ranging from 0 to 255.
blue = 0 # What values of red, green, blue give yellow?
color = picture.makeColor(red, green, blue)
picture.filledCircle(.5, .5, radius, color)
picture.start()
Once you have all of your functions working, exit Python,
compress your folder and upload it to the appropriate
folder on the Canvas website.
|