ICS 31 • DAVID G. KAY • UC IRVINE • WINTER 2013
Lab Assignment 1
This assignment is due at the end of lab on Friday, January 11.
Preparation (Do this part individually, before coming to lab)
(1) Do everything on the course refrence sheet (the syllabus) under the heading "What to do this week to get started in ICS 31." This includes registering yourself at checkmate.ics.uci.edu and piazza.com, filling out the questionnaire at eee.uci.edu/survey/ics31.w13.q, and turning in your photo to your TA.
(2) Read through the Lab Work part of this assignment before you come to lab, to get an idea of what you'll be doing with your partner.
(3) If you haven't already, read the guidelines for pair programming. They describe how you will be doing all your work in the lab. Pair programming is not as simple as just working together; there are specific roles and specific things each person needs to do.
(4) We encourage you to talk with each other and help each other understand how to do the assignments. There are some limits, though; in particular, remember that knowing how to do it (when the quizzes and exams come, for example) is much more important than just putting down the answer. If you haven't already, read the guidelines for collaboration and independent work. You may also wish to read some general advice about how to approach college work; not all of the practices that served you well in secondary school will work well here.
(5) Read Chapter 1 of the Perkovic textbook; it provides some background, including some issues we talked about in class on Tuesday.
These "preparation" sections of each assignment will include the reading in the textbook and some exercises for you to try out as you read. People learn best when they apply what they read as they go, so it's best to read the book with a computer at hand so you can type things in and try them out. There's always a big gap between thinking you understand something you read and actually being able to apply it. We expect these exercises to go quickly, but if you find yourself spending more than a couple of minutes on an exercise without making any progress, you should ask about it (on Piazza, probably) rather than continuing to spin your wheels unproductively. Note that this also means you should get started early, so that you'll have the time to get any questions answered.
[Reading note: In Chapter 1 on page 3, the author characterizes different career paths in computing. But he mis-characterizes work in Human-Computer Interaction (HCI) when he says, "Artistic computer professionals design the interfaces that clients use to interact with an application." Designing interfaces is not primarily an artistic activity and many people who work in HCI have no particular artistic interest or ability. Instead, HCI designers are experimenters, observing how actual users perform when using a design and refining the design based on what they observe. UCI is a world leader in HCI research; ICS offers half a dozen undergrad courses in various aspects of HCI.]
(6) Read sections 2.1 and 2.2 of the Perkovic text. You don't have to do all of this by Wednesday, but we'll be talking about material in sections 2.1 and 2.2 on Thursday.
As you're reading, you will run into "Practice Problem" sections. Do at least two of the parts in each of these sections. It's important that you apply what you're reading; it's impossible to learn this material without actually working through it. If you have more time, or if you have trouble with these, you should do more of them. Especially early in the course, it may seem tedious and slow because you're just learning your way. But the effort here will pay off in a greater facility later (or, to put it negatively, failing to do this now will result in disappointing performance later).
We don't require you to turn in your answers to the practice problems; the solutions are at the end of the chapter. But don't just read the problem, turn to the solution, and say, "Yeah, that looks right." Write down your answer first; force yourself to work it through. That's how you learn.
Lab Work (Do this part with your partner in lab)
(a) Choose a partner for this assignment and make sure the TA knows who your partner is. Remember that you'll choose a different partner for each lab assignment, so you'll work with this partner only this week. Make sure you know your partner's name (first and last) and contact information (Email or cellphone or whatever) in case one of you can't make it to lab. All your computer-based work on this (and every) lab assignment must follow the pair programming guidelines.
(b) It's especially important that you be able to receive electronic mail sent to your UCInet ID (you@uci.edu). You may redirect this mail to another account of your choosing, but don't let that mail go unread. Redirecting is a good idea (the course reference sheet says how), since your UCI mailbox is small and when it fills up, messages sent to you get lost.
With your partner, test this out by sending each other Email at your UCI addresses. There's nothing to turn in for this part.
(c) Locate and launch the IDLE software.
IDLE starts up with a "Python Shell" window where we'll see the results of most of our programs. IDLE also lets you open up a program window; do this now, by choosing "New Window" from the File menu. Give that window a name right away and save it, by choosing "Save As" from the file menu and specifying the name lab1.py . (You can save it anywhere on your machine; just don't forget where you put it and remember to recycle/discard it after you're done so that the next student on your lab machine doesn't use your work by mistake.)
In your
lab1.py file, you can type print statements that contain expressions; when you run the code in the file (choose "Run Module" from the Run menu or press the F5 key), the results are printed in the Python Shell window. Try it: Type print('Hello') in your lab1.py file and then run it. You should see Hello
in the shell window.
[It is also possible to type expressions, without the print , directly into the shell window after IDLE's "prompt" of three greater-than signs (">>>"). This behaves a little differently, though, and most beginners find it confusing, so for now we're going to type all our Python code in a code window and run it when we're ready.]
Experiment with IDLE to get familiar with it, following the suggestions below. One person "drives" (types); the other "navigates" (observes, questions, suggests); if you don't know what this is about, go back and read the pair programming guidelines. The main point of this assignment is to get you familiar with the mechanics of IDLE, Python, and pair programming.
Try printing the values of some expressions, like print(123 + 456) and print(2 ** 5) (** is the operator for exponents) and print(5 * 4 * 3 * 2 * 1).
Type in some definitions, like number_of_students = 308 and number_of_staff = 18 and then try printing expressions like number_of_students / number_of_staff.
(That is, type the Python statement print(number_of_students / number_of_staff). At this point we'll just say "print the expresion X" and you'll know to type the Python statement print(X) in your lab1.py file and then run it.)
Probably you've already done this a few times: Type some things that Python can't evaluate and see what messages Python produces. (Error messages are not bad; they're an inevitable part of programming. Don't think of them as the computer yelling at you; it's just trying to tell you why it couldn't process something. The language of error messages can be cryptic, but you will learn how to pick through them for useful clues to what Python thinks the problem is.)
Save a copy of the Python Shell window that shows your work for this part (using Save a Copy As from the File menu). A good name for the file would be lab1c.txt; stick with a naming pattern like this for the rest of the quarter. It's fine if your interactions show false starts and mistakes; it's also fine if you just produce a short, clean copy. Edit the file to make sure it includes your name and your partner's at the top; submit this file via Checkmate. (Don't know what Checkmate is? Look at the "What to do this week" section of the course syllabus).
(d) Switch roles now, if you haven't already, so the navigator can drive and vice versa. Don't worry if the new driver is less familiar with Python or IDLE than the first driver. Both will get more experienced rapidly.
As we mentioned briefly above, the Python shell window can be useful for testing out small things, but it doesn't save the code we type, so as our code gets longer and more involved, we need the technique we've already started using: We create a new window into which we can type Python code to save as a Python (.py) file. That's the way we'll be doing our work this quarter.
The factorial function (written in mathematical notation with an exclamation point, so "n factorial" would be n!) is used in calculating how many ways there are to arrange things (like the number of different ways to arrange five students in a row). The value of n! is n · (n-1) · (n-2) · ... · 1, so 5! = 5 · 4 · 3 · 2 · 1 = 120.
Create a new window (from the File menu) and save it as lab1d.py. On the first line of the file type a pound sign followed by the names and IDs of both partners and some other identifying information, like this:
# Paula Programmer 11223344 and Andrew Anteater 44332211. ICS 31 Lab sec 7. Lab asst 1d.
Type the following function definition into your new window. Actually do the typing so you can get used to the way it works; don't just copy and paste. (We'll go over the details of this code in class some time soon; for now, this is just a typing exercise and you shouldn't try too hard to figure out how it works. If you're not used to typing programs, be careful to type accurately. In particular, the spacing at the beginnings of the lines matters in Python.) Watch what happens every time you type a right-parenthesis, and watch what happens when you hit Return after typing a colon.
def factorial (n: int) -> int:
''' Compute n! (n factorial) '''
if n <= 0:
return 1
else:
return n * factorial(n - 1)
assert(factorial(0) == 1)
assert(factorial(5) == 120)
print("10! is", factorial(10))
print("100! is", factorial(100))
When you're done, run the code (select Run Module from the Run menu or type the F5 key). Don't be surprised to get error messages; you've probably just mis-typed something, and programming languages are pickier than the pickiest human proofreader. Just use the messages to help you identify each typo until you get an error-free result.
Now try changing the print statement(s) to print expressions like factorial(120), factorial(50 * 10), and factorial(factorial(5)). Re-run the code to see what the results are.
Python can handle long numbers effortlessly, but any computer is finite; what do you think might happen if you evaluate factorial(factorial(50))? You can try it, but you'll have to stop the evaluation by selecting Restart Shell from the Shell menu.
Make sure both partners' names are at the top of the Python file you typed, save that file, and submit it via Checkmate. But before you submit it, run the file one last time. It should produce correct results. If it doesn't, correct the mistakes before you submit it. Whenever you submit a Python file in ICS 31, the file should produce correct results when you (or we) run it.
(e)
In IDLE, create a new file and save it as lab1e.py. When you have your personal ICS lab account, save the file on your personal H: drive (or on a removable thumb drive). If you save it on the common C: drive, someone else might pick it up later (by mistake, we hope, but it would still lead to a lot of awkward questions about academic honesty). Keeping personal information secure is a real-world skill; people who work with health and other personal information can get in huge trouble if they leave it open to unauthorized access.
On the first line of the file type a pound sign followed by the names and IDs of both partners and some other identifying information, like this:
# Paula Programmer 11223344 and Andrew Anteater 44332211. ICS 31 Lab sec 7. Lab asst 1e.
Now put in a couple of print statements that print some expressions—any expressions—and run the code in the file. Once you have this working, continue with the rest of this assignment. Type your solutions to the rest of the lab into your lab1e.py file, which you will submit via Checkmate. (It's a good idea if each partner keeps a copy of the lab work at the end of each session, just in case someone can't make it to the next lab.)
(f) At the end of Chapter 2, do the following exercises. Where the exercises say to write Python expressions or evaluate them in the shell window, instead you should put those expressions into your lab1e.py file and print their values.
Exercise 2.12: choose one part from (a) to (e); Exercise 2.13: chose one from (a) to (c) plus one from (d) to (f); Exercise 2.14: choose two of the letters listed; Exercise 2.15: choose three from (a) to (g).
Then do Exercise 2.16 (a), (b), (d), and (e); Excercise 2.17 (a), (c), (d), and (e); Exercise 2.21.
(g) Remember that each partner must complete a partner evaluation form and submit it individually. Do this by connecting to eee.uci.edu and choosing the Survey tool for your lab section of ICS 31. Make sure you know your partner's name, first and last, so you can evaluate the right person. Please complete your evaluation by the end of the day on Friday, or Saturday morning at the latest. It only takes a couple of minutes and not doing it hurts your participation score.
What to turn in: Submit via Checkmate the files you created for parts (c), (d), (e) and (f) above. It would be an excellent idea to go back and re-read the assignment carefully now, to make sure you've completed all the steps specified. Also remember that each student must complete a partner evaluation form by logging in to eee.uci.edu and selecting the Survey tool; these evaluations contribute to your class participation score. Get in the habit of doing this every week on Friday after you've submitted your assignment; the survey closes on Saturday.
Written by David G. Kay in Fal 2012 for ICS 31, based on materials from ICS H21, H22, and Informatics 41; modified by David G. Kay, Winter 2013.