EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University

Slides:



Advertisements
Similar presentations
Data Representation. Units & Prefixes Review kilo, mega, and giga are different in binary! bit (b) – binary digit Byte (B) – 8 binary digits KiloByte.
Advertisements

DOS and BIOS Interrupts DOS and BIOS interrupts are used to perform some very useful functions, such as displaying data to the monitor, reading data from.
CS252: Systems Programming Ninghui Li Program Interview Questions.
Computer Science 111 Fundamentals of Programming I More Digital Image Processing.
The Binary Numbering Systems
 Caesar used to encrypt his messages using a very simple algorithm, which could be easily decrypted if you know the key.  He would take each letter.
HW 3: Problems 2&3. HW 3 Prob 2:Encipher encipher( S, n ) takes as input a string S and a non-negative integer n between 0 and 25. This function returns.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
First Bytes - LabVIEW. Today’s Session Introduction to LabVIEW Colors and computers Lab to create a color picker Lab to manipulate an image Visual ProgrammingImage.
Data Representation (in computer system) Computer Fundamental CIM2460 Bavy LI.
Intersecting & Parallel Lines, Line Segments, and Rays! By: Ms. Castela.
IS 313 Today What's ahead? I see I'm not the only one loopy around here… 10/7 Lec 5 ~ today, now! HMC standings!? 10/8 homework 3 due 10/14 Lec 6 ~ "dictionaries"
Cryptography Programming Lab
CS 102 Computers In Context (Multimedia)‏ 01 / 28 / 2009 Instructor: Michael Eckmann.
Forth A stack language.
Spring  Evolving Lists & Lights On!  Caesar Cipher  Looks Good!
Chapter 1 Data Storage(2) Yonsei University 1 st Semester, 2014 Sanghyun Park.
Image Processing & Perception Sec 9-11 Web Design.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 5: List Comprehensions Aleksandar Kuzmanovic Northwestern University
The building blocks of functional computing data, sequences conditionals recursion CS 121 today List Comprehensions map and applications.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
… Caesar Cipher: encipher encipher( 'gv vw dtwvg', 0 ) encipher( 'gv vw dtwvg', 1 ) encipher( 'gv vw dtwvg', 2 ) encipher( 'gv vw dtwvg', 3 ) encipher(
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University
Representing Characters in a computer Pressing a key on the computer a code is generated that the computer can convert into a symbol for displaying or.
Comprehending List Comprehensions
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
CS 111 – Sept. 3 More data representation Review hex notation Text –ASCII and Unicode Sound and images Commitment: –For Wednesday: Please read pp
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University
Loops ! We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range ?
Data Representation. What is data? Data is information that has been translated into a form that is more convenient to process As information take different.
EECS 110: Lec 10: Definite Loops and User Input Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 7: Program Planning Aleksandar Kuzmanovic Northwestern University
CHARM Lecture 1 Outline of the Problem. The Maltese Alphabet A aB bĊ ċD dE eF fĠ ġG gGħ għH h abeċedeeefġegeajnakka Ħ ħI iIe ieJ jK kL lM mN nO oP p ħeiiejekeelleemmeenneope.
EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
Information in Computers. Remember Computers Execute algorithms Need to be told what to do And to whom to do it.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
 Computers are 2-state devices › Pulse – No pulse › On – Off  Represented by › 1 – 0  BINARY.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
EECS 110: Lec 7: Program Planning Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 8: Lists of Lists
EECS 110: Lec 10: Definite Loops and User Input
EECS 110: Lec 12: Mutable Data
Spring 2010 EECS 110: Homework III.
EECS 110: Lec 9: Review for the Midterm Exam
EECS 110: Lec 5: List Comprehensions
EECS 110: Lec 5: List Comprehensions
EECS 110: Lec 7: Program Planning
Caesar Cipher: encipher
Homework 3 Problem 0 If you liked the article on the DNA computer, check the following: Biocomputer and Memory Built Inside Living Bacteria IEEE Spectrum,
EECS 110: Lec 10: Definite Loops and User Input
Numbers and their bases
Fundamentals of Data Representation
CSC 221: Introduction to Programming Fall 2018
EECS 110: Lec 4: Functions and Recursion
Spring 2015.
3-5 Slopes of Lines Warm Up Lesson Presentation Lesson Quiz
functions: argument, return value
Dynamic Programming-- Longest Common Subsequence
Symmetry and Congruence
CS2911 Week 2, Class 3 Today Return Lab 2 and Quiz 1
EECS 110: Lec 12: Mutable Data
Random numbers What does it mean for a number to be random?
Presentation transcript:

EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University

Hw #3 due Sunday… hw3pr1.py EECS 110 today 'Krphzrun 3, Sureohp 3: Wkh Fhdvdu Flskhu' hw3pr2.py Computing with language Computing with images hw3pr3.py

Sorting a List What data do we need to keep track of? What is the input/output of the function?

Sorting a List If we had an easy way to find the maximum of the list, how could we use this to sort the list?

Taking only one… def removeOne( e, L ): """ this function removes one element e from the top level of the list L """ if len(L) == 0: return L # L is empty elif e == L[0]: return L[1:] # remove this one else: return L[0:1] + removeOne(e,L[1:]) # keep the non-e element and then keep going removeOne(42, [5,7,42,8,42]) [5,7,8,42] removeOne('p', 'computer programming') 'comuter programming'

sort(L) def sort( L ): """ a list of elements in L, sorted from hi to low """ if len(L) < 1: return L else:

sort(L) def sort( L ): """ a list of elements in L, sorted from hi to low """ if len(L) < 1: return L else: return [max(L)] + sort(removeOne( max(L), L ))

sort(L, maxFun) def sort( L, maxFun ): """ a list of elements in L, sorted using maxFun """ if len(L) < 1: return L else: return

sort(L, maxFun) def sort( L, maxFun ): """ a list of elements in L, sorted using maxFun """ if len(L) < 1: return L else: return [maxFun(L)] + sort(removeOne( maxFun(L), L )) Will this work?

sort(L, maxFun) def sort( L, maxFun ): """ a list of elements in L, sorted using maxFun """ if len(L) < 1: return L else: return [maxFun(L)] + sort(removeOne( maxFun(L), L ), maxFun)

sort(L, maxFun) def sort( L, maxFun ): """ a list of elements in L, sorted using maxFun """ if len(L) < 1: return L else: return [maxFun(L)] + sort(removeOne( maxFun(L), L ), maxFun) What happens if you call >>>sort( L, min )

Lights On!

Comprehending List Comprehensions def runGenerations( L ): """ runGenerations keeps running evolve... """ print L # display the list, L time.sleep(0.5) # pause a bit newL = evolve( L ) # evolve L into newL runGenerations( newL ) # 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 [ setNewElement( L, i ) for i in range(N) ] def setNewElement( L, i, x=0 ): """ setNewElement 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 [ setNewElement( L, i ) for i in range(N) ] def setNewElement( L, i, x=0 ): """ setNewElement 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 >>> L = [42, 43, 44, 45, 46] >>> evolve(L) N5 (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 [ setNewElement( L, i ) for i in range(N) ] def setNewElement( L, i, x=0 ): """ setNewElement 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 >>> L = [42, 43, 44, 45, 46] >>> evolve(L) N5 (i.e., len(L)) [ setNewElement( L, i ) for i in range(5) ] [0, 1, 2, 3, 4] [42, 43, 44, 45, 46] [,,,, ] 01234i

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 [ setNewElement( L, i ) for i in range(N) ] def setNewElement( L, i, x=0 ): """ setNewElement 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]] >>> evolve(L) [[43, 44], [45, 46]] [ setNewElement( L, i ) for i in range(2) ] L 01 N2 (i.e., len(L)) [[42, 43], [44, 45]] 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 [ setNewElement( L, i ) for i in range(N) ] def setNewElement( L, i, x=0 ): """ setNewElement 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]] >>> evolve(L) [[43, 44], [45, 46]] [ setNewElement( L, i ) for i in range(2) ] L 01 N2 (i.e., len(L)) [[42, 43], [44, 45]] What is i ? What is L[i] ? Going deeper

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]] [ setNewElement2d( L, i, j ) for i in range(2) ] for j in range(2) ] def setNewElement2d( L, i, j, x=0, y=0 ): """ setNewElement 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… (255, 255, 255): white (150, 150, 150): gray

Brightening a Picture def modify(pic): """ modify modifies an image to make it brighter """ pixels = getPixels(pic) if len(pixels) == 0: return newPixels = [ [setNewPixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] setPixels(pic, newPixels) def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW imanges (row, col) (r,g,b) value input pixels: a 2D 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, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] Width: len(pixels[0]) Height: len(pixels) 2x2 pixel image

Tuples vs. Lists [ [(3, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] Tuples use ( ); lists use [ ] But otherwise, they are the same… (for now, almost) >>> t = (1, 2, 3) >>> t[1] 2 >>> t[1:] (2, 3) >>> (x, y, z) = t >>> x 1 >>> y 2

def modify(pic): """ modify modifies an image to make it brighter """ pixels = getPixels(pic) if len(pixels) == 0: return newPixels = [ [setNewPixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] setPixels(pic, newPixels) def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW imanges (row, col) (r,g,b) value input pixels: a 2D 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) Brightening a Picture

"Quiz“ Want more? How would you turn only the sky red? Name(s): It's all clear to me now! Write a function that tints the top half of the picture red (how red is up to you): def setNewPixel( pixels, row, col ): """ setNewPixel 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 setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """

"Quiz“ Want more? How would you turn only the sky red? Name(s): It's all clear to me now! Write a function that tints the top half of the picture red (how red is up to you): def setNewPixel( pixels, row, col ): """ setNewPixel 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 setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """

"Quiz“ Want more? How would you turn only the sky red? Name(s): It's all clear to me now! Write a function that tints the top half of the picture red (how red is up to you): def setNewPixel( pixels, row, col ): """ setNewPixel 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 setNewPixel( pixels, row, col ): """ setNewPixel 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]

… Caesar Cipher: encipher encipher( 'gv vw dtwvg', 0 ) encipher( 'gv vw dtwvg', 1 ) encipher( 'gv vw dtwvg', 2 ) encipher( 'gv vw dtwvg', 3 ) encipher( 'gv vw dtwvg', 4 ) encipher( 'gv vw dtwvg', 5 ) encipher( 'gv vw dtwvg', 25 ) returns 'gv vw dtwvg' 'hw wx euxwh' 'ix xy fvyxi' 'jy yz gwzyj' 'kz za hxazk' 'la ab iybal' 'fu uv csvuf' encipher( S, n ) should return the string s with each alphabetic character shifted/wrapped by n places in the alphabet

How Strings are Represented and Stored? ASCII is a table that tells the computer how to represent characters as bits! 8 bits = 1 byte The SAME bits represent integers, if the variable has type int instead of str American Standard Code for Information Interchange type: str type: int bits name: value: '*' 42 Identical bits are stored in each variable! The types determine how to interpret the bits; the names don't matter at all…

ASCII ASCII is a table that tells the computer how to represent characters as #s American Standard Code for Information Interchange chr ord convert to number convert to char.

chr and ord chr( n ) ord( c ) Input: an integer in range(255) Input: a string of one character, c abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ ASCII VALUES Output: a one-char. string of that ASCII value Output: an integer, the ASCII value of c CONVERTERS [ [i,chr(i)] for i in range(128) ] [ ord(i) for i in '**** CS! ****' ] try these!

chr and ord chr(66) is ? ord('a') is ? abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 90 ASCII VALUES What is chr( ord('i')+13 ) ? What is chr( ord('P')+13 ) ?

chr and ord chr(66) is ? ord('a') is 97 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 90 ASCII VALUES What is chr( ord('i')+13 ) ? What is chr( ord('P')+13 ) ?

chr and ord chr(66) is 'B' ord('a') is 97 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 90 ASCII VALUES What is chr( ord('i')+13 ) ? What is chr( ord('P')+13 ) ?

chr and ord chr(66) is 'B' ord('a') is 97 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 90 ASCII VALUES What is chr( ord('i')+13 )'v' What is chr( ord('P')+13 ) ?

chr and ord chr(66) is 'B' ord('a') is 97 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 90 ASCII VALUES What is chr( ord('i')+13 )'v' What is chr( ord('P')+13 ) ']' How can we wrap this around?

Rot 13 def adv13( 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 adv13( 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 adv13( 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.' But how ? >>> decipher('Uifz xpsl ju pvu xjui b qfodjm!') >>> decipher('gv vw dtwvg')

Caesar Cipher: decipher >>> decipher('gv vw dtwvg') CaesarBrutus Strategy using max : (1) consider all possible answers (2) give them each a score (3) use our techniques to get max gv vw dtwvg hw wx euxwh ix xy fvyxi jy yz gwzyj kz za hxazk la ab iybal mb bc jzcbm nc cd kadcn od de lbedo pe ef mcfep qf fg ndgfq rg gh oehgr sh hi pfihs ti ij qgjit uj jk rhkju vk kl silkv wl lm tjmlw xm mn uknmx yn no vlony zo op wmpoz ap pq xnqpa bq qr yorqb cr rs zpsrc ds st aqtsd et tu brute fu uv csvuf all 26 possibilities Score for "Englishness"? up to you…

Caesar Cipher: decipher >>> decipher('gv vw dtwvg') 'od de lbedo' CaesarBrutus Strategy using max : (1) consider all possible answers (2) give them each a score (3) use our techniques with max [0, 'cr rs zpsrc'] [0, 'gv vw dtwvg'] [0, 'jy yz gwzyj'] [0, 'mb bc jzcbm'] [0, 'qf fg ndgfq'] [0, 'wl lm tjmlw'] [1, 'bq qr yorqb'] [1, 'ds st aqtsd'] [1, 'nc cd kadcn'] [1, 'vk kl silkv'] [1, 'xm mn uknmx'] [2, 'ap pq xnqpa'] [2, 'hw wx euxwh'] [2, 'ix xy fvyxi'] [2, 'kz za hxazk'] [2, 'rg gh oehgr'] [2, 'sh hi pfihs'] [2, 'uj jk rhkju'] [2, 'yn no vlony'] [3, 'fu uv csvuf'] [3, 'pe ef mcfep'] [3, 'ti ij qgjit'] [3, 'zo op wmpoz'] [4, 'et tu brute'] [4, 'la ab iybal'] [4, 'od de lbedo'] all 26 possibilities won't always be correct! number-of-vowels score

Caesar Cipher: decipher >>> decipher('gv vw dtwvg') 'et tu brute' CaesarBrutus Strategy using max : (1) consider all possible answers (2) give them each a score (3) use our techniques with max [0.4680, 'jy yz gwzyj'] [0.4960, 'mb bc jzcbm'] [0.5420, 'uj jk rhkju'] [0.5567, 'ix xy fvyxi'] [0.5597, 'qf fg ndgfq'] [0.5718, 'fu uv csvuf'] [0.5753, 'bq qr yorqb'] [0.5833, 'kz za hxazk'] [0.5859, 'xm mn uknmx'] [0.5880, 'gv vw dtwvg'] [0.5902, 'vk kl silkv'] [0.6110, 'ap pq xnqpa'] [0.6304, 'zo op wmpoz'] [0.6318, 'wl lm tjmlw'] [0.6717, 'cr rs zpsrc'] [0.6735, 'hw wx euxwh'] [0.6963, 'nc cd kadcn'] [0.7153, 'ti ij qgjit'] [0.7398, 'la ab iybal'] [0.7442, 'yn no vlony'] [0.7867, 'pe ef mcfep'] [0.7880, 'sh hi pfihs'] [0.7918, 'rg gh oehgr'] [0.8213, 'ds st aqtsd'] [0.8609, 'od de lbedo'] [0.9082, 'et tu brute'] all 26 possibilities letter- probability score not always correct, but better!

'Weet bksa ed Xecumeha 3!'