1 / 132

The not-so-subtle art of singling out the best (and worst) of anything…

Python? How about coffee – or M&Ms or Poptarts to the max?. Python to the max !. Schedule…. The not-so-subtle art of singling out the best (and worst) of anything…. hw#2 due Wed. 2/19. a comparison comparison. functions…. 'm&ms'. 'coffee'. no class 2/24. [ 0, 42 ] . [ 4, 2 ] .

arnie
Download Presentation

The not-so-subtle art of singling out the best (and worst) of anything…

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python? How about coffee – or M&Ms or Poptarts to the max? Python to the max! Schedule… The not-so-subtle art of singling out the best (and worst) of anything… hw#2 due Wed. 2/19 a comparisoncomparison functions… 'm&ms' 'coffee' no class 2/24 [ 0, 42 ] [ 4, 2 ] hw#3 due Wed. 2/26 True 'True' PythonBat > or < hw#4 due Wed. 3/5 Computing with all senses! all-senses problems Sounds good? Turtle graphics! and English

  2. Hw #3 ~ PythonBat (1) create an account (2) Do 5 each from these four groups: String-1, List-1, Logic-1, and Logic-2 That's it! You may work in pairs or on your own – that's up to you…

  3. Lines of code estimates (thanks, Patrick!)

  4. max A recipe for life ? and python already has it for us… The real problem is knowing what we want to maximize! … plus, this will help minimize your lines of code!

  5. to themax And I thought $100 was overpriced! NASDAQ: GOOG Want the highest price? max( [475.5, 458.0, 441.3, 470.8, 532.8, 520.9] ) 'nov' 'jan' 'mar' 'may' 'jul' 'sep' What if the months are in there, as well? max( [ [470.8,'may'], [532.8,'jul'], [520.9,'sep'] ] ) max( [ ['may',470.8], ['jul',532.8], ['sep',520.9] ] )

  6. to themax And I thought $100 was overpriced! NASDAQ: GOOG Want the highest price? max( [475.5, 458.0, 441.3, 470.8, 532.8, 520.9] ) 'nov' 'jan' 'mar' 'may' 'jul' 'sep' What if the months are in there, as well? max( [ [470.8,'may'], [532.8,'jul'], [520.9,'sep'] ] ) max( [ ['may',470.8], ['jul',532.8], ['sep',520.9] ] ) Mudd'smax? L = ['Harvey', 'Mudd', 'College', 'seeks', 'to', 'educate', 'engineers,', 'scientists', 'and', 'mathematicians', 'well-versed', 'in', 'all', 'of', 'these', 'areas', 'and', 'in', 'the', 'humanities', 'and', 'the', 'social', 'sciences', 'so', 'that', 'they', 'may', 'assume', 'leadership', 'in', 'their', 'fields', 'with', 'a', 'clear', 'understanding', 'of', 'the', 'impact', 'of', 'their', 'work', 'on', 'society'] max(L) Or Mudd'smin? min(L)

  7. writingmax defmax( L ): """ returns the max element from L input: L, a nonempty list """ iflen(L) < 2: # one element is the max. return L[0] elifL[0] < L[1] : #1 wins return max( L[1:] ) else: #0 wins return max( L[0:1] + L[2:] ) Spacey! I like it!

  8. scrabble-scoremax defbestWord( L ): """ returns L's max-scrabble-score word input: L, a list of strings (words) """ iflen(L) < 2: # one element is the max return L[0] elifL[0] < L[1] : #1 wins return bestWord( L[1:] ) else: #0 wins return bestWord( L[0:1] + L[2:] ) What do we need here?

  9. scrabble-scoremax defbestWord( L ): """ returns L's max-scrabble-score word input: L, a list of strings (words) """ iflen(L) < 2: # one element is the max return L[0] elifsScore(L[0]) < sScore(L[1]): #1 wins return bestWord( L[1:] ) else: #0 wins return bestWord( L[0:1] + L[2:] ) What else might be nice to know ?

  10. A more comprehensive solution L = [ 'aliens', 'zap', 'hazy', 'code' ] defbestWord( L ): """ returns L's max-scrabble-score word """ LoL = [ [sScore(w), w] for w in L ] bestpair= max( LoL ) returnbestpair[1] This does look funny!

  11. A more comprehensive solution I loathe hazy code! L = [ 'aliens', 'zap', 'hazy', 'code' ] defbestWord( L ): """ returns L's max-scrabble-score word """ LoL = [ [sScore(w), w] for w in L ] bestpair= max( LoL ) returnbestpair[1] LoL = [ [6,'aliens'], [9,'zap'], [19,'hazy'], [5,'code'] ]

  12. A more comprehensive solution I loathe hazy code! L = [ 'aliens', 'zap', 'hazy', 'code' ] defbestWord( L ): """ returns L's max-scrabble-score word """ LoL = [ [sScore(w), w] for w in L ] bestpair= max( LoL ) returnbestpair[1] LoL = [ [6,'aliens'], [9,'zap'], [19,'hazy'], [5,'code'] ] bestpair = [19,'hazy'] 'hazy'

  13. Other examples… What is bestnumb? What is mostnumb? >>>bestnumb( [10,20,30,40,50,60,70] ) 40 >>>bestnumb( [100,200,300,400] ) 100 >>>bestnumb( [1,2,3,4,5,6,7,8,7] ) 8 >>>mostnumb( [1,2,3,4,5,6,7,8,7] ) 7 These functions have made me number

  14. Try it! L = ['aliens', 'zap', 'hazy', 'code' ] defbeststr(L): LoL = [ [len(s),s] for s in L ] bst = max( LoL ) returnbst[1] • 1. What is LoL? • 2. What is bst? Extra! Change exactly three characters so that 3 is returned. • 3. What is returned? Use the LoL method to write these two functions defbestnumb(L): """ returns the # in L closest to 42 """ LoL = [ ] bst = max( LoL ) returnbst[1] Hint: Python has abs(x)built-in defmostnumb( L ): """ returns the item most often in L """ LoL= [ ] bst = max( LoL ) returnbst[1] • Hint:Use this helper function! defcount(e,L): LC = [e==x for x in L] returnsum(LC)

  15. L = ['aliens', 'zap', 'hazy', 'code' ] defbeststr(L): LoL = [ [len(s),s] for s in L ] bst = max( LoL ) returnbst[1] [ [6,'aliens'], [3,'zap'], [4,'hazy'], [4,'code']] • 1. What is LoL? [6,'aliens'] • 2. What is bst? 'aliens' • 3. What is returned? Extra!Change exactly three characters so that 3 is returned.

  16. Example defbestnumb( L ): """ returns the # closest to 42 in L """ LoL = [ [abs(x-42),x] for x in L ] bst = min( LoL ) returnbst[1] [30,40,50] L LoL [[12,30],[2,40],[8,50]] [2,40] bst 40

  17. Example Helper function: count(e,L) def count( e, L ): """ returns the # of e's in L """ LC = [ e==x for x in L ] returnsum( LC ) L [6,7,7,8] LoL [ [1,6], [2,7], [2,7], [1,8]] [2,7] bst defmostnumb( L ): """ returns the item most often in L """ LoL = [ [count(x,L),x] for x in L ] bst = max( LoL ) returnbst[1] 7

  18. Big data? Any guesses as to what kind of data this is? I find your lack of faith in this data disturbing.

  19. One example of big data: sound

  20. Sound physics continuous variation of air pressure vs. time air pressure sampling samples taken every 1/22050th of a second (or some sampling rate) time quantization Each sample is measured on a loudness scale from -32,768 to 32,767. (This fits into 2 bytes.) storage These two bytes are called a frame. Raw audio data - such as what is written to the surface of a CD - is simply a list of these frames. pulse code modulation = PCM data

  21. hw#4, problem #1 (0) starter problems on list comprehensions… (1) reverse a sound (2) alter its volume (3) add static (4) overlay two sounds (5) create an echo effect (6) create a chord of pure tones

  22. From sound… I have three eyes and I still can't believe what I'm seeing…! … to sight www.gvetchedintime.com

  23. Python's Etch-a-Sketch fromturtleimport* Turtle Canvas reset() shape('turtle') # cool! width(5) left(90) forward(50) right(90) backward(50) down() orup() color('darkgreen') tracer(1)ortracer(0) width(5) pixels! (42,42) degrees! pixels! window_height() (0,0) states if the pen draws or not states if the pen animates or not window_width() http://docs.python.org/library/turtle.html

  24. Recursive Graphics A triangle-drawing f'n: Could we tri this with recursion? (1) def tri(): """ draws a triangle """ forward(100) left(120) forward(100) left(120) forward(100) left(120) def tri( n): """ draws a triangle """ if n==0: return else: forward(100) # one side left(120) tri( n-1 ) # draw rest Could we create any regular N-gon? (2) defpoly( n, N): """ draws a polygon """ I don't know about tri, but there sure is NO return … !

  25. Be the turtle ! (2) Finish rwalk so it draws a "stock-market" path: Nstepsof 10 pixels each. Use recursion. (1) What does chai(100)draw? from random import * defrwalk(N): """ N 10-pixel steps, each NE or SE """ if N == 0: return ifchoice(['left','right']) == 'left': else: # this handles 'right' left(45) forward(10) def chai(size): """ mystery! """ forward(size) left(90) forward(size/2.0) right(90) right(90) forward(size) left(90) left(90) forward(size/2.0) right(90) backward(size) Ex Cr: How could you make it a bull (or a bear) market? one possible result of rw(20)

  26. (1) What does chai(100)draw? def chai(size): """ mystery! """ if size<9: return forward(size) left(90) forward(size/2.0) right(90) right(90) forward(size) left(90) left(90) forward(size/2.0) right(90) backward(size) Why are there two identical commands in a row? How could you add more to each T-tip?

  27. from random import * defrwalk(N): """ N 10-px steps, NE or SE """ if N == 0: return if choice(['left','right'])=='left': left(45) forward(10) right(45) rwalk( N-1 ) else: # 'right' right(45) forward(10) left(45) rwalk( N-1 ) (2) rwalk is a random stock market walk... What if we didn't turn back to face east each time? Extra: Creating a bull (or a bear) market?

  28. hw4pr2 81 fractal art! 90 spiral(100,90,0.9) 100 spiral( initLength, angle, multiplier )

  29. hw4pr2 Be sure you return the turtle to its starting position! levels == 4 levels == 3 levels == 2 levels == 1 levels == 0 (no drawing) svtree( 100, 4 ) svtree( trunkLength, levels )

  30. The Koch curve (extra credit) This is SoCal snow! snowflake(100, 0) snowflake(100, 1) snowflake(100, 2) snowflake(100, 3) snowflake(100, 4) snowflake(100, 5)

  31. there's sound… there's sight… … and there's language

  32. Computing with language ideas / meaning language / words / phrases Python strings are here. strings numbers / bits

  33. Computing with language open questions in AI… ideas / meaning language / words / phrases processing language – how English-y is it? strings how strings are represented and stored numbers / bits

  34. returns returns returns returns returns returns returns hw4pr3: Caesar Cipher / encipher 'I <3 Latin' 'J <3 Mbujo' 'K <3 Ncvkp' 'L <3 Odwlq' 'M <3 Pexmr' 'N <3 Qfyns' encipher( 'I <3 Latin' , 0 ) encipher( 'I <3 Latin' , 1 ) encipher( 'I <3 Latin' , 2 ) encipher( 'I <3 Latin' , 3 ) encipher( 'I <3 Latin' , 4 ) encipher( 'I <3 Latin' , 5 ) 'H <3 Kzshm' encipher( 'I <3 Latin' , 25 )

  35. returns returns returns returns returns returns returns Caesar Cipher: encipher should return the string swith each alphabetic character shifted/wrapped by nplaces in the alphabet encipher(s,n) 'I <3 Latin' 'J <3 Mbujo' 'K <3 Ncvkp' 'L <3 Odwlq' 'M <3 Pexmr' 'N <3 Qfyns' encipher( 'I <3 Latin' , 0 ) encipher( 'I <3 Latin' , 1 ) encipher( 'I <3 Latin' , 2 ) encipher( 'I <3 Latin' , 3 ) encipher( 'I <3 Latin' , 4 ) encipher( 'I <3 Latin' , 5 ) 'H <3 Kzshm' encipher( 'I <3 Latin' , 25 )

  36. ASCII American Standard Code for Information Interchange • 1 byte 8 bits The SAME bits represent an integer or a string, depending on type: int or str

  37. ASCII American Standard Code for Information Interchange over in memory… value: '/' 00101111 bits type:str name: Identical bits are stored in each variable! The types determine how to interpret the bits; the names don't matter at all… • 1 byte value: 47 8 bits 00101111 bits type:int name: The SAME bits represent an integer or a string, depending on type: int or str

  38. ASCII convert # to char chr ord convert char to #

  39. chrandord abcdefghijklmnopqrstuvwxyz ASCII values 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 Input: a string of one character, c ord( c ) Output: an integer, the ASCII value of c Conversion functions Input: an integer in range(255) chr( n ) Output: a one-char. string of that ASCII value [ [i,chr(i)] for i in range(128) ] Try these! [ ord(i) for i in'**** CS! ****' ]

  40. chr,ord, and rot13 What is ord('U')/2? What is chr( ord('i')+13 )? What is chr( ord('W')+13 )? I hate being the punchline! No joke: rot13 vs. q-encoding: qqqaqlqqqqiqqiqqqqqqqiqeqqqqqnqqqqqq

  41. chr,ord, and rot13 What is ord('U')/2? What is chr( ord('i')+13 )? What is chr( ord('W')+13 )? I hate being the punchline! No joke: rot13 vs. q-encoding: qqqaqlqqqqiqqiqqqqqqqiqeqqqqqnqqqqqq

  42. Rot 13 def rot13( c ): """ rotates c by 13 chars, "wrapping" as needed NON-LETTERS don't change! """ if'a' <= c <= 'z': new_ord = ord(c) + 13 ifnew_ord <= ord('z'): returnchr(new_ord) else: return chr( ) elif'A'<= c <= 'Z': # upper-case test! else: Rotate n? Rotate an entire string?

  43. Caesar Cipher: decipher Caesar Brutus >>> decipher('Bzdrzqbhogdq? H oqdedqBzdrzqrzkzc.') '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('Uifz xpsl ju pvu xjui b qfodjm!') >>> decipher('gvvwdtwvg') But how ?

  44. Decipher – examples… >>> decipher('Bzdrzqbhogdq? H oqdedqBzdrzqrzkzc.') 'Caesar cipher? I prefer Caesar salad.' Strategies? Algorithms?

  45. gvvwdtwvg hwwxeuxwh ix xyfvyxi jyyzgwzyj kzzahxazk la abiybal mbbcjzcbm nc cd kadcn od de lbedo peefmcfep qffgndgfq rgghoehgr sh hi pfihs tiijqgjit ujjkrhkju vk kl silkv wl lm tjmlw xmmnuknmx yn no vlony zo op wmpoz appqxnqpa bqqryorqb crrszpsrc ds staqtsd et tu brute fuuvcsvuf Decipher All possible decipherings Strategies? Algorithms?

More Related