2.89k likes | 3.06k Views
IST380. Coding in circles !. Thinking loopily and cumulatively. for a while. +=. sounds natural to me!. Today : Loops have arrived… . Our Task : putting loops to good use. Recursive design can seem strange. Nothing alien here!. But we have help!.
E N D
IST380 • Coding in circles! Thinking loopilyand cumulatively for a while += sounds natural to me! Today:Loopshave arrived… • Our Task:putting loops to good use..
Recursive design can seem strange Nothing alien here!
But we have help! See the CS60 tutors (on course webpage): Loring Thomas has joined us as a tutor… For the follow up, it would be wonderful if I could work 8-10 on Tuesdays and Wednesdays. Preferably for Tuesdays, I could work in the Pitzer study room, which is C301, in East Sanborn. On Wednesday, the LAC works great. Seek out Loring!
Iterative designin Python while for variables vary a lot!
Iterative designin Python x = 42 while x > 0: print x x -= 1 while for for x in [40,41,42]: print x the initial value is often not the one we want in the end x = 41 x += 1 variables vary a lot! But we change it as we go…
This slide is four for for! for loops for x in [2,4,6,8]: print x for y in [7]*6: print y for c in'down with loops!': print c How could we get this loop to run 42 times? for i in print i There are a range of answers to this one…
It's what the fox says: Duck! for! x is assigned each value from this sequence 1 for x in [2,4,6,8]: print'x is', x print'Done!' 3 LOOP back to step 1 for EACH value in the list This is the #1 for-loop error! (what? why?) the BODY or BLOCK of the for loop runs with that x 2 Code AFTER the loop will not run until the loop is finished. 4 anatomy? empty? x unused?
That's why they're called variables Only in Python can one's newer age be older than one's older age… ! age = 41 age = age + 1 The "old" value (41) age += 1 The "new" value (42) Truth-in-powerpoint disclosure: all of this is true, in base 11
That's why they're called variables Only in Python can one's newer age be older than one's older age… ! age = 41 age = age + 1 The "old" value (41) age += 1 The "new" value (42) Python shortcuts hwToGo = 7 hwToGo = hwToGo - 1 amoebas = 100000 amoebas = amoebas * 2 u235 = 10000000000000; u235 = u235 / 2 hwToGo amoebas u235
four questions for for for x in [1,2,3,4,5,6,7]: print'x is', x avoid writing the whole list? find the sum of the list? showing partial sums? factorial function?
facwithfor deffac( N ): result = 1 for x inrange(1,N+1): result = result * x return result Hey! That's not the right answer!
Try it What does the loop say? result x result = 1 for x in [2,5,1,4]: result *= x print result 1 2
Try it Name(s): __________________________ What does the loop say? R x x i result = 1 for x in [2,5,1,4]: result *= x print result x = 0 for iin range(4): x += 10 print x L = ['golf','fore!','club','tee'] for iin range(len(L)): if i%2 == 1: print L[i] S = 'time to think this over! ' result = '' for iinrange(len(S)): if S[i-1] == ' ': result += S[i] print result i These answers seem somehow familiar!
Try it What does the loop say? x i x = 0 for iin range(4): x += 10 print x 0 0
Try it What does the loop say? i L = ['golf','fore!','club','tee'] for i in range(len(L)): if i%2 == 1: print L[i] 0 1 2 3
Try it What does the loop say? i S[i] S = 'time to think this over! ' result = '' for iinrange(len(S)): if S[i-1] == ' ': result += S[i] print result 't' 0 'i' 1 'm' 2 . . . . . .
for: two types L = [3, 15, 17, 7] x for x in L: print x element-based loops
for: two types L[0] L[1] L[2] L[3] L = [3, 15, 17, 7] 1 2 0 3 i for i in range(len(L)) print L[i] index-based loops for x in L: print x element-based loops
simpler vs. flexibler L = [3, 15, 17, 7] x def sum(L): total = 0 for x in L: total += x return total element-based loops
simpler vs. flexibler L = [3, 15, 17, 7] 1 2 0 3 i def sum(L): total = 0 fori in range(len(L)) total += _____ return total def sum(L): total = 0 for x in L: total += x return total element-based loops index-based loops
Perspective on for loops At the top of a CS5 project file … // Author: Matt Beaumont // Purpose: To get me out of CS5... // ...no, really... // Purpose: To create and maintain a list // of films and directors /* Notes: * I haven't liked for-loops since the day I met them. * They bother me for some reason. Hence, no for-loops… */ … and it is possible to avoid them entirely
Thinking in loops for while indefinite iteration definite iteration For a known number/list of iterations For an unknown number of iterations
Loops are familiar ? Careful here! Thanks: Melody S.
Extreme Looping What does this code do? print'It keeps on' whileTrue: print'going and' print'Phew! I\'m done!' I'm whiling away the time with this one!
Extreme Looping Anatomy of a while print'It keeps on' whileTrue: print'going and' print'Phew! I\'m done!' the loop keeps on running as long as this test is True while loop This won't print until the while loop finishes - In this case, it never prints! alternative tests?
Escape ?! import random escape = 0 whileescape != 42: print'Help! Let me out!' escape = random.choice([41,42,43]) print'At last!' how could we count the number of loops we run? how could we make it easier/harder to escape? random.uniform!
while power? start the answer at a reasonable value defpow( b, p ): """ finds b**p """ result = while return result create a loop that ACCUMULATES the correct answer and return it...
L will be a non-empty list of numbers. should return the number of n's in L def min( L ): defcount( n, L ): count( 99, L ) L = [50,99,43,99,45,42] An example list to consider… index?
Homework 5 preview ASCII Art #1 Pi from Pie #2 TTS Securities #3 Loopy thinking (Extras throughout)
Hw5 Pr2 Survival by loops! Executives from CBS* have stranded several teams of CSerson a sandy desert island with only Dominos' delivery #, and $7.99. The teams then compete to see who can estimate pi more accurately before being consumed by carnivorous slugs… *The Claremont Broadcasting Station * Note: $7.99 is not enough to get you the meat-lovers pizza, which would distract the carnivorous slugs… .
(1,1) Hw5 Pr2 Pie Estimating π from pie? Box (-1,-1)
(1,1) Hw5 Pr2 Pie Estimating π from pie! area π = area 4 hits 4 * π ~ Box hits (-1,-1)
Loops: fororwhile? pi_one(e) e == how close to π we need to get pi_two(n) n == number of darts to throw Which function will use which kind of loop?
Loops: fororwhile? pi_one(e) e == how close to π we need to get while pi_two(n) n == number of darts to throw for
Homework 5 preview ASCII Art #1 Pi from Pie #2 TTS Securities #3 Nested loops…
Nestedloops are familiar! So close!
Life clock Nested loops for y inrange(84): for m inrange(12): for d inrange(f(m,y)): for h inrange(24): for mninrange(60): for s inrange(60): tick()
Nested loops! Claremont time for y inrange(84): for m inrange(12): for d inrange(f(m,y)): for h inrange(24): for mninrange(60): for s inrange(60): tick()
One hour: Nested loops'2d structure rectangular vs. circular for mninrange(60): fors inrange(60): tick()
Nested loops'2d structure for m inrange(12): for d inrange(f(m)): num_bdays(m,d) vizwiz.blogspot.com/2012/05/how-common-is-your-birthday-find-out.html
Nested loops ~2d structure Nested loops'2d structure for x inrange(100): for y inrange(100): pin = str(x)+str(y)
Creating 2d structure col 2 3 1 0 # # # # # # # # # # # # 0 1 row 2 This doesn't work! for row in range(3): for col in range(4): print"#"
Creating 2d structure [0,1,2] for row inrange(3): for col inrange(4): print'#', print row = [0,1,2,3] col = col = col = col = row = col col = col = col = col = 2 3 1 0 0 row = 1 row col = col = col = col = 2
Examples… cols for row in range( 3 ): for col in range( 6 ): print ______, print 2 3 4 5 1 0 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 rows What is being printed in each of these cases? for row in range( 3 ): for col in range( 6 ): print____________, print for row in range( 3 ): for col in range( 6 ): print____________, print for row in range( 3 ): for col in range( 6 ): print____________, print 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 0 1 2 3 4 5 1 2 3 4 5 6 2 3 4 5 6 7 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1
For these loops, let side=5: columns 0 1 2 3 4 Nested loops: Rows vs. columns for row inrange(side): for col inrange(side): if row >= col: print row, else: print' ', print 0 1 2 3 4 What does this print? rows 0 1 2 3 4 for row inrange(side): for col inrange(side): if print row, else: print' ', print 0 0 0 0 0 1 1 1 1 2 2 2 3 3 4 0 1 2 3 4 What if tests print these? 0 1 2 3 4 for row inrange(side): for col inrange(side): if print row, else: print' ', print 0 1 2 3 4 0 0 0 0 0 1 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4 Extra! how about this hourglass? 0 0 0 0 0 1 1 1 2 3 3 3 4 4 4 4 4
Extra! how about this hourglass? B columns 0 1 2 3 4 for row inrange(side): for col inrange(side): if row >= col: print row, else: print' ', print 0 1 1 2 2 2 3 3 3 3 4 4 4 4 4 0 1 2 3 4 0 0 0 0 0 1 1 1 2 3 3 3 4 4 4 4 4 rows A 0 1 2 3 4 C for row inrange(side): for col inrange(side): if print row, else: print' ', print 0 0 0 0 0 1 1 1 1 2 2 2 3 3 4 0 1 2 3 4 D 0 1 2 3 4 for row inrange(side): for col inrange(side): if print row, else: print' ', print 0 1 2 3 4 0 0 0 0 0 1 1 1 1 2 2 2 3 3 3 3 4 4 4 4 4
ASCII art...? How about ASCII video ! http://www.asciimation.co.nz/