1 Completing Racquetball Simulation
1.1 Manipulating Lists
1.2 Creating Order From Randomness
1.3 Completing Racquetball Assignment
1.4 Lab 6
1.5 Final Exam Questions
1.6 Assessment
1.7 Version : 2014/ 02/ 24

CS 110X Feb 24 2014

Lecture Path: 19
Back Next

Expected Readings: 275-279
Clicker: Assessment

For every finish-line tape a runner breaks – complete with the cheers of the crowd and the clicking of hundreds of cameras – there are the hours of hard and often lonely work that rarely gets talked about.
Grete Waitz

1 Completing Racquetball Simulation

In today’s lecture, we will return to the Racquetball Simulation, but first let’s discuss some administrivia and some new skills.

1.1 Manipulating Lists

So far you have seen how to construct lists by appending values, one at a time at the end.

>>> list = [] >>> list.append(3) >>> list.append(5) >>> list [3, 5]

Now what if you wanted to add a value to the front of the list?

>>> list.insert(0,99) >>> list [99, 2, 34]

This function, insert(index, value) inserts the value at that index location and the old value at that location and all higher-index values are pushed one higher to make room in the list.

Note if the index location is already higher than any location in the list, this translates into an append.

>>> list = [] >>> list.insert(1000000,99) [99]

With this ability to insert and append values into a list, it is time that you learned how to delete an element in a list. To do so, we use the Python keyword, del.

>>> list = [2, 4, 6, 8, 10] >>> del list[2] >>> list [2, 4, 8, 10]

Using del you can delete any element by its index location. Want to delete more than one element at a time? Use the slicing syntax that you have seen already.

>>> list = [2, 4, 6, 8, 10] >>> del list[3:5] >>> list [2, 4, 6]

On a final note, you can use del on nearly anything.

>>> x = 10 >>> x 10 >>> del x >>> x Traceback (most recent call last): File "<pyshell#105>", line 1, in <module> x NameError: name ’x’ is not defined

You have seen how to use del in an earlier lecture for deleting a (key,value) pair from a dictionary.

1.2 Creating Order From Randomness

Now that you are comfortable with pseudo-random numbers, let’s say you run a simulation and get an unexpected result? How can you recreate the exact sequence of "random" numbers?

In programming, the trick is that pseudo random number generators use a "seed" to start the computation. Provide the exact same seed, and you get the same random numbers in the exact same order!

>>> import random >>> random.random() 0.045762313071872174 >>> random.random() 0.9001084432222091 >>> >>> random.seed(2013) ### Set seed for future numbers >>> >>> random.random() 0.4251774387679699 >>> random.random() 0.051906837944063144 >>> >>> random.seed(2013) ### (re)set seed to same value >>> random.random() 0.4251774387679699 ### THIS IS THE SAME! >>> random.random() 0.051906837944063144 ### AND CONTINUES TO BE SO... >>> random.seed(2013)

1.3 Completing Racquetball Assignment

The final RacquetBall implementation is provided here, but I still encourage you to type it in, rather than using copy/paste from these web pages. There is something about the mechanistic process of typing code. Doing so repeatedly helps you when it comes time, say, on an exam to write a function definition.

We’ll go over this in class, with an emphasis on logical structure and iteration. This was a top-down implementation effort.

1.4 Lab 6

Lab six is now ready for you. Of course, you won’t be able to actually complete the lab without the Arduino hardware, but I think you should review the lab prior to actually showing up on Wednesday.

Lab6 will be done in pairs. You will only be able to receive credit for this lab by attending your lab section and signing the attendance sheet.

Again, not everyone will get a chance to complete this lab, because of the two lab sections scheduled at 10AM (DOH!) If you don’t complete Lab6, then be sure to do so on the final lab day of the class. I am currently completing a Lab7 that will also be available by Wednesday. Thus the goal is to ensure that everyone completes Lab6 and Lab7, though not everyone will do so in the same order.

1.5 Final Exam Questions

You will be allowed, once again, to bring in a single piece of 8.5" x 11" paper on which (front and back) you have hand-written any concepts in the course. Take advantage of this opportunity. In many ways, this sheet of paper will become the best study guide you could hope for.

We have seen a number of question types so far. There will be five questions on the final exam. The exam is cumulative, but still must be completed within the 50 alloted minutes.

1.6 Assessment

Given an integer value, you want to create a string with the appropriate commas at thousands increments.

addCommas(431) > 431
addCommas(3431) > 3,431
addCommas(2823431) > 2,823,431
addCommas(2123123123123823431) > 2,123,123,123,123,823,431

Here is a solution. This question is a bit too difficult for an exam, but would have been suitable on a homework.

# in class exercise def addCommas(val): """ Return string representation of value with commas inserted at thousands markers. """ base = str(val) if val < 1000: return base formatted = ” while len(base) > 3: lastThree = base[len(base)-3:] base = base[:len(base)-3] formatted = ’,’ + lastThree + formatted return base + formatted

1.7 Version : 2014/02/24

(c) 2014, George Heineman