EECS 110 Lec 8 Lists of Lists Aleksandar
- Slides: 38
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University http: //cs. northwestern. edu/~akuzma/classes/EECS 110 -s 09/
EECS 110 today Hw #3 due Sunday… Computing with language hw 3 pr 1. py Computing with images 'Krphzrun 3, Sureohp 3: Wkh Fhdvdu Flskhu' hw 3 pr 2. py hw 3 pr 3. py
Lights On! http: //www. whitman. edu/mathematics/lights_out/
Comprehending List Comprehensions def run. Generations( L ): """ run. Generations keeps running evolve. . . """ print L # display the list, L time. sleep(0. 5) # pause a bit new. L = evolve( L ) # evolve L into new. L run. Generations( new. L ) # recurse def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ set. New. Element( L, i ) for i in range(N) ] def set. New. Element( L, i, x=0 ): """ set. New. Element returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[i] + 1
Comprehending List Comprehensions def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ set. New. Element( L, i ) for i in range(N) ] def set. New. Element( L, i, x=0 ): """ set. New. Element returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[i] + 1 >>> L = [42, 43, 44, 45, 46] >>> evolve(L) L N 42 43 44 45 46 47 0 1 2 3 4 5 5 (i. e. , len(L))
Comprehending List Comprehensions def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ set. New. Element( L, i ) for i in range(N) ] def set. New. Element( L, i, x=0 ): """ set. New. Element returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[i] + 1 L [42, 43, 44, 45, 46] 0 1 2 3 4 >>> L = [42, 43, 44, 45, 46] >>> evolve(L) [ set. New. Element( L, i ) for i in range(5) ] [0, 1, 2, 3, 4] [ i , 0 , 1 , 2 , 3 ] 4 N 5 (i. e. , len(L))
Comprehending List Comprehensions def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ set. New. Element( L, i ) for i in range(N) ] def set. New. Element( L, i, x=0 ): """ set. New. Element returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[i] + 1 L [[42, 43], [44, 45]] 0 1 >>> L = [[42, 43], [44, 45]] >>> evolve(L) [[43, 44], [45, 46]] N 2 (i. e. , len(L)) [ set. New. Element( L, i ) for i in range(2) ] What is i? What is L[i]?
Comprehending List Comprehensions def evolve( L ): """ evolve takes in a list of integers, L, and returns a new list of integers considered to be the "next generation" """ N = len(L) # N now holds the size of the list L return [ set. New. Element( L, i ) for i in range(N) ] Going deeper def set. New. Element( L, i, x=0 ): """ set. New. Element returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[i] + 1 L [[42, 43], [44, 45]] 0 1 >>> L = [[42, 43], [44, 45]] >>> evolve(L) [[43, 44], [45, 46]] N 2 (i. e. , len(L)) [ set. New. Element( L, i ) for i in range(2) ] What is i? What is L[i]?
Comprehending List Comprehensions [ L[j][0] for j in range(2) ] L [[42, 43], [44, 45]] [ [L[0][i]] for i in range(2) ] [ [ L[j][i]+1 for i in range(2) ] for j in range(2) ]
Comprehending List Comprehensions L [[42, 43], [44, 45]] [ set. New. Element 2 d( L, i, j ) for i in range(2) ] for j in range(2) ] def set. New. Element 2 d( L, i, j, x=0, y=0 ): """ set. New. Element returns the NEW list's ith element input L: any list of integers input i: the index of the new element to return input x: an extra, optional input for future use """ return L[j][i] + 1
Representing Pictures
Digital representations of pictures Grid of Pixels—each Pixel has a color But how is color represented?
RGB Model for Representing Color • Most popular, but not only one • Each pixel represented in three parts (100, 0, 0) R G B
Color “levels” • Each color component or “channel” is represented with a single byte – 1 byte = 8 bits; which can represent numbers from 0 to 255 (2^8 – 1) – Each RGB value is between 0 and 255 – Examples… http: //www. colorschemer. com/online. html http: //www. drpeterjones. com/colorcalc/ (255, 255): white (150, 150): gray
Brightening a Picture def modify(pic): """ modify modifies an image to make it brighter """ pixels = get. Pixels(pic) if len(pixels) == 0: return new. Pixels = [ [set. New. Pixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] set. Pixels(pic, new. Pixels) def set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW imanges (row, col) (r, g, b) value input pixels: a 2 D list containing RGB information in the pixels in a picture input row: the row of the pixel in question input col: the column of the pixel in question """ rval= min(pixels[row][col][0]+30, 255) gval = min(pixels[row][col][1]+30, 255) bval = min(pixels[row][col][2]+30, 255) return (rval, gval, bval)
Representing the Pixels in a Picture pixels: [ [(3, 100), (3, 110)], [(3, 10, 200), (10, 110, 290)] ] Width: len(pixels[0]) Height: len(pixels) 2 x 2 pixel image
Tuples vs. Lists [ [(3, 100), (3, 110)], [(3, 10, 200), (10, 110, 290)] ] Tuples use ( ); lists use [ ] But otherwise, they are the same… (for now, almost) >>> 2 >>> (2, >>> 1 >>> 2 t = (1, 2, 3) t[1] t[1: ] 3) (x, y, z) = t x y
Brightening a Picture def modify(pic): """ modify modifies an image to make it brighter """ pixels = get. Pixels(pic) if len(pixels) == 0: return new. Pixels = [ [set. New. Pixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] set. Pixels(pic, new. Pixels) def set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW imanges (row, col) (r, g, b) value input pixels: a 2 D list containing RGB information in the pixels in a picture input row: the row of the pixel in question input col: the column of the pixel in question """ rval= min(pixels[row][col][0]+30, 255) gval = min(pixels[row][col][1]+30, 255) bval = min(pixels[row][col][2]+30, 255) return (rval, gval, bval)
"Quiz“ It's all clear to me now! Name(s): Write a function that tints the top half of the picture red (how red is up to you): def set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW image's (row, col) (r, g, b) value """ Write a function that copies the top half of an image to the bottom half. def set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW image's (row, col) (r, g, b) value """ Want more? How would you turn only the sky red?
"Quiz“ It's all clear to me now! Name(s): Write a function that tints the top half of the picture red (how red is up to you): def set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW image's (row, col) (r, g, b) value """ if row <= len(pixels)/2: rval = min(pixels[row][col][0]+75, 255) else: rval = pixels[row][col][0] return (rval, pixels[row][col][1], pixels[row][col][2]) Write a function that copies the top half of an image to the bottom half. def set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW image's (row, col) (r, g, b) value """ Want more? How would you turn only the sky red?
"Quiz“ It's all clear to me now! Name(s): Write a function that tints the top half of the picture red (how red is up to you): def set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW image's (row, col) (r, g, b) value """ if row <= len(pixels)/2: rval = min(pixels[row][col][0]+75, 255) else: rval = pixels[row][col][0] return (rval, pixels[row][col][1], pixels[row][col][2]) Write a function that copies the top half of an image to the bottom half. def set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW image's (row, col) (r, g, b) value """ if row > len(pixels)/2: return pixels[row-len(pixels)/2][col] else: return pixels[row][col] Want more? How would you turn only the sky red?
Caesar Cipher: encipher( 'gv vw dtwvg' , 0 ) returns 'gv vw dtwvg' encipher( 'gv vw dtwvg' , 1 ) returns 'hw wx euxwh' encipher( 'gv vw dtwvg' , 2 ) returns 'ix xy fvyxi' encipher( 'gv vw dtwvg' , 3 ) returns 'jy yz gwzyj' encipher( 'gv vw dtwvg' , 4 ) returns 'kz za hxazk' encipher( 'gv vw dtwvg' , 5 ) returns 'la ab iybal' … encipher( 'gv vw dtwvg' , 25 ) encipher( S , n ) returns 'fu uv csvuf' should return the string s with each alphabetic character shifted/wrapped by n places in the alphabet
How Strings are Represented and Stored? American Standard Code for Information Interchange value: bi type: str ts 10 10 10 '*' 00 ASCII is a table that tells the computer how to represent characters as bits! name: value: type: int name: ts 10 10 10 42 bi The SAME bits represent integers, if the variable has type int instead of str The types determine how to interpret the bits; the names don't matter at all… 00 8 bits = 1 byte Identical bits are stored in each variable!
ASCII American Standard Code for Information Interchange ASCII is a table that tells the computer how to represent characters as #s chr convert to char. convert to number ord
chr and ord ASCII VALUES abcdefghijklmnopqrstuvwxyz 97 99 101 103 105 107 109 111 113 115 117 119 122 ABCDEFGHIJKLMNOPQRSTUVWXYZ 65 67 69 71 73 75 77 79 81 83 85 87 90 ord( c ) Input: a string of one character, c Output: an integer, the ASCII value of c chr( n ) Input: an integer in range(255) Output: a one-char. string of that ASCII value CONVERTERS try these! [ [i, chr(i)] for i in range(128) ] [ ord(i) for i in '**** CS! ****' ]
chr and ord ASCII VALUES abcdefghijklmnopqrstuvwxyz 97 99 101 103 105 107 109 111 113 115 117 119 122 ABCDEFGHIJKLMNOPQRSTUVWXYZ 65 67 69 71 73 75 77 79 ord('a') is ? chr(66) is ? What is chr( ord('i')+13 ) ? What is chr( ord('P')+13 ) ? 81 83 85 87 90
chr and ord ASCII VALUES abcdefghijklmnopqrstuvwxyz 97 99 101 103 105 107 109 111 113 115 117 119 122 ABCDEFGHIJKLMNOPQRSTUVWXYZ 65 67 69 71 73 75 77 79 ord('a') is 97 chr(66) is ? What is chr( ord('i')+13 ) ? What is chr( ord('P')+13 ) ? 81 83 85 87 90
chr and ord ASCII VALUES abcdefghijklmnopqrstuvwxyz 97 99 101 103 105 107 109 111 113 115 117 119 122 ABCDEFGHIJKLMNOPQRSTUVWXYZ 65 67 69 71 73 75 77 79 ord('a') is 97 chr(66) is 'B' What is chr( ord('i')+13 ) ? What is chr( ord('P')+13 ) ? 81 83 85 87 90
chr and ord ASCII VALUES abcdefghijklmnopqrstuvwxyz 97 99 101 103 105 107 109 111 113 115 117 119 122 ABCDEFGHIJKLMNOPQRSTUVWXYZ 65 67 69 71 73 75 77 79 ord('a') is 97 chr(66) is 'B' What is chr( ord('i')+13 )'v' What is chr( ord('P')+13 ) ? 81 83 85 87 90
chr and ord ASCII VALUES abcdefghijklmnopqrstuvwxyz 97 99 101 103 105 107 109 111 113 115 117 119 122 ABCDEFGHIJKLMNOPQRSTUVWXYZ 65 67 69 71 73 75 77 79 81 83 85 87 90 ord('a') is 97 chr(66) is 'B' What is chr( ord('i')+13 )'v' What is chr( ord('P')+13 ) ']' How can we wrap this around?
Rot 13 def adv 13( c ): """ rotates c by 13 chars, "wrapping" as needed NON-LETTERS DO NOT CHANGE! """ if 'a' <= c <= 'z': neword = ord(c) + 13 if neword <= ord('z'): return chr(neword) # no wrapping else: return elif else: How would you rotate an entire string?
Rot 13 def adv 13( c ): """ rotates c by 13 chars, "wrapping" as needed NON-LETTERS DO NOT CHANGE! """ if 'a' <= c <= 'z': neword = ord(c) + 13 if neword <= ord('z'): return chr(neword) # no wrapping else: return chr(ord('a')+neword-ord('z')-1) elif else: How would you rotate an entire string?
Rot 13 def adv 13( c ): """ rotates c by 13 chars, "wrapping" as needed NON-LETTERS DO NOT CHANGE! """ if 'a' <= c <= 'z': neword = ord(c) + 13 if neword <= ord('z'): return chr(neword) # no wrapping else: return chr(ord('a')+neword-ord('z')-1) elif 'A' <= c <= 'Z': # same as above, only use 'A' and 'Z' else: return c How would you rotate an entire string?
Caesar Cipher: decipher >>> decipher('Bzdrzq bhogdq? H oqdedq Bzdrzq rzkzc. ') 'Caesar cipher? I prefer Caesar salad. ' >>> decipher('Hu lkbjhapvu pz doha ylthpuz hmaly dl mvynla ' 'lclyfaopun dl ohcl slhyulk. ') 'An education is what remains after we forget everything we have learned. ' >>> decipher('gv vw dtwvg') >>> decipher('Uifz xpsl ju pvu xjui b qfodjm!') But how ?
Caesar Cipher: decipher Caesar >>> decipher('gv vw dtwvg') Strategy using max: (1) consider all possible answers (2) give them each a score (3) use our techniques to get max Score for "Englishness"? up to you… all 26 possibilities gv hw ix jy kz la mb nc od pe qf rg sh ti uj vk wl xm yn zo ap bq cr ds et fu vw wx xy yz za ab bc cd de ef fg gh hi ij jk kl lm mn no op pq qr rs st tu uv dtwvg euxwh fvyxi gwzyj hxazk iybal jzcbm kadcn lbedo mcfep ndgfq oehgr pfihs qgjit rhkju silkv tjmlw uknmx vlony wmpoz xnqpa yorqb zpsrc aqtsd brute csvuf Brutus
Caesar Cipher: decipher Caesar >>> decipher('gv vw dtwvg') 'od de lbedo' Strategy using max: (1) consider all possible answers (2) give them each a score (3) use our techniques with max number-of-vowels score all 26 possibilities [0, [0, [0, [1, [1, [1, [2, [2, [3, [3, [4, [4, 'cr 'gv 'jy 'mb 'qf 'wl 'bq 'ds 'nc 'vk 'xm 'ap 'hw 'ix 'kz 'rg 'sh 'uj 'yn 'fu 'pe 'ti 'zo 'et 'la 'od rs vw yz bc fg lm qr st cd kl mn pq wx xy za gh hi jk no uv ef ij op tu ab de zpsrc'] dtwvg'] gwzyj'] jzcbm'] ndgfq'] tjmlw'] yorqb'] aqtsd'] kadcn'] silkv'] uknmx'] xnqpa'] euxwh'] fvyxi'] hxazk'] oehgr'] pfihs'] rhkju'] vlony'] csvuf'] mcfep'] qgjit'] wmpoz'] brute'] iybal'] lbedo'] Brutus won't always be correct!
Caesar Cipher: decipher Caesar >>> decipher('gv vw dtwvg') 'et tu brute' Strategy using max: (1) consider all possible answers (2) give them each a score (3) use our techniques with max letterprobability score all 26 possibilities [0. 4680, [0. 4960, [0. 5420, [0. 5567, [0. 5597, [0. 5718, [0. 5753, [0. 5833, [0. 5859, [0. 5880, [0. 5902, [0. 6110, [0. 6304, [0. 6318, [0. 6717, [0. 6735, [0. 6963, [0. 7153, [0. 7398, [0. 7442, [0. 7867, [0. 7880, [0. 7918, [0. 8213, [0. 8609, [0. 9082, 'jy 'mb 'uj 'ix 'qf 'fu 'bq 'kz 'xm 'gv 'vk 'ap 'zo 'wl 'cr 'hw 'nc 'ti 'la 'yn 'pe 'sh 'rg 'ds 'od 'et yz bc jk xy fg uv qr za mn vw kl pq op lm rs wx cd ij ab no ef hi gh st de tu gwzyj'] jzcbm'] rhkju'] fvyxi'] ndgfq'] csvuf'] yorqb'] hxazk'] uknmx'] dtwvg'] silkv'] xnqpa'] wmpoz'] tjmlw'] zpsrc'] euxwh'] kadcn'] qgjit'] iybal'] vlony'] mcfep'] pfihs'] oehgr'] aqtsd'] lbedo'] brute'] Brutus not always correct, but better!
'Weet bksa ed Xecumeha 3!'
- Vignette mutuelle 110/110
- 110 000 110 & 111 000 111
- Comp sci 110 northwestern
- Kartelj matf
- Aleksandar.krizo
- Aleksandar plamenac
- Aleksandar nikcevic
- Aleksandar prokopec
- Vjenčanje europe i azije
- Aleksandar stefanovic sorbonne
- Aleksandar rakicevic
- Aleksandar baucal
- Aleksandar kupusinac
- Primjeri franšize u hrvatskoj
- Aleksandar kuzmanovic flashback
- Aleksandar prokopec
- Abx aleksandar settings
- Objektno programiranje
- Vats simpatektomija
- Aleksandar tatalovic
- Scoreboarding computer architecture
- Lec hardver
- Lec
- August lec 250
- Art 455 lec
- Scoreboard computer architecture
- History of software development life cycle
- Tipos de lec
- Componentes del lec
- Brayton cycle
- Lec 16
- Lec
- 11th chemistry thermodynamics lec 13
- Lec arg
- Lec renal
- Sekisui slec
- 11th chemistry thermodynamics lec 10
- Lec promotion
- Lec material