Exercise 0: Introduction to Programming

Take your time and read everything thoroughly! There are a lot of parts to this week's lab, and you don't want to miss anything important.

During this lab, you will:

  1. Review lab etiquette.
  2. Set up your CS account.
  3. Review academic conduct policy (there is a SURVEY).
  4. Read and interpret Python code (there is a QUIZ).
  5. Write and submit a Python program.
  6. Play Light Bot and submit your results.

Welcome to CS 301!

The goal of this lab exercise is to get you acquainted with the Computer Sciences (CS) labs and your CS account. We'd also like to get you using Python on the CS machines or your own machine, and give you some warm-up problem solving exercises. So let's get to it!

First, some lab etiquette. The labs in the CS building are maintained by the Computer Systems Lab (CSL) and we love them and want to keep them happy. Here's what you can do to help:

  • Eat and drink outside of the lab. Spills happen, don't risk it. (Also it's prohibited.)
  • If a computer isn't working, switch computers, notify a TA, and send an email to lab@cs.wisc.edu with the name of the computer (written on the computer's case), what the problem is, and when it started.
  • Don't turn off or reset a computer. The lab network requires a specialized restart sequence - let the CSL do it.
  • Log off when you're done by clicking the "Log off" button in the start menu, pictured below:

Setting up your CS account

As a registered (i.e., not auditing or sitting in) CS 301 student, you are provided an account on the CS lab machines. You'll need to activate that account in order to use the computers in the CS building, but it will also grant you (some) storage and webspace on the CS computers, as well as a (limited) printing quota and an email address you can use.

You can either activate your account in the CS labs or through the CSL website by clicking on the "Activate Account" link at the very top of the page and following their instructions.

Appropriate academic conduct for CS 301

For many of you, this is your first exposure to programming in an academic setting (or at all!). I'd like to make expectations as far as appropriate academic conduct as clear as possible, so no one ends up accidentally violating the academic misconduct policies of the university.

Go to CS 301 on Learn@UW and click on Surveys.

Take a few minutes to read over the policy in the Appropriate Academic Conduct survey and enter your full name to sign electronically. Note that if you are working in a pair in the lab, both members of the pair will need to sign the policy separately, on their own Learn@UW accounts.

Note: if you skip this step, you'll lose a point on this homework.

Quiz: Interpreting functions

This part is to be completed individually by noon on 28 January.

Go to CS 301 on Learn@UW and click on Quizzes.

There is one quiz available to you, called "How do functions work?" You'll have four attempts to get as many points as you can (all questions are worth half a point). This is a problem-solving exercise; I don't expect you to know how Python functions work, but I do expect you to be able to reason through it given what you've learned in your math courses up to now.

All questions in the quiz are short answer, and all questions about values should be given as numerals (i.e. "5", not "five").

Getting started with Python

When you registered for 301, you chose an (optional) weekly lab section. These labs are held in the CS building and on CS-provided Windows computers, but you may choose to complete these exercises outside of the scheduled lab time on your personal computer. You'll only have a TA available to help you out with these assignments in lab or office hours, so choose wisely!

Not in the labs? Here's how to set up Python on your own computer.

In the CS labs, you'll be using the integrated development environment (IDE) Eclipse, with the Python-specific plug-in PyDev. If you're working on your personal computer, it can be much simpler to just get the spin-off version LiClipse, which comes pre-configured for Python. The link above walks you through getting all of this set up on your personal computer.

Get LiClipse configured on your computer: here's how!

Now that your IDE is configured, it's time to actually write some code:

  • Create a new PyDev project, and call it Lab 0. In Lab 0, create a new PyDev module and call it four_fours (that's an underscore character, not a space!).
  • Into your new empty file, type or paste

    print "Zero is", 4+4-4-4
  • Try running it (Run > Run As > Python Run). You should see

    Zero is 0
  • Here's the challenge! Add five more lines of code to your file similar to this one, in which you use arithmetic operations, +, -, *, /, parens ( and ) and ** (power) to compute the values 1 through 5 using exactly four fours on each line. You may use 44, counting as two fours, or 4., the floating-point value of four, though neither of these is required to succeed with 1 through 5. Here are what the output - but not the source code - will look like:

    Zero is 0
    One is 1
    Two is 2
    Three is 3
    Four is 4
    Five is 5
  • Hint: I found the power operator very helpful. If you're frustrated that you don't have factorial or square root, don't worry, they're there - you don't need them for 1 through 5, but here's how to use them if you want to try:

    import math
    print "4! is", math.factorial(4)
    print "the square root of 4 is", math.sqrt(4)


    If you want to try more numbers beyond 1 through 5, do it!! Only 1 through 5 are required for this lab, though, so if you're frustrated you don't need to keep going. I think it's a fun mental challenge, though!

Make sure you're saving your work frequently as you go.

Exploring problem solving

Let's do some more complex problem-solving. "But Hobbes, that sounds like worrrrk!" No, that sounds like playing flash games.

That's right, you're going to play a flash game for homework - specifically Light Bot. Play through as many levels as you can (there are 12, so it's totally possible to beat the game). You'll be including your highest successful Light Bot program as part of this week's assignment!

Light Bot's programs are pictorial, but you'll be submitting yours in writing. Here's the translation:

  • = F
  • = R
  • = L
  • = J
  • = !
  • = 1
  • = 2
So a program that consists of:


would be represented in writing as

FFRF!

Take notes as you play - as soon as you successfully complete a level, you're taken to the next one and you won't be able to copy down your last program.

When you're done playing, add your highest-level program to the end of your four_fours.py file in this format (substitute your own highest level and code in the appropriate places):

# Highest level: (level number)
#
# Main: (main code)
# F1:   (function 1 code, if any)
# F2:   (function 2 code, if any)

These "hashtagged" (starting with #) lines are called comments, and are completely ignored by the interpreter when you're running a program. If you added these lines correctly, try running four_fours.py now and note that they had absolutely no effect on the outcome of your program. Comments are great that way!

A note on academic misconduct. The purpose of this portion of the assignment is NOT just to get the answer, it's to get practice problem-solving in a low-pressure, fun situation. Solving these problems on your own is a way to help yourself as the course progresses. If you feel you need to cheat to complete this assignment, maybe this course isn't the right one for you right now.

Submitting your file

At the top of your four_fours.py file, add your name (and if you coded with a partner, their name as well) as comments, like this:

# Author:       (your name)
# Pair Partner: (your partner's name)

You'll be handing in your lab work via the course Learn@UW dropboxes. Navigate to our 301 course page, and click the Dropbox link in the top navigation bar. You should see a dropbox for Program 0 - this is where you should hand in your four_fours.py file. (If you worked in a pair, only one person will need to hand in the code.)

Note that the dropbox will close at noon on 28 January, so be sure to submit your files before then.

Summary

During this lab, you should have:

  1. Reviewed lab etiquette.
  2. Set up your CS account.
  3. Filled out the academic conduct policy survey.
  4. Completed the Python code quiz.
  5. Written and submitted a Python program.
  6. Played Light Bot and added your algorithm to your program file as comments.

If you missed something, GO BACK AND DO IT AGAIN.