WARMUP
Boggle is a dice and word game.
There is a box with a 4x4 grid and transparent top, and 16 dice with letters on each face. The Boggle box is shaken to mix the faces and positions of the dice.
The players then have 3 minutes to identify words made from the letters shown. Words are made by chains of adjacent (including diagonal) letters. Paths to make words may cross, but letters may not be re-used.
The players record their words, and at the end of the 3 minutes, they are scored. Words found by multiple players are removed from their lists. The remaining words are awarded points according to length (3-4: 1 point, 5:2, 6:3, 7:5, 8+:11).
HW Review
What did people have the most trouble figuring out? ask random
Programming
Thing big: what was this homework about? ask random
What is the form of data from experiments?
For many equations, we have a few parameters that determine system state. The dynamics of a simple spring-mass system, e.g., are determined by ultimately a single parameter:
\begin{align} \ddot{x} = -{k \over m}x \rightarrow {k \over m} = \omega^2 \end{align}
Even the Lotke-Volterra model is really just one parameter. But the physical world is quite messier than a single parameter.
If want we to determine \(\omega\) for some actual spring mass (ignoring that the actual physics are more complicated than this idealization), we need to gather many data points both to actually solve the equation, but also to account for measurement error.
Real data are quite a bit more complex than the mostly single variables we have been working with so far. They tend to have many samples, and samples tend to be a mixture of types of things. Often times there are samples of different kinds of things.
Even if you restrict your future work to pure mathematics, many of the interesting questions are about the number of configurations and proportions of of those things that have certain properties. These combinatorial problems also need many variables.
So in general, we need to be able to work with "collections" of things.
Python Built-in Collections
Python has several built-in collections, serving different purposes. They are either sequential (tuple
, list
, range
) or not (dict
, set
).
tuple
A tuple
is
__author__ = 'cap10'
def print_info(t):
try:
print("whole:",t)
print("piece:",t[0])
print("slice:",t[0:2])
print('max:',max(t),
'min:',min(t),
'sum:',sum(t))
except TypeError as err:
print(err.args[0])
atuple = (1, 2, 3, 3)
print_info(atuple)
import turtle
btuple = (1, 'two', 3.0, turtle.Screen())
print_info(btuple)
... a sequence of things, not necessarily the same type. You use a tuple when you need a sequence, but do not need (or want) to change what is in the sequence.
list
A list
is a
__author__ = 'cap10'
def print_info(t):
try:
print("whole:",t)
print("piece:",t[0])
print("slice:",t[0:2])
print('max:',max(t),
'min:',min(t),
'sum:',sum(t))
except TypeError as err:
print(err.args[0])
finally:
print()
alist = [1, 2, 3, 3]
print_info(alist)
import turtle
blist = [1, 'two', 3.0, turtle.Screen()]
print_info(blist)
## but also:
alist.append(5)
print_info(alist)
... also a sequence of things, not necessarily the same type. However, you can change the contents of a list
, either adding, deleting or replacing elements.
range
You may have already worked with range(...)
a few times. How do ranges
compare to tuple
s and list
s?
set
A set
is
__author__ = 'cap10'
def print_info(t):
try:
print("whole:",t)
print('max:',max(t),
'min:',min(t),
'sum:',sum(t))
except TypeError as err:
print(err.args[0])
finally:
print()
aset = {1, 2, 3, 3}
print_info(aset)
import turtle
bset = {1, 'two', 3.0, turtle.Screen()}
print_info(bset)
## but also:
aset.add(5)
print_info(aset)
for thing in aset:
print(thing)
... not a sequence of things, but like tuple
s and list
s, the things can be different types. However, only unique things appear in the set. We can still loop over the contents of a set
but there are no guarantees about the order.
dict
A dict
is
__author__ = 'cap10'
def print_info(t):
try:
print("whole:",t)
print('max key:',max(t.keys()),
'max value:',max(t.values()))
except TypeError as err:
print(err.args[0])
finally:
print()
adict = {'one':1, 'two':2, 'three':3, 'four':3}
print_info(adict)
import turtle
bdict = {'one':1, 'two':'two',
3:3.0, 4.0:turtle.Screen() }
print_info(bdict)
## but also:
adict.update({'one':2})
print_info(adict)
for thing in adict:
print(thing)
for key, val in adict.items():
print(key,val)
... also not a sequence of things, but as always, the things can be different types. Most notably, a dict
is a collection where you can look things up by name:
adict = { 'students':[], 'instructors':['Carl', 'Des'] }
print(adict['instructors'])
Comprehensions
Comprehensions are a way to take these basic collections and filter or transfrom them in a very compact syntax:
__author__ = 'cap10'
for y in [x*2 for x in range(-5, 50) if x > 0]:
print(y)
squares = {x:x**2 for x in range(10)}
print("square map dict comprehension:", squares)
evensquares = {x:x**2 for x in range(10) if x % 2 == 0}
print("even squares map:", evensquares)
setcomp = {x % 2 for x in range(10)}
print("set comprehension", setcomp)
HOMEWORK
Within the outline provided in script shape_objects.py
, fill in the appropriate definitions. Modify your previous code to take advantage of the new Shape
objects.