Download presentation

Presentation is loading. Please wait.

Published byHarley Gossard Modified over 3 years ago

1
**IST380 Loops got you going in circles? Nest them!**

infinitely nested structure… 3/10 2day == 2d data 3/17 spring break! 3/24 no class from finitely nested loops 3/ we meet again… nested loops == 2d data Schedule

2
**What will Python think? User input meters = raw_input('How many m? ')**

cm = meters * 100 print 'That is', cm, 'cm.' You know you've become a programmer when you ask yourself… What will Python think? I think I like these units better than light years per year!

3
**I think I like these units better than light years per year!**

User input meters = raw_input('How many m? ') cm = meters * 100 print 'That is', cm, 'cm.' What will Python think? I think I like these units better than light years per year!

4
**raw_input ALWAYS returns a string – no matter what has been typed!**

User input meters = raw_input('How many m? ') cm = meters * 100 print 'That is', cm, 'cm.' raw_input ALWAYS returns a string – no matter what has been typed! What will Python think? I think I like these units better than light years per year!

5
**Fix #1: convert to the right type**

meters_str = raw_input('How many m? ') meters = float( meters_str ) cm = meters * 100 print 'That is', cm, 'cm.' Electric: from My tutorial is at: Lasergene (a molecular biology suite of programs for aligning and comparing DNA sequences, etc.) Statview (statistical analysis) KaleidaGraph (plotting & graphing) Populus (programs that simulate ecological and evolutionary processes) '42' 42.0 4200.0 name: meters_str type: string name: meters type: float name: cm type: float

6
**input interprets its input raw_input always returns a string**

Fix #2: use input meters = input('How many m? ') cm = meters * 100 print 'That is', cm, 'cm.' input interprets its input raw_input always returns a string I always use input -- but don't "quote" me on that.

7
**hw6pr1: T. T. Securities (TTS)**

Analyzes a sequence of stock prices day day 1 day 2 day 3 day 4 day 5 day 6 day 7 L = [ 40, 80, 10, 30, 27, 52, 5, 15 ] (0) Input a new list (1) Print the current list (2) Find the average price (3) Find the standard deviation (4) Find the min and its day (5) Find the max and its day (6) Your TTS investment plan (9) Quit Enter your choice: The menu to implement:

8
**Loop Review! hw6pr1: T. T. Securities (TTS)**

Analyzes a sequence of stock prices day day 1 day 2 day 3 day 4 day 5 day 6 day 7 L = [ 40, 80, 10, 30, 27, 52, 5, 15 ] Loop Review! (0) Input a new list (1) Print the current list (2) Find the average price (3) Find the standard deviation (4) Find the min and its day (5) Find the max and its day (6) Your TTS investment plan (9) Quit Enter your choice: The menu to implement:

9
**Calls a helper function Perhaps uc the reason for this?**

A larger application def menu(): """ prints our menu of options """ print "(1) Input a new list of numbers" print "(2) I will divine the next element" print "(9) Quit" def main(): """ handles user input for our menu """ while True: menu() uc = input('Which option? ') Calls a helper function Perhaps uc the reason for this?

10
**(9) Quit! (1) Get new list (2) other… def main():**

""" handles user input for our menu """ L = [2,12,22,32] # a starting list while True: menu() # print menu uc = input('Which option? ') if uc == 9: elif uc == 1: elif uc == 2: (9) Quit! (1) Get new list (2) other…

11
**other functions, as needed**

def main(): """ handles user input for our menu """ L = [2,12,22,32] # a starting list while True: menu() # print menu uc = input('Which option? ') if uc == 9: elif uc == 1: elif uc == 2: break exits the loop (9) Quit! use input to get a new L (1) Get new list other functions, as needed (2) other…

12
**webbrowser.open_new_tab(url)**

Functions you'll write All use loops… def average( L ) Menu def stdev( L ) (0) Input a new list (1) Print the current list (2) Find the average price (3) Find the standard deviation (4) Find the min and its day (5) Find the max and its day (6) Your TTS investment plan (9) Quit Enter your choice: (L[i] - Lav)2 i len(L) def minday( L ) def maxday( L ) webbrowser.open_new_tab(url)

13
Min price Just call min ? What's the idea for finding the smallest (minimum) price? day day 1 day 2 day 3 day 4 day 5 day 6 day 7 L = [ 40, 80, 10, 30, 27, 52, 5, 15 ] m = m is the "min so far" track the value of the minimum so far as you loop over L

14
**What about the day of the minimum price?**

Min price vs. min day day day 1 day 2 day 3 day 4 day 5 day 6 day 7 L = [ 40, 80, 10, 30, 27, 52, 5, 15 ] m = 40 m = 10 m = 5 5 is returned def minprice( L ): m = L[0] for x in L: if x < m: m = x return m What about the day of the minimum price?

15
**minday def minday( L ): m = L[0] mndy = 0**

return the index of L's minimum. >>> minday( [9, 8, 5, 7, 42] ) 2 1 2 3 4 index def minday( L ): m = L[0] mndy = 0 for : if < m: m = return mndy How do we loop through the INDEX of each element? How do we ensure m keeps track of the minimum? How and where should we update mndy? index-based loop

16
**minday def minday( L ): m = L[0] mndy = 0 day = 0**

return the index of L's minimum. >>> minday( [9, 8, 5, 7, 42] ) 2 1 2 3 4 index def minday( L ): m = L[0] mndy = 0 day = 0 for x in L: if x < m: m = x mndy = day day += 1 return mndy Create a "counter" that will keep track of the current day. IF we find a smaller min, save which day it was in mndy Regardless, we add 1 to day so that we're keeping track! element-based loop

17
**personal motivation for TT securities…**

LightPath, Summer 1999 ~ wedding gift…

18
LightPath, Summer 1999

19
LightPath, Spring 2000

20
**Why it's called a brokerage**

LightPath, Fall 2013

21
**Investment analysis for the 21st century … and beyond!**

T. T. Securities (0) Input a new list (1) Print the current list (2) Find the average price (3) Find the standard deviation (4) Find the min and its day (5) Find the max and its day (6) Your TTS investment plan (9) Quit Enter your choice: Software side … Hardware side… Investment analysis for the 21st century … and beyond!

22
**Investment analysis for the 21st century … and beyond!**

T. T. Securities (0) Input a new list (1) Print the current list (2) Find the average price (3) Find the standard deviation (4) Find the min and its day (5) Find the max and its day (6) Your TTS investment plan (9) Quit Enter your choice: Software side … Hardware side… Investment analysis for the 21st century … and beyond!

23
**Investment analysis for the 21st century … and beyond!**

T. T. Securities (0) Input a new list (1) Print the current list (2) Find the average price (3) Find the standard deviation (4) Find the min and its day (5) Find the max and its day (6) Your TTS investment plan (9) Quit Enter your choice: Back to the future Software side … Hardware side… Investment analysis for the 21st century … and beyond!

24
**What is the best TTS investment strategy here?**

The TTS advantage! Your stock's prices: L = [ 40, 80, 10, 30, 27, 52, 5, 15 ] What is the best TTS investment strategy here? Day Price To be realistic, however (for our VC backers and the SEC), you may only sell after you buy.

25
**In CS, rules rule! L = [5,42,'hi'] s = 'hi'**

Reference Pointer id L = [5,42,'hi'] s = 'hi' 'hi' 5 42 'hi' L s L[0] L[1] L[2] (1a) Numbers and strings are handled "by value" (1b) Lists are handled "by reference"

26
**The contents of the variable's "box" in memory are copied.**

In CS, rules rule! Reference Pointer id L = [5,42,'hi'] s = 'hi' 'hi' 5 42 'hi' L s L[0] L[1] L[2] (1a) Numbers and strings are handled "by value" (1b) Lists are handled "by reference" The contents of the variable's "box" in memory are copied. (2) Inputs pass to functions "by value" 7 copy of fav 7 fav f( fav ) fav

27
**Python's two methods for handling data**

Reference vs. Value Python's two methods for handling data L = [5,42,'hi'] s = 'hi' Reference Pointer id Whee! 'hi' 5 42 'hi' s L L[0] L[1] L[2] Lists are handled by reference (the variables really hold a memory address) Primitive data and strings are handled by value: imagine they hold the data

28
**Python functions: pass by value**

def conform(fav) fav = 42 return fav fav def main() print " Welcome! " fav = 7 conform(fav) print " My favorite # is", fav 7 fav But what if the underlined part were absent… ?

29
**Python functions: pass by value**

def conform(fav) fav = 42 return fav 42 7 fav copy of fav "pass by value" means the contents of fav are copied to fav def main() print " Welcome! " fav = 7 conform(fav) print " My favorite # is", fav 7 fav But what if the underlined part were absent… ?

30
**Python functions: pass by value**

def conform(fav) fav[0] = 42 fav[1] = 42 return fav fav 7 11 def main() print " Welcome! " fav = [7,11] conform(fav) print " My favorite #s: ", fav fav[0] fav[1] fav But what if the underlined part were absent… ?

31
**Python functions: pass by value**

def conform(fav) fav[0] = 42 fav[1] = 42 return fav fav 7 11 def main() print " Welcome! " fav = [7,11] conform(fav) print " My favorite #s: ", fav fav[0] fav[1] fav But what if the underlined part were absent… ?

32
Lists are Mutable You can change the contents of lists in functions that take those lists as input. - Lists are MUTABLE objects Those changes will be visible everywhere. One rule rules them all: pass-by-value Numbers, strings, etc. are IMMUTABLE – they can't be changed, only reassigned.

33
**Differing approaches to rules …**

Engineers Physicists different worldviews… Mathematicians CS?

34
**Engineers believe equations approximate reality;**

"the rules" Engineers believe equations approximate reality; Safety margins! Grand Canyon Skywalk

35
**Engineers believe equations approximate reality;**

Physicists believe reality approximates equations… Image forensics' verdict: Fake! Not a parabola, so not a real shot!

36
**Banach-Tarski paradox**

Engineers believe equations approximate reality; Physicists believe reality approximates equations… Mathematicians don't care either way! A solid sphere can be split into 5 parts and rigidly reassembled into two spheres the same size as the original Banach-Tarski paradox Banach-Tarski XKCD the parts are "mist"

37
**Engineers believe equations approximate reality;**

Physicists believe reality approximates equations… Mathematicians don't care either way! why settle for gears, when you could have fractal gears? In CS? Don't like reality? Build a new one!

38
**arithmetic operations**

Engineers believe equations approximate reality; Physicists believe reality approximates equations… Mathematics reasons about structural rules… … and CS reasons about procedural ones. for lists variables while Axioms arithmetic operations Definitions if/else proofs programs Insights, tools, truths Insights, tools, algorithms math worldview CS worldview

39
**One rule rules them all: everything's a list!**

2D data! One rule rules them all: everything's a list!

40
**One rule rules them all: everything's a list!**

Lists ~ 2D data A = [ 42, 75, 70 ] 42 75 70 list int int int A One rule rules them all: everything's a list! 1D lists are familiar – but lists can hold ANY kind of data – including lists!

41
**Lists ~ 2D data A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] 1 2 3 4 ? 5 6**

Where's 3? len(A[0]) len(A) Replace 10 with 42.

42
**Rectangular 2D data A = [ [1,2,3,4], [5,6,7,8], [9,0,1,2] ] 1 2 3 4 5**

list list A[0] A A[0][0] 5 6 7 8 list A[1] 9 1 2 list A[2] A[2][3] To try… What is A[1][2]? What does each component of A[1][2] mean ? How many rows does A have, in general ? How many columns does A have, in general ?

43
**What are the resulting values in A?**

Try it… A = [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] Starting with the 2d array A shown here, what are the values in A after running this code? A Before def mystery(A): """ what happens to A ? """ NROWS = len(A) NCOLS = len(A[0]) for row in range( 0,NROWS ): for col in range( 0,NCOLS ): if row == col: A[row][col] = 42 else: A[row][col] += 1 row 0 row 1 row 2 col 0 col 1 col 2 col 3 A After What are the resulting values in A?

44
**hw6pr2: John Conway's Game of Life**

red cells are "alive" John Conway Are there a few simple rules that could give rise to intricate, complex phenomena… They need to be local Chemistry ~ life! How? white cells are empty

45
**hw6pr2: John Conway's Game of Life**

Grid World Evolutionary rules red cells are "alive" Everything depends on a cell's eight neighbors Exactly 3 neighbors give birth to a new, live cell. Exactly 2 or 3 neighbors keep an existing cell alive. Only 3 rules Any other # of neighbors and the central cell dies… white cells are empty

46
**Lab Problem: Conway's Game of Life**

Grid World Evolutionary rules red cells are "alive" Everything depends on a cell's eight neighbors Exactly 3 neighbors give birth to a new, live cell. Exactly 2 or 3 neighbors keep an existing cell alive. Any other # of neighbors and the central cell dies… white cells are empty

47
**Lab Problem: Conway's Game of Life**

Grid World Evolutionary rules red cells are "alive" Everything depends on a cell's eight neighbors Exactly 3 neighbors give birth to a new, live cell. Exactly 2 or 3 neighbors keep an existing cell alive. Any other # of neighbors and the central cell dies… white cells are empty

48
**Lab Problem: Conway's Game of Life**

Grid World Evolutionary rules red cells are alive Everything depends on a cell's eight neighbors Exactly 3 neighbors give birth to a new, live cell. What's next? Exactly 2 or 3 neighbors keep an existing cell alive. Any other # of neighbors and the central cell dies… white cells are empty

49
**next_life_generation( A )**

Lab Problem: Creating life next_life_generation( A ) For each cell… 3 live neighbors – life! 2 live neighbors – same 0, 1, 4, 5, 6, 7, or 8 live neighbors – death computed all at once, not cell-by-cell: the ? at left does NOT come to life! ?

50
**next_life_generation( A )**

Lab Problem: Creating life next_life_generation( A ) old generation is the input, A returns the next generation 1 2 3 4 5 1 2 3 4 5 1 1 2 2 3 3 4 4 5 5

51
**Lab Problem: Creating life**

Stable configurations: "rocks" Periodic "plants" period 2 period 3 Self-propagating "animals" glider

52
**Lab Problem: Creating life**

Many life configurations expand forever… "Gosper glider gun" "glider" What is the largest amount of the life universe that can be filled with cells? How sophisticated can Life-structures get?

53
**extra! the Mandelbrot Set**

z0 = 0 zn+1 = zn2 + c Consider an update rule for all complex numbers c Wait! What are complex numbers…?

54
**Small values of c keep the sequence near the origin, 0+0j.**

Complex numbers… z0 = 0 zn+1 = zn2 + c Consider an update rule for all complex numbers c Small values of c keep the sequence near the origin, 0+0j. z3 c z1 z2 z4 z0 Real axis Imaginary axis

55
**Complex numbers are simply ordinary 2d points**

x ~ "real" part Complex numbers are simply ordinary 2d points y ~ "imaginary" part z = x + yi ~ complex # z3 is 4 + 2i z1 is ~ z3 z1 z2 z4 z0 Real axis zlost zlost is ~ Imaginary axis

56
**Mandelbrot Definition**

z0 = 0 zn+1 = zn2 + c Consider an update rule for all complex numbers c Which c's stick around? Small values of c keep the sequence near the origin, 0+0j. Benoit B. Mandelbrot 1924 – 2010 c Real axis c Large values of c make the sequence head to infinity. Imaginary axis

57
**Python's complex #s >>> c = 3 + 4j >>> c.real 3.0**

>>> c.imag 4.0 >>> abs(c) 5.0 Nothing's too complex for Python!

58
**Mandelbrot Set ~ low-res version**

The shaded area are points that do not diverge for z = z**2 + c

59
**Higher-resolution M. Set**

-2 + 1j 1 + 1j connected finite area perimeter! -2 - 1j 1 - 1j The black pixels are points that do not diverge for z = z**2 + c

60
**Chaos! Zooming in reveals more and more detail, not less:**

not 100% self-similar but quasi-self-similar

61
**The black pixels are points that do not diverge for z = z**2 + c**

What are these colors?

62
**The black pixels are points that do not diverge for z = z**2 + c**

escape velocities What are these colors?

63
Happy Mandelbrotting!

64
**Good luck with Hwks #5 and 6!**

We meet again on March 31!

Similar presentations

OK

Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.

Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on ms word 2007 tutorial Ppt on db2 mainframes cics Ppt on review of literature outline Ppt on difference between product and service marketing Ppt on personal swot analysis Ppt on nature and human soul Ppt on porter's five forces model analysis Ppt on different types of dance forms computer Ppt on eye of tiger Ppt on solar power energy