1 / 57

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

Computing to the max. You know this would make me hungry… if it weren't 7 pm!. The not-so-subtle art of singling out the best (and worst) of anything…. Computing with language. Consider literature a bunch of strings ?. Battle-tested ciphers - and how to break them !.

aimon
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. Computing to the max You know this would make me hungry… if it weren't 7 pm! The not-so-subtle art of singling out the best (and worst) of anything… Computing with language Consider literature a bunch of strings ? Battle-tested ciphers - and how to break them ! a comparison comparison > or < Seasons by Mike Maguire 'm&ms' 'coffee' Drawn Inward [ 0, 42 ] [ 4, 2 ] True 'True' Fall leaves when leaves fall.

  2. What's ahead? I'm a head! 10/14Dynamic data - references 10/15ASCII art due 10/21no class- Fall break 10/22encipher/decipher due 10/28Objects and naming 1 10/29Wandering and Life due 11/4 Objects and naming 2 11/5Markov text due 11/5Project proposals due Conway gives a whole new outlook on "loopy" ! John Conway

  3. Recursive max def max( L ): if len(L) < 2: return L[0] elif L[0] < L[1]: return max( L[1:] ) else: return max( L[0:1] + L[2:] ) If there is only 1 element, it's the max! If the first element is less than the second, "forget about" the first one and find the max of everything else… But if the second element is smaller, forget aboutit and take the max of everything else ! A recipe for life ? and python already has it for us! The hard part is knowing what we want to maximize!

  4. Out of the box… If we want the highest price… And I thought $100 was overpriced! 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( [ [475.5,'nov'], [458.0,'jan'], [441.3,'mar'], [470.8,'may'], [532.8,'jul'], [520.9,'sep'] ] ) If we want the final word: 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) min(L) or if we want the initial word… ?

  5. "max" word L = [ "to","be","or","not","to","be","that","is","the","question" ] def maxWord( L ): """ finds the "max" word from L, a list of words """ return max(L)

  6. "max" word - part 2! L = [ "to","be","or","not","to","be","that","is","the","question" ] def maxWord( L ): """ finds the "max" word from L, a list of words """ return max(L) ?? How could we redefinemax!? This is max? lowest scrabble score? longest word? most times?

  7. "Best" word ~ longest L = [ "to","be","or","not","to","be","that","is","the","question" ] def bestWord( L ): """ finds the "best" word from L, a list of words """

  8. "Best" word ~ lowest scrabble score def scrabbleScore(w): # see homework #2! Let's abbreviate this function as scsc(w) def bestWord( L ): """ finds the "best" word from L, a list of words here, "best" means lowest scrabble score """ Add printing as you see fit!

  9. What are these? >>> bestNumber( [ 10, 20, 30, 40, 50, 60, 70 ] ) 40 >>> bestNumber( [ 100, 200, 300, 400 ] ) 100 >>> bestNumber( [ 1, 2, 3, 4, 5, 6, 7, 8, 7 ] ) 8 >>> mode( [ 1, 2, 3, 4, 5, 6, 7, 8, 7 ] ) 7 Guesses for bestNumber ? for mode ?

  10. "Coding Challenge" Nothing but the best! Write these two functions: abs( x ) is built-in to Python Hint: Use bestWord as a guide def bestNumber( L ): """ returns the # in L closest to 42 """ You may want to define a helper function ! Hint: def mode( L ): """ returns the element appearing most often in L """

  11. Try it… abs( x ) is built-in to Python Hint: Write this function: Use bestWord as a guide def bestNumber( L ): """ returns the # in L closest to 42 """

  12. Try it… You may want to define a helper function ! Hint: def mode( L ): """ returns the element appearing most often in L """

  13. An example “close to home” Hw5 Pr1 Starbucks ... ... CGU home S (E) (W) 0 22 23 24 25 26 27 28 50 An overworked CGU student (S) leaves Starbucks after their “late-night” breakfast and, each moment, randomly stumbles toward campus (W) or toward home (E) Once the student arrives at home or the classroom, the trip is complete. The program should then print the total number of steps taken. Write a program to model and analyze! this scenario...

  14. Some random info… import random for more explanation, try dir(random) or help(random) random.choice( L ) chooses 1 element from the list L random.choice( ['drucker', 'sisat', 'sah'] ) How would you get a random int from 0 to 9? random.uniform(low,hi) chooses a random float from low to hi random.uniform(41.9,42.1) How likely is this to return 42 ?

  15. An example: the ASCII equalizer How could we write a function to print lines of asterisks… ? ****** ******* ******** ******* ****** ***** **** *** **** ***** ****** ****** ****** ****** ****** ****** ****** ****** ****** ****** ****** ****** ****** ** *********** * ***** ******* ******* ********** *** **** second ~ random lengths first ~ all the same third ~ changing by 1

  16. An example: the ASCII equalizer Notes… second ~ random lengths first ~ all the same third ~ changing by 1

  17. Randomness vs. Determinism Are there random numbers? Can a computer generate them? RNG A “black box” model of a random number generator. Output

  18. Randomness vs. Determinism Are there random numbers? Can a computer generate them? Yes Not without help! The RNG revealed. Periodic! Output p = 219937-1 http://en.wikipedia.org/wiki/Mersenne_twister

  19. True Randomness ! LavaRnd’s lava lamps using a chaotic physical system to seed random number generators (Patent 5,732,138: "Method for seeding a pseudo-random number generator with a cryptographic hash of a digitization of a chaotic system.") www.wired.com/wired/archive/11.08/random.html This has since been “improved”…

  20. The two Monte Carlos Making random numbers work for you! Monte Carlo casino, Monaco Monte Carlo methods, Math/CS

  21. An example “close to home” Hw5 Pr1 Starbucks ... ... CGU home S (E) (W) 0 22 23 24 25 26 27 28 50 An overworked CGU student (S) leaves Starbucks after their “late-night” breakfast and, each moment, randomly stumbles toward campus (W) or toward home (E) Once the student arrives at home or the classroom, the trip is complete. The program should then print the total number of steps taken. Write a program to model and analyze! this scenario... rs() rwPos(s, nsteps) rwSteps(s, low, hi) take a random step of +1 or -1 take nsteps random steps starting at s take random steps starting at s until you reach either low or hi

  22. Gel electrophoresis one of many applications for random walks… uses a basic random-walk model with unequal step probabilities Used to separate proteins and nucleic acids (DNA) from a biological sample. Molecules with different properties travel different distances.

  23. print: Making programs talk to you! import time def countdown( num ): """ remarkably accurate sim. of 2006's final moments """ if num == 0: return 'Hooray!' else: print 'Countdown is', num time.sleep(1) return countdown(num-1) you can slow things down, as well! Essential for debugging …

  24. print: Making programs talk to you! Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs. - Maurice Wilkes Programming: the art of debugging an empty file. - The Jargon File http://www.tuxedo.org/~esr/jargon/

  25. The first bug Grace Hopper from the UNIVAC 1 “In the days they used oxen for heavy pulling, when one ox couldn't budge a log, they didn't try to grow a larger ox. We shouldn't be trying for bigger and better computers, but for better systems of computers.”

  26. Reference vs. Value Changeable types: Unchangeable types: tuple float list string bool int L = [7,11,'hi'] x = 42 Reference, Pointer, id 42 'hi' 11 7 L x L[0] L[1] L[2]

  27. “Pass By Value” defmain() """ calls conform """ print " Welcome to Conformity, Inc. " fav = 7 conform(fav) print " My favorite number is", fav defconform(fav) """ sets input to 42 """ fav = 42 return fav 7 fav fav

  28. “Pass By Value” defmain() """ calls conform """ print " Welcome to Conformity, Inc. " fav = 7 conform(fav) print " My favorite number is", fav defconform(fav) """ sets input to 42 """ fav = 42 return fav 7 fav PASS BY VALUE 7 42 fav “Pass by value” means that data is copied when sent to a method

  29. Passing lists by value… defmain() """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print " My favorite numbers are", fav defconform2(fav) """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42 11 7 fav L[0] L[1] fav What gets passed by value here?

  30. Passing lists by value… defmain() """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print " My favorite numbers are", fav defconform2(fav) """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42 11 7 fav L[0] L[1] The reference is copied! fav can change data elsewhere!

  31. The conclusion You can change the contents of lists in functions that take those lists as input. (actually, lists or any mutable objects) Those changes will be visible everywhere. (immutable objects are safe, however)

  32. Views of the world Engineers think their equations approximate reality.

  33. Views of the world Engineers think their equations approximate reality. Physicists think reality approximates their equations.

  34. Views of the world Engineers think their equations approximate reality. Physicists think reality approximates their equations. Mathematicians don't care.

  35. Views of the world Engineers think their equations approximate reality. Physicists think reality approximates their equations. Mathematicians don't care.  Creating structure from a few simple actions ... Creating structure from a few simple facts... for arrays variables Axioms while Definitions arithmetic operations if/else Proof Algorithm

  36. Lists’ flexibility Lists can hold ANY type of data A = [ 42., 75., 70. ] 70.0 42.0 75.0 float float list float A they don’t have to be horizontal lists!

  37. Lists’ flexibility Lists can hold ANY type of data A = [ 42., 75., 70. ] 70.0 42.0 75.0 float float list float A 42.0 list double they don’t have to be horizontal lists! A 75.0 double 70.0 double

  38. Lsits’ flexibility Lists can hold ANY type of data 70.0 42.0 75.0 double list double double A -11 42 7 int list int int A “go” “red” “sox!” String list String String A

  39. 2d lists or arrays Lists can hold ANY type of data -- including lists ! A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list A

  40. 2d arrays Lists can hold ANY type of data -- including lists ! A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list list A[0] A list A[1] list A[2]

  41. Jagged arrays Lists can hold ANY type of data -- including lists ! A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list list A[0] A list A[1] list A[2] Rows within 2d arrays need not be the same length…

  42. We will not use jagged arrays at least in hw 10 Lists can hold ANY type of data -- including lists ! A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list list A[0] A list A[1] list A[2] Rows within 2d arrays need not be the same length…

  43. Rectangular arrays A[1][2] = 42 list list A[0] A A[0][0] list A[1] list A[2] A[2][3] 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 ?

  44. Creating a 2d array def create2dArray( width, height ): """ does just that """ A = [] # start with nothing for row in range( height ): for col in range( width ): return A

  45. Displaying a 2d array from csgrid import * B = create2dArray( 10, 10 ) show( B ) B[1][8] = 1 # set this item to 1 csplot.show( B ) nearby coordinates…

  46. Problem 2 -- “Life” John Conway Grid World red cells are alive Evolutionary rules • 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 number of neighbors kill the central cell (or keep it dead) white cells are empty

  47. Problem 2 -- Life Grid World red cells are alive Evolutionary rules • 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 number of neighbors kill the central cell (or keep it dead) white cells are empty

  48. Problem 2 -- Life Grid World red cells are alive Evolutionary rules • 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 number of neighbors kill the central cell (or keep it dead) white cells are empty

  49. Problem 2 -- Life Grid World red cells are alive Evolutionary rules • 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 number of neighbors kill the central cell (or keep it dead) Keep going! white cells are empty life out there...

  50. Problem 2 -- Creating Life B = createNextLifeBoard( B ) new generation or "board" old generation or "board" 0 0 1 2 3 4 5 1 2 3 4 5 0 0 1 1 2 2 3 3 4 4 5 5

More Related