1 Getting Ready For Final Exam
1.1 Motivation
1.2 Available Artifacts
1.3 Final Review For Exam
1.4 Question 1: Short Python Facts
1.4.1 A question type that won’t appear on test
1.4.2 Another question that won’t appear
1.4.3 Final question that won’t appear
1.4.4 If vs. While
1.5 Question 2: Short Python Program
1.6 Question 3: Hand-Execute Python Statements
1.6.1 Ok, so this won’t appear either
1.7 Question 4: Debugging Challenge
1.8 Question 5: Short Python Program
1.9 Version : 2014/ 03/ 06

CS 110X Mar 05 2014

Lecture Path: 25
Back

TA Evaluation
Office hours: 1PM (FL-B20), 2PM (FL-320)
Online tonight: 8:30 or possibly 8:45

If we shadows have offended,
Think but this, and all is mended,
That you have but slumber’d here
While these visions did appear.
And this weak and idle theme,
No more yielding but a dream,

Gentles, do not reprehend:
if you pardon, we will mend:
And, as I am an honest Puck,
If we have unearned luck
Now to ’scape the serpent’s tongue,
We will make amends ere long;

Else the Puck a liar call;
So, good night unto you all.
Give me your hands, if we be friends,
And Robin shall restore amends.
A Midsummer Night’s Dream

1 Getting Ready For Final Exam

1.1 Motivation

Here’s a video about coding. I couldn’t have said it better myself.

1.2 Available Artifacts

Homework solutions are being graded and should be all posted by tonight. Lab 6 solutions have all been posted. Clickers data has been uploaded. I am still awaiting the final lab section for Lab07 and will attend to that after the exam. If you have not done so yet, make sure you turnin your lab07 by Saturday.

1.3 Final Review For Exam

Five Questions. Five by Five. Are you ready?

1.4 Question 1: Short Python Facts

You are given the following variables:

list = [1, 2, 4, 0, 3] val = 3 str = ’Worcester’

What is the value of the following expressions:

On this last one, does the value of list change in the above expression?

Speaking of changing values, what does the following do?

>>> x = 10 >>> y = z = x

1.4.1 A question type that won’t appear on test

While I won’t ask the following kind of questions on the exam, this is a good test of your knowledge.

>>> list = range(5) >>> XXXXXXXXXX >>> list [0, 1, 2, 0, 4]

In the above code, you can’t read the statment that has been X’d out. Can you come up with some possibilities?

1.4.2 Another question that won’t appear

Given list = [1, 5, 3, 2, 4] what single python statement can you perform to turn list into a 3-element list of ascending values?

1.4.3 Final question that won’t appear

Given str definition above, what is the value of str[2:7][0] + str[2:7][2:]

1.4.4 If vs. While

Make sure you understand the difference between if and while. In English, they can often be interchanged, but never in programming.

... if x == 0: print ("x is zero.")

The above prints out the statement "x is zero" if x == 0. However, change this to:

... while x == 0: print ("x is zero.")

And the program runs forever without halting. Be sure you know the difference.

1.5 Question 2: Short Python Program

I’ll then have you write a small Python program to address a specific functional request. Lab 7 demonstrated several examples of the kind of questions you will likely see.

Let’s try the following question. Can you write a program that returns the reverse of a string?

>>> str = ’sample’ >>> str = reverse(str) >>> str ’elpmas’

Let’s try this in an open setting. There are several ways to tackle this one.

1.6 Question 3: Hand-Execute Python Statements

To hand-execute code, be sure to pay attention to the value of all variables and update them as you proceed.

When there are loops, identify the potential values in the range so you know in advance (when possible) how many times it is going to execute.

Let’s get started.

>>> list = [1, 2, 3, 4, 5] >>> del list[:]

What value does list have after the above? Work it out. You are asking to delete the sub-slice that is equal to the contents of the whole list. In that case, list becomes [] the empty list. Let’s keep going.

>>> list = list + list

what value does list have? Well, since list is the empty the list, adding it to itself leaves the empty list.

Let’s move ahead with a longer sequence of code:

list = [1, 2, 3, 5, 4] list[3],list[4] = list[1],list[2]

What is the value of list? Note the use of multiple simultaneous assignment.

How would you swap two values in a list? Here we swap the 5 and 4.

list = [1, 2, 3, 5, 4] list[3],list[4] = list[4],list[3]

And let’s put a number of these together:

list = [1, 2, 3] list = list + list list.remove(2) del list[2] list.remove(2) del list[2]

What value of list now?

1.6.1 Ok, so this won’t appear either

What values are printed?

list = [3, 2, 1] for idx in range(list[list.count(0)]): print (idx)

1.7 Question 4: Debugging Challenge

def remove3 (list, filter): """ Remove elements from list that also exist in filter. """ for idx in range(len(list)): if list[idx] in filter: del list[idx]

1.8 Question 5: Short Python Program

The last question will have you write a Python program given a Python function that I provide to you. This is a final test of your ability to write code fragments that serve a greater purpose, rather than low-level data management.

1.9 Version : 2014/03/06

(c) 2014, George Heineman