CS 1004 Aug 06 2014
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 Real Estate Transaction Case Study
In today’s lecture, we will complete the case study and reflect on its design.
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 Real Estate Transactions case study
This case study gives you the opportunity to explore using CSV files to store information used by Python programs. This makes it easier to write your programs, because you don’t have to encode the information in your program. Additionally, it shows you how to more closely integrate with real world problems.
You have seen how to:
Load up entries in a CSV file into a LIST-OF-RECORDS structure
Process entries to extract information from a single column
Create Pivot Table (day17) to show a two dimensional relationship between the number of bedrooms and number of baths
Depict histogram based on aggregate totals
To complete this case study, let’s write a program to print a summary report of crimes (by total) within a given distance of a latitude,longitude coordinate.
def report (longitude, latitude, crimeRows, distance): """ Print summary report of crimes (by total) within given distance in miles. """ # columns for Longitude, Latitude, CrimeDescr cols = [8, 7, 5] tally = {} for idx in range(1,len(crimeRows)): row = crimeRows[idx] crimeLong = float(row[cols[0]]) crimeLat = float(row[cols[1]]) if distanceMiles(latitude, longitude, crimeLat, crimeLong) <= distance: crimeType = row[cols[2]] if crimeType in tally: tally[crimeType] += 1 else: tally[crimeType] = 1 for entry in tally: print (str(tally[entry]) + ’\t’ + entry)
Please review full solution to see the helper methods.
There is a video to help review the above implementation.
1.4 Final Exam Questions
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 hour. At the end of each of the remaining lectures, I will present a question that was deemed to be "too hard" to appear on the exam. However, these are very useful to work on, to ensure you know the material.
1.5 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.6 Version : 2014/08/06
(c) 2014, George Heineman