Due at 11:59pm on 09/04/2016.

Starter Files

Download lab00.zip. Inside the archive, you will find starter files for the questions in this lab, along with a copy of the OK autograder.

Submission

By the end of this lab, you should have submitted the lab using ok. You may submit more than once before the deadline; only the final submission will be graded.

  • Please complete Question 1, twenty_sixteen, in lab00.py.

Terminal, Python, Text Editor

A terminal is a program that allows you to interact with your computer by entering commands. Python is the programming language we will be using in this course. A text editor is a program that allows you to view and edit files.

It is common to have a terminal, Python, and text editor downloaded on your computer when you are programming. If you go on to take other computer science courses, you may have to download these programs. For this course, however, you will not have to download any of these tools because we will be using Jupyter. In addition to the Python notebooks that you are using in Data 8, Jupyter also provides these other programs. One benefit of using Jupyter is a common programming environment regardless of what operating system you are using. In addition, Jupyter stores your work on the cloud so you can complete assignments from any computer.

  • To access Jupyter, log in to the account that you are using for Data 8 using a Berkeley email.
  • If you see the My Server button, click on it.
  • Use the New drop down at the top right to open a Terminal.

You should now have two tabs open: the folder view and the terminal. You will need to switch between both of these tabs when you complete your assignments for CS 88.

In Data 8, you work mostly with Jupyter notebooks and avoid dealing with Python files, the terminal, and many other aspects of software development. That is where CS 88 differs. In this course, you will learn to write and run programs made of many files using the terminal! Before we start writing code, we will learn a bit more about how to use the terminal.

Organize your files

There are many terminal commands that you will be using in this course.

Directories

The first command we'll use is ls. Try typing it in the terminal:

ls

The ls command lists all the files and folders in the current directory. A directory is another name for a folder (such as the Documents folder). Since we're in the home directory right now, you should see the contents of your home directory.

Making new directories

Our next command is called mkdir, which makes new directories. Let's make a directory called cs88 to store all of the assignments for this class:

mkdir cs88

A folder called cs88 will appear in our home directory.

Changing directories

To move into another directory, we use the cd command. Try typing the following command into your terminal:

cd cs88

The cd command will change directories — in other words, it moves you into the specified folder. In the example above, we chose to move into the cs88 directory.

If we want to go back to our home directory, there are a few ways to do so:

  • Type cd .. (two dots). The .. means "the parent directory". In this case, the parent directory of cs88 happens to be our home directory, so we can use cd .. to go up one directory.
  • Type cd ~ (the tilde). Remember that ~ means home directory, so this command tells your terminal to change to the home directory, no matter where you currently are.
  • Type cd (that is, the cd command with no arguments), typing just cd is a shortcut for typing cd ~.

At this point, let's create some more directories. Make sure you are in the ~/cs88 directory, using the necessary cd commands. Then create projects and labfolders inside of our cs88 folder:

cd ~/cs88
mkdir projects
mkdir lab

Now if we list the contents of the directory (using ls), we'll see two folders, projects and lab.

Downloading the assignment

Next download the zip archive, lab00.zip, if you have not already done so. This zip file contains all the files that we'll need for this lab.

Uploading the assignment

The lab must be uploaded to Jupyter as a zip file; uploading a folder will not work. Switch from the terminal to the homepage and upload the zip file into the cs88 directory you created earlier.

Extracting starter files

Switch back to the terminal tab.

Use the command below to unzip the file.

unzip lab00.zip

Make sure you are in the cs88 directory in your terminal.

Once you unzip lab00.zip, you should have a new folder called lab00 in the cs88 directory which contains the following files:

  • lab00.py: The template file you'll be adding your code to
  • ok: A program used to test and submit assignments
  • lab00.ok: A configuration file for ok

You no longer need lab00.zip and can delete it from your cs88 folder if you want. You're now ready to start looking at the code! Don't worry if this seems complicated — it will get much easier over time. Just keep practicing! You can also take a look at our UNIX tutorial for a more detailed explanation of terminal commands.

Run Python

In your terminal window, you can run Python using the command below. We will be using the default version of Python 3.5.

  python

You should see something like

  Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:53:06)
  [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  >>>

The '>>>' is the prompt. If you type an python expression, it will evaluate it and print the answer. Since you have already seen cells in a jupyter notebook, you may think of this as a super duper cell. (Well, actually its the other way around - the shell came first.) Type some expressions at it and see what it does.

  >>> 2016
  2016
  >>> 2016+1
  2017
  >>> 2016*2
  4032

Type expressions using numbers, +, and * and see how fancy you can get that returns 2016

  >>>(2016+2)*2-2020
  2016

To exit, type exit() into the interpreter prompt. You can also use the keyboard shortcut Ctrl-D on Linux/Mac machines or Ctrl-Z Enter on Windows.

  >>> exit() 

The python command is the most basic way to run Python. Later in the lab, you will see how to run Python in more complex ways by adding certain flags to this command.

Understand the given code

Question 1: Twenty Sixteen

Open up lab00.py in your text editor. You should see something like this:

def twenty_sixteen():
    """Come up with the most creative expression that evaluates to 2016,
    using only numbers and the +, *, and - operators.

    >>> twenty_sixteen()
    2016
    """
return ______
return ((13 * 31 + 100) * 4) + 4

The lines in the triple-quotes """ are called a docstring, which is a description of what the function is supposed to do. When writing code in CS 88, you should always read the docstring!

The lines in the docstring that begin with >>> are called doctests. Doctests explain what the function does by showing actual Python code. The lines underneath the >>> show the expected output from running the above Python code.

In twenty_sixteen,

  • The docstring tells us to "come up with the most creative expression that evaluates to 2016," but that we can only use numbers and arithmetic operators + (add), * (multiply), and - (subtract).
  • The doctest for twenty_sixteen() checks that no matter how we do our calculation, twenty_sixteen should return the number 2016.

You should never change the doctests in your assignments! The only part of your assignments that you'll need to edit is the code.

Write your own code

Once you understand what the question is asking, you're ready to start writing code! You should replace the underscores in return ______ with an expression that evaluates to 2016. What's the most creative expression you can come up with?

  • Open lab00.py in your text editor. Start by simply returning 2016. This is not very creative, but it gives the right answer.
  • Run Python on your code in lab00.py using the interactive flag. When you get the prompt, type the name of the function, just like in the doc string. Did you get the right answer?

    python -i lab00.py
    >>> twenty_sixteen()
    2016
    >>> exit()
  • Exit Python to get back to your terminal shell. You will need to do that each time you run Python interactively.

You are going to want to learn to test your code before submitting it to the autograder. This always begins with reading it, thinking about it and convincing yourself it should work. Come up with examples that verify it really does what it should. That is what goes into the docstring.

Test your code

In CS 88, we will use a program called ok to test our code. ok will be included in every assignment in this class.

Back to the terminal! Make sure you are in the lab00 directory we created earlier (remember, the cd command lets you change directories).

In that directory, you can type ls to verify that there are the following three files:

  • lab00.py: the starter file you just edited
  • ok: our autograder
  • lab00.ok: a configuration file for OK

Now, let's test our code to make sure it works. You can run ok with this command:

python ok --local

If you wrote your code correctly, we should see a successful test:

=====================================================================
Assignment: Lab 0
OK, version v1.4.1
=====================================================================

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Running tests

---------------------------------------------------------------------
Test summary
    Passed: 1
    Failed: 0
[ooooooooook] 100.0% passed

If you didn't pass the tests, ok will instead show you something like this:

---------------------------------------------------------------------
Doctests for twenty_sixteen

>>> from lab00 import *
>>> twenty_sixteen()
2013

# Error: expected
#     2016
# but got
#     ...

---------------------------------------------------------------------
Test summary
    Passed: 0
    Failed: 1
[k..........] 0.0% passed

Fix your code in the text editor until the test passes.

Submitting assignments

Now that you have completed your first CS88 lab assignment, it's time to turn it in.

Download the lab00.py file to your computer. Go to okpy.org and find the Lab 0 assignment under the CS 88 course. Click on Create a new submission and upload the lab00.py file.

Verify that your submission worked. You should see a successful submission for lab00.

Congratulations, you just submitted your first CS 88 lab!