GuestLecture ~Tues.Prof.JulieMedero!
andthis,beforewatches– or.glasses...
useHmmmcodesomewhereinyourfirstWrit1 orSpecRel
assignment…
Challenge:
Loopoflife,XKCD'stake:
NextTues
GuestLecture ~Tues.Prof.JulieMedero!
andthis,beforewatches– or.glasses...
useHmmmcodesomewhereinyourfirstWrit1 orSpecRel
assignment…
Challenge:
Loopoflife,Prof.Medero's take:
NextTues
CS5Today
CSMidterm
Seemsmoreliketwo-thirdstermtome...
Thursday,Nov.7
In-class,written
PageofnotesisOK
• RecursioninPython
• Functioncomposition
• Circuitdesign
• Hmmmassemblycode
• LoopsinPython
infinitelynested structure…
fromfinitelynested loops
Topics
Seeonlinepractice…Homework8Loops! dueMon.11/4
Accommodations...
Homework8preview
TheMandelbrotSet
TTSSecurities
PifromPie
WhenAlgorithmsDiscriminate...
ASCIIArt
ThinkinginLoops...
(Extra)
#4
#1~lab
#3
#0
Lotsofloops!#2
Algorithms~betterangels...?
TheMandelbrotSet
TTSSecurities
PifromPie
WhenAlgorithmsDiscriminate...
ASCIIArt
Loopythinking
(Extra)
#3
#1~lab
#2
#0
TheMandelbrotSet
TTSSecurities
PifromPie
WhenAlgorithmsDiscriminate...
ASCIIArt
Loopythinking
(Extra)
#3
#1~lab
#2
#0
Or~louder angelsofhumannature...?
Homework8preview
TheMandelbrotSet
TTSSecurities
PifromPie
WhenAlgorithmsDiscriminate...
ASCIIArt
ThinkinginLoops!
(Extra)
#4
#1~lab
#3
#0
Lotsofloops!#2
PythonBat looppractice...
googlefor"PythonBat"then...
5pointsrequired,upto11pointsavailable...
Thinkinginloops
Whatarethedesign differencesbetweenthesetwotypesofPythonloops?
for x in range(42):
print(x)
x = 1
while x < 42:
print(x)
x *= 2
for while
list
Loopdesign...
Carefulhere!
Tabletent…
Isthisafor orawhileloop?
Isthisafor orawhileloop?
Loopdesign...
Thinkinginloops
definiteiteration
indefiniteiteration
Foraknown listor#ofiterations
Foranunknownnumberofiterations
for while
Lotsofloops!
Homework8preview
TheMandelbrotSet
TTSSecurities
PifromPie
WhenAlgorithmsDiscriminate...
ASCIIArt
Loopythinking
(Extra)
#4
#1~lab
#3
#0
CSS:CascadingStyleSheets(Webextra)
#2
PifromPie?Hw8Pr3
pPizzaistheuniversalconstant,afterall…
PifromPie?Hw8Pr3
pThiscouldn'tbejustacoincidence!
Estimatingπ frompie?
Hw8Pr3
Whatifwejustthrowdartsatthis
picture?
(-1,-1)
(1,1)
Pie
Box
(0,0)
Estimatingπ frompie?
(-1,-1)
(1,1)
Hints
Howbigisaside ofthesquare? itsarea?
Howbigistheradius ofthecircle?itsarea?
Howdothesehelp!?
Pi-designchallenge...
Pie
Box (1)Supposeyouthrow100dartsatthesquare(Allofthemhitthesquare)
(2)Suppose80ofthe100hitinsidethecircle.
(3)Howcouldyouestimateπfromthesethrows?
Estimatingπ frompie?
(-1,-1)
(1,1)
Pi-designchallenge...
Pie
Box
*
areaarea
=
π ~hits
hits4
π4
Hw8Pr3
pi_two(n)
pi_one(e)
n==numberofdartstothrow
e==howclosetoπweneedtoget
Whichfunctionwillusewhichkindofloop?
Loops:for or while?
Loops:for or while?
pi_two(n)
pi_one(e)
n==numberofdartstothrow
e==howclosetoπweneedtoget
πday! 3/14/15 9:26:53
Lotsofloops!
Homework8preview
TheMandelbrotSet
TTSSecurities
PifromPie
WhenAlgorithmsDiscriminate...
ASCIIArt
Notjustloops...Nested loops
(Extra)
#4
#1~lab
#3
#0
#2
Nested loopsarefamiliar,too!
for mn in range(60):for s in range(60):
tick()
list
list
Nested loopsarefamiliar,too!
Soclose!
Nested loops Lifeclock
for y in range(84):for m in range(12):
for d in range(f(m,y)):for h in range(24):
for mn in range(60):for s in range(60):
tick()
list
list
list
list
list
list
Nestedloops!
for y in range(84):for m in range(12):
for d in range(f(m,y)):for h in range(24):
for mn in range(60):for s in range(60):
tick()
Hownestedloopscanfeel...Persistence of Memory, S. Dali (MoMA)
list
list
for mn in range(60):for s in range(60):
tick()
Nestedloops' 2dstructure Onehour~3600seconds
mn == 0
mn == 42
mn == 59
s == 0 s == 59
42nd minute's59th second
list
list
hour()
Creating2dstructure~inASCII
for row in range(3):for col in range(4):
print("#")
# # # ## # # ## # # #
Wait!thisneedssomethingmore…
col
row
1 2 30
2
1
0
list
list
Creating2dstructure
for row in range(3):for col in range(4):
print("#", end='')
# # # ## # # ## # # #
Hmmm...
col
row
1 2 30
2
1
0
list
list
for row in range(3):
for col in range(4):print('#',end='')
print()
row =col =col =col =col =
row =col =col =col =col =
row =col =col =col =col =
col
row
1 2 30
2
1
0
[0,1,2]
[0,1,2,3]
Creating2dstructure
for row in range(3):for col in range(4):
if col == row:print('#',end='')
else:print(' ',end='')
print()
row =col =col =col =col =
row =col =col =col =col =
row =col =col =col =col =
col
row
1 2 30
2
1
0
Creating2dstructure
0
1
2
0123
0123
0123
Let'stakeanalien's-eyeview!
# # # # # # # # # # # # # # #
1cols
2 3 4 50
rows2
1
0
for r in range(3):for c in range(6):if c >= r: print('#',end='')
else: print(' ',end='')
print()
for r in range(3):for c in range(6):if c%2 == 1: print('#',end='')
else: print(' ',end='')
print()
for r in range(3):for c in range(6):if c%2 == r%2: print('#',end='')
else: print(' ',end='')
print()
# # # # # # # # # # # #
1cols
2 3 4 50
rows2
1
0
[0,1,2]
[0,1,2,3,4,5]
# # # # # #
# # #
1cols
2 3 4 50
rows2
1
0
# # # # # # # # #
1cols
2 3 4 50
rows2
1
0
Match!Whatcodecreatesthefourthone?
A B C
1 2
3 4
# # # # ## # # #
1cols
2 3 40
2
1
# # # # # ## # # #
1cols
2 3 40
2
1
0
*
**
0
*and**areextra!Name(s) ______________________
# # # # # # # # # # # # # # #
1cols
2 3 4 50
rows2
1
0
for r in range(3):for c in range(6):if c >= r: print('#',end='')
else: print(' ',end='')
print()
for r in range(3):for c in range(6):if c%2 == 1: print('#',end='')
else: print(' ',end='')
print()
for r in range(3):for c in range(6):if c%2 == r%2: print('#',end='')
else: print(' ',end='')
print()
# # # # # # # # # # # #
1cols
2 3 4 50
rows2
1
0
[0,1,2]
[0,1,2,3,4,5]
# # # # # #
# # #
1cols
2 3 4 50
rows2
1
0
# # # # # # # # #
1cols
2 3 4 50
rows2
1
0
Match!Whatcodecreatesthefourthone?
A B C
1 2
3 4
# # # # ## # # #
1cols
2 3 40
2
1
# # # # # ## # # #
1cols
2 3 40
2
1
0
*
**
0
*and**areextra!
if c+r
# # # # # # # # # # # # # # #
1cols
2 3 4 50
rows2
1
0
for r in range(3):for c in range(6):if c >= r: print('#',end='')
else: print(' ',end='')
print()
for r in range(3):for c in range(6):if c%2 == 1: print('#',end='')
else: print(' ',end='')
print()
for r in range(3):for c in range(6):if c%2 == r%2: print('#',end='')
else: print(' ',end='')
print()
# # # # # # # # # # # #
1cols
2 3 4 50
rows2
1
0
[0,1,2]
[0,1,2,3,4,5]
# # # # # #
# # #
1cols
2 3 4 50
rows2
1
0
# # # # # # # # #
1cols
2 3 4 50
rows2
1
0
Match!Whatcodecreatesthefourthone?
A B C
1 2
3 4
# # # # ## # # #
1cols
2 3 40
2
1
# # # # # ## # # #
1cols
2 3 40
2
1
0
*
**
0
*and**areextra!
if c+r
for d in range(f(m)):for m in range(1,13):num_bdays(m,d)
Nestedloops'2dstructure
vizwiz.blogspot.com/2012/05/how-common-is-your-birthday-find-out.html
howmanysharedbirthdaysareinCS5?
Whattrendsappearinthisbirthdaydata?
Howmightwebesuspiciousofthe
fairness ofthisdata?!
That'smytypeofalien!
Nestedloops:fromASCIIArt
...to"real" images!
Pythonandimagesfrom cs5png import *
im = PNGImage( 300, 200 )
inputs are width and height
200
0 0 300
im.plotPixel( 10, 100 )
(10,100)
(0,0)isintheusualplace!
Pythonandimages
im.plotPixel( 10, 100 )
im.plotPixel( 42, 42, (255,0,0) )
im.saveFile( ) Thesefunctionsareclearlyplotting something – ifonlyIknewwhattheywereupto...
from cs5png import *
im = PNGImage( 300, 200 )
objects arevariablesthatcancontaintheirownfunctions,oftencalledmethods
inputs are width and height
red green bluecolx rowy
200
0 0
(42,42)
(10,100)
300
ImaginingImages
from cs5png import *
def testImage():""" image demonstration """WD = 300HT = 200im = PNGImage( WD, HT )
for row in range(HT):for col in range(WD):
if col == row:
im.plotPoint( col, row )
im.saveFile()
thicker line?other diagonal?
stripes ?
thicker stripes?thatching?
Nothing'stoocomplexfor
Python!Complex#s!
1j * 1j == -1i can'tbelievethis!
(-1+0j)
Nothing'stoocomplexfor
Python!Complex#s!
1j * 1j == -1
In[]: c = -2+1j
In[]: c**2(3-4j)
(-2+1j)*(-2+1j)
i can'tbelievethis!
Nothing'stoocomplexfor
Python!Complex#s!
i can'tbelievethis!
In[]: c = -2+1j
In[]: c**2(3-4j)
Real axis
Imaginary axis
3-2
1j
2j3j
-4j
-2+1j
3- 4j
Lab8:theMandelbrotSet
Consideranupdateruleforallcomplexnumbersc
z0=0
zn+1=zn2+c
Real axis
Imaginary axis
.3
.4j
z = z**2+c ; print(z)
c=.3+.4j
c
z0
z1 z2
z3
z4
Real axis
Imaginary axis
MandelbrotDefinition
Consideranupdateruleforallcomplexnumbersc
z0=0
zn+1=zn2+c
Smallvaluesofc keepthesequencenearthe
origin,0+0j.
z5
some"stickaround"~oscillateorconverge
c=.3+.4j
z = z**2+c ; print(z)
MandelbrotDefinition
Real axis
Imaginary axis
Othervaluesofcmakethesequenceheadtoinfinity.
c
Smallvaluesofc keepthesequencenearthe
origin,0+0j.
c
Consideranupdateruleforallcomplexnumbersc
z0=0
zn+1=zn2+c
z = z**2+c ; print(z)c=3- 4j
c=.3+.4j
MandelbrotDefinition
Real axis
Imaginary axis
Othervaluesofcmakethesequenceheadtoinfinity.
BenoitB.Mandelbrot1924– 2010c
Smallvaluesofc keepthesequencenearthe
origin,0+0j.
c
Consideranupdateruleforallcomplexnumbersc
z0=0
zn+1=zn2+c
z = z**2+c ; print(z)c=3- 4j
c=.3+.4j
Lab8:theMandelbrotSet
Consideranupdateruleforallcomplexnumbersc
z0=0
zn+1=zn2+c
Lab8:theMandelbrotSet
Consideranupdateruleforallcomplexnumbersc
z0=0
zn+1=zn2+c
MandelbrotSet~pointsthatstickaround
Theshadedareaarepointsthatdonot divergeforz = z**2 + c
Higher-resolutionM.Set
Theblackpixelsarepointsthatdonot divergeforz = z**2 + c
-2 + 1j
-2 - 1j
1 + 1j
1 - 1j
connected
finite area
¥ perimeter!
Complexthingsalwaysconsistedofsimpleparts…
Chaos?
Chaos!
http://www.youtube.com/watch?v=0jGaio87u3A
notself-similarbutquasi-self-similar
Thiswasa"naturallyoccurring" objectwherezoominguncoversmoredetail,notless:
BeforetheM.Set,complexthingsweremadeofsimpleparts:
Whatarethesecolors?
Theblackpixelsarepointsthatdonot divergeforz = z**2 + c
escapevelocities!
??
AtlasoftheM.Set
IntheSeahorseValley….
HappyMandelbrotting!
www.cs.hmc.edu/~jgrasel/projects http://www.youtube.com/watch?v=0jGaio87u3A