Post Midterm, Compsci 6, Fall 2010 Current tools for structuring data

Download Report

Transcript Post Midterm, Compsci 6, Fall 2010 Current tools for structuring data

Post Midterm, Compsci 6, Fall 2010

Current tools for structuring data



Current tools for organizing code



Types: int, boolean, string, float, list, file, tuple
This week and next: set and dictionary collections
Modules, functions, for loop, selection
This week and next: while loop
Why do we study tools for programming?

Facilitates solving problems in a variety of settings
Compsci 06/101, Fall 2010
8.1
What do sets get us?
Finding unique/different words in a file
def listUnique(filename):
file = open(filename)
words = []
for line in file:
for w in line.strip().split():
if not w in words:
words.append(w)
file.close()
return words

Compsci 06/101, Fall 2010
8.2
What do sets get us?

Finding unique/different words in a file
def setUnique(filename):
file = open(filename)
words = set()
for line in file:
for w in line.strip().split():
words.add(w)
file.close()
return words
Compsci 06/101, Fall 2010
8.3
Set details and examples

Set methods (see book)




add, clear, difference, intersection, issubset, issuperset,
remove, symmetric_difference, union
Short cuts/operators: -, &, <=, >=, |, ^
What are these?
Two-dimensional random walk on a grid



Why useful?
How to model?
How to view?
Compsci 06/101, Fall 2010
8.4