Nov 8
Assignment for next week has been posted - look at it and discuss briefly.
dictionaries and counting
Say you have some things, like this :
things = ['cat', 'dog', 'cat', 'cat', 'dog', 'skunk']
# How would you count how many of each you have, in python?
# Well, how about a dictionary?
counts = {}
# one way
for thing in things:
if thing in counts:
counts[thing] += 1
else:
counts[thing] = 1
# a slightly better way
for thing in things:
counts[thing] = counts.get(thing, 0) + 1
print counts
randomly generated papers
We looked at these in passing:
list functions and randoms
It's a good time to look again at Python's list and
random methods, since they're particularly helpful
in the sorts of programming we're doing now.
Check out in particular
random.shuffle(list) # modify list in place
list.sort() # put list in order, in place (uses cmp())
x = list.pop(i) # remove list[i] and return it
cards
Yes, let's look at this one more time,
since many of you have been distracted by this and that lately.
And the practice can't hurt.
And then there's my way-over-the-top
poker.py ...
just to illustrate what can be done. That one doesn't
use anything that you haven't seen, but does use
data structures in some math-ish tricky ways.
If time remains, practice together on a different problem,
say, tic tac toe.