what's ahead? conway gives a whole new outlook on "loopy" ! john conway 10/18 no...
TRANSCRIPT
What's ahead?
Conway gives a whole new outlook on "loopy"
!
John Conway
10/18 no class - Fall break10/19 no new hw due
10/11 Dynamic & binary data10/12 ASCII art / C. cipher due
11/1 Objects and classes 211/2 Regular HW due
10/25 Dictionaries and objects 110/29 Images or life due
11/8 Final project ideas, p. 1
I'm a head!
ASCII diamond?
10-11-10 ?
Binary, briefly
8 bits = 1 byte = 1 box
The SAME bits can represent two different pieces of data,
depending on the type
type: str type: int00
1010
10
0010
1010
bits
bits
name: name:
value: value:
'*' 42
The same bits are in each container.But why these?
Base 10 What is 42 ?
424 tens + 2 ones
1231 hundred + 2 tens + 3 ones
ON
Es
colu
mn
TEN
s co
lum
nO
NEs
colu
mn
TEN
s co
lum
n
HU
ND
RED
s co
lum
n
--------
Base 2 Base 10
424 tens + 2 ones
101010
1231 hundred + 2 tens + 3 ones
ON
Es c
olu
mn
TEN
s co
lum
nO
NEs
colu
mn
TEN
s co
lum
n
HUN
DRED
s co
lum
n
each column represents
another power of the base
ON
Es c
olu
mn
TWO
s co
lum
n
FOURs
colu
mn
EIGH
Ts c
olu
mn
SIX
TEEN
s co
l.
THIR
TYTW
Os
col.
ON
Es c
olu
mn
TWO
s co
lum
n
FOURs
colu
mn
EIGH
Ts c
olu
mn
SIX
TEEN
s co
l.
THIR
TYTW
Os
col.
SIX
TYFO
URs
col
128s
colu
mn
Write 123 in binary…
10-11-10 ?
Conversions
from binary string to decimal number:
binstr = '101010'
N = int( binstr, 2 )
N is now 42.
Conversions
from binary string to decimal number:
from decimal number to binary string:
binstr = '101010'
N = int( binstr, 2 )
N is now 42.
def n2b( N ): if N<1: return '' return n2b(N/2) + str(N%2)
Use this helper function:
n2b( 42 )
'101010'
input any integer...
... and the binary string is returned
Hw5: binary, applied
Binary Image Encoding as raw bitsjust one big string of 64 characters
10101010 01010101 10101010 01010101 10101010 01010101 10101010 01010101
"1010101001010101101010100101010110101010010101011010101001010101"
Hw5: binary image compression
Binary Image
00000000 00000000 11111111 11111111 00000000 00000000 00000000 00001111
Encoding as raw bitsjust one big string of 64 characters
"0000000000000000111111111111111100000000000000000000000000001111"
If our images tend to have long streaks of unchanging data, how might we represent it more efficiently… (but still in binary)?
Hw5: binary image compression
Encoding as raw bits
One possible idea
0100001100000111001100
0 is the first digit
There are 16 of them.
1 is the next digit
Again, there are 16 of them.
just one big string of 64 characters
problems??
00000000 00000000 11111111 11111111 00000000 00000000 00000000 00001111
0 is the next digit
There are 28
1 is the final digit
There are 4
fixed-width image compression
a FIXED-width encoding is needed
00010000100100000001110010000100
0 is the first digit
There are 16 of them.
1 is the next digit
Again, there are 16 of them.
7 bits: # of repeats
1st bit holds 1 pixel value
7 bits holds the # of those values in a row
and so on…
7 bits: # of repeats
8-bit data block 8-bit data block 8-bit data block 8-bit data block
original data original image
00000000 00000000 11111111 11111111 00000000 00000000 00000000 00001111
28 zeros 4 ones8 bits for each "block"
• If 7 bits holds the # of consecutive repetitions of data, what is the largest number of 1s or 0s that one block can represent?
• For hw5pr2, the standard images will be small (< 127 pixels)
B bits?00010000
7 bits: # of repeats
8-bit data block
7 bits?
the pixel
hw5 pr2
def compress( I ): """ returns the RLE of the input binary image, I """
def uncompress( CI ): """ returns the binary image I from the run-length- encoded, "compressed" input, CI """
What function(s) might help here?
from cs5png import *binaryIm( I, ncols, nrows )
can download and run the image-file interface (not at all necessary)
for fun…
A start...
Write a function frontNum(s). It should return the # of times the first element of the input s appears consecutively AT THE FRONT of that string:
>>> frontNum('1111010')4
>>> frontNum('00111000010')2
Examples:
def frontNum(s):
What are the BEST and the WORST compression results you can get for an 8x8 input image (64 bits)?
How can you change fixed-length compression so that all compressed images are smaller than their originals ? That is,
how can we make compression always "work" ? EXTRA
Write a function frontNum(s). It should return the # of times the first element of the input s appears consecutively AT THE FRONT of that string:
def frontNum(s):
>>> frontNum('1111010')4
>>> frontNum('00111000010')2
Examples:
What are the BEST and the WORST compression results you can get for an 8x8 image input (64 bits)?
shortest compressed
representation
longest compressed representation
How can you change this compression algorithm so that all compressed images are smaller than their originals ? That is, how can we make compression
always work ?
EXTRA
Original Image T = 78/255 T = 120/255 Adaptive T
Binary images?
T = 120/255
Adaptive Threshold
Plot of total intensity across each row of pixels
What's in the image?
Why is it important to have binarized first?
What do these peaks
mean?
Which way is up?
This landscape image is determined to
contain a portrait document.
Intensity profiles
Which way is up?
Right-side up?
IS 313 Initial Project
Some preliminary thinking about project possibilities...
VPython
PyGame
Python for Android
Web Application ?• File upload ?
Python + SQL
• Image converter ?
• Library for 3d graphics
Connecting to external projects:
• Library for 2d game building
Python + ArcGIS
• Survey with stored results ?
• Other external devices • Databases? • Geog. displays
IS 313 Initial Project
This project has three deadlines:
• 10/25 Plan and initial reading/design
Using an external library? Make sure it's installed and an example works...
Start with the course's final project
• 11/8 Update with progress
• 11/15 Final version of this trial project
Make sure the plans and goals are reasonable, but potentially looking ahead...
There should be at least one piece of added, working functionality
Perhaps a revised plan for what to accomplish by 11/15
May choose to extend the initial project or completely change direction.
Potentially, a revised plan for what to accomplish by 11/15
Computing's Initial Project?
The Mark 1
Grace Hopper, Howard Aiken at Harvard
ran at 0.00001 MHz
5 tons, 530 miles of wiring765,299 distinct parts!
relay-based computer
Addition: 0.6 secondsMultiplication: 5.7 secondsDivision: 15.3 seconds
http://www-03.ibm.com/ibm/history/exhibits/markI/markI_reference.html
print: Making programs talk to you!
Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in
finding mistakes in my own programs.- Maurice Wilkes
Programming: the art of debugging an empty file.
- The Jargon Filehttp://www.tuxedo.org/~esr/jargon/
The first bug
Grace Hopper
“In the days they used oxen for heavy pulling, when one ox couldn't budge a log, they didn't try to grow a larger ox. We shouldn't be trying for bigger and better computers, but for
better systems of computers.”
from the UNIVAC 1
hw5pr3
Image processing...
import cs5png
Images are represented by Python lists...
IM = cs5png.getRGB( 'olin.png' )
newIM = invert( IM )
cs5png.saveRGB( IM )
hw5pr3
Creating a 4-pixel image from scratch....
PX = [ [ [255,0,0], [0,255,0] ]
cs5png.saveRGB( PX )
top row
1st column 2nd column
red, green, and blue components
Images... are just lists
How many dimensions do those lists have?
PX = [ [ [255,0,0], [0,255,0] ] [ [0,0,255], [255,255,0] ] ]
dimensions ~ complexity
Reference vs. Value
Changeable types: Unchangeable types:
liststring
int
float
bool
LL[0] L[1] L[2]
Reference,Pointer,
id
L = [7,11,'hi']
x7 11 'hi'
42
x = 42
“Pass By Value”
def main() """ calls conform """ print " Welcome to Conformity, Inc. "
fav = 7 conform(fav)
print " My favorite number is", fav
def conform(fav) """ sets input to 42 """ fav = 42 return fav
7
fav
fav
7
“Pass By Value”
def main() """ calls conform """ print " Welcome to Conformity, Inc. "
fav = 7 conform(fav)
print " My favorite number is", fav
def conform(fav) """ sets input to 42 """ fav = 42 return fav
7
fav
fav
PASSBY
VALUE
“Pass by value” means that data is copied when sent to a method
42
Passing lists by value…
def main() """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print " My favorite numbers are", fav
def conform2(fav) """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42
What gets passed by value here?
favL[0] L[1]
7 11
fav
Passing lists by value…
def main() """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print " My favorite numbers are", fav
def conform2(fav) """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42
favL[0] L[1]
7 11
fav
can change data elsewhere!
The reference is copied!
The conclusion
You can change the contents of lists in functions that take those lists as input.
Those changes will be visible everywhere.
(actually, lists or any mutable objects)
(immutable objects are safe, however)
Views of the world
Engineers think their equations approximate reality.
Views of the world
Engineers think their equations approximate reality. Physicists think reality approximates their equations.
Views of the world
Engineers think their equations approximate reality. Physicists think reality approximates their equations.
Mathematicians don't care.
Views of the world
Axioms
Definitions
Creating structure from a few simple facts...
Creating structure from a few simple actions ...
if/else
while
for
arithmetic operations
variablesarrays
Proof Algorithm
Engineers think their equations approximate reality. Physicists think reality approximates their equations. Mathematicians don't care.
computer science
Lists’ flexibility
Lists can hold ANY type of data
A = [ 42., 75., 70. ] 42.0 75.0 70.0float float floatlist
A
they don’t have to be horizontal
lists!
42.0
75.0
70.0
double
double
double
listAthey don’t have
to be horizontal lists!
Lists’ flexibility
Lists can hold ANY type of data
A = [ 42., 75., 70. ] 42.0 75.0 70.0float float floatlist
A
Lsits’ flexibility
Lists can hold ANY type of data
42.0 75.0 70.0double double doublelist
A
42 7 -11int int intlist
A
“go” “red” “sox!”
String String StringlistA
2d lists or arrays
Lists can hold ANY type of data -- including lists !
listA
A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]
listA
2d arrays
list
list
list
A[0]
A[1]
A[2]
Lists can hold ANY type of data -- including lists !
A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]
listA
Jagged arrays
list
list
list
A[0]
A[1]
A[2]
Lists can hold ANY type of data -- including lists !
A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]
Rows within 2d arrays need not be the same length…
listA
list
list
list
A[0]
A[1]
A[2]
Lists can hold ANY type of data -- including lists !
A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]
Rows within 2d arrays need not be the same length…
We will not use jagged arraysat least in hw 10
Rectangular arrays
listA
list
list
list
A[0]
A[1]
A[2]
How many rows does A have, in general ?
How many columns does A have, in general ?
What does each piece of the syntax A[1][2] mean ?
A[1][2] = 42
A[2][3]
A[0][0]
Creating 1d and 2d lists...
def createRow( width ): """ does just that """ R = [] # start with nothing for col in range( width ): R = R + [0] return R
Creating 1d and 2d lists...
def createRow( width ): """ does just that """ R = [] # start with nothing for col in range( width ): R = R + [0] return R
def createBoard( height, width ): """ creates a 2d array! """ Bd = [] for row in range( ):
return Bd
Problem 4 -- “Life”
Evolutionary rules
Grid World
• Everything depends on a cell’s eight neighbors
red cells are alive
white cells are empty
• Exactly 3 neighbors give birthto a new, live cell!
• Exactly 2 or 3 neighbors keep anexisting cell alive
• Any other number of neighbors killthe central cell (or keep it dead)
John Conway
Problem 4 -- Life
Evolutionary rules
Grid World
• Everything depends on a cell’s eight neighbors
red cells are alive
white cells are empty
• Exactly 3 neighbors give birthto a new, live cell!
• Exactly 2 or 3 neighbors keep anexisting cell alive
• Any other number of neighbors killthe central cell (or keep it dead)
Problem 4 -- Life
Evolutionary rules
Grid World
• Everything depends on a cell’s eight neighbors
red cells are alive
white cells are empty
• Exactly 3 neighbors give birthto a new, live cell!
• Exactly 2 or 3 neighbors keep anexisting cell alive
• Any other number of neighbors killthe central cell (or keep it dead)
Problem 4 -- Life
Evolutionary rules
Grid World
• Everything depends on a cell’s eight neighbors
red cells are alive
white cells are empty
• Exactly 3 neighbors give birthto a new, live cell!
• Exactly 2 or 3 neighbors keep anexisting cell alive
• Any other number of neighbors killthe central cell (or keep it dead)
life out there...
Keep going!
Problem 4 -- Creating Life
0 1 2 3 4 50 1 2 3 4 5
0
1
2
3
4
5
0
1
2
3
4
5
newB = make_next_generation( oldB )
old generation or "board" new generation or "board"
Problem 4 -- Creating Life
0 1 2 3 4 50 1 2 3 4 5
0
1
2
3
4
5
0
1
2
3
4
5
newB = make_next_generation( oldB )
old generation or "board" new generation or "board"
Problem 4 -- Details
For each generation… • 0 represents an empty cell
• 1 represents a living cell
• outermost edge should always be left empty (even if there are 3 neighbors)
• compute all cells based on their previous neighbors before updating any of them
http://www.math.com/students/wonders/life/life.html
life out there...
Problem 2 -- to and beyond!
• Are there stable life configurations?
• Are there oscillating life configurations?
• Are there self-propagating life configurations?
"rocks"
"plants"
"animals"
period 3
period 2
Problem 2 -- to and beyond!
• Are there life configurations that expand forever?
• What is the largest amount of the life universe that can be filled with cells?
• How sophisticated can the structures in the life universe be?
Google for GOLLY, Game of Life
• Are all feasible configurations reachable?
Lab, Life!, and Homework…
import sys
for i in range(4): for j in range(8): sys.stdout.write('#') print
software object representing the screen's standard output
Without extra spaces…
Output
################################
method (function) that prints only its input
The not-so-subtle art of singling out the best (and worst) of anything…
Computing with language
Computing to the max You know this would make me hungry… if it weren't 7 pm!
Battle-tested ciphers - and how to break them !
Consider literature a bunch of strings ?
'm&ms'
[ 0, 42 ]
a comparison comparison
[ 4, 2 ]
True 'True'
'coffee'
> or <
Drawn Inward
Fall leaves when leaves fall.
Seasons by Mike Maguire
Beyond binary … to bits themselves
CS 101 Today
King of ANDs Queen of ORs
Jack of NOTs
Too many bits? Compress!
Olin during the day?
alien 2 diner 1
faces 2 diner 1
Hw #4 due Mon. 10/4
HW Notes
Lab binary & decimal
pr2 all your bases are belong to us!
pr3 image compression
Morning
Aft
mount 1 ghost 1
seven 0 ghost 0
extra image processing...
class's guesses
class's guesses