1 / 18

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 I hadn't just had lunch!. The not-so-subtle art of singling out the best (and worst) of anything…. How to break apart a tough problem. What CS is really about. thinking like a machine. variables. 42. storage. int guess.

nemo
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 I hadn't just had lunch! The not-so-subtle art of singling out the best (and worst) of anything… How to break apart a tough problem

  2. What CS is really about thinking like a machine variables 42 storage int guess sequences 'w' 'a' 'r' 't' str s[1] str s[2] str s[3] str s[0] if…elif…else making decisions “high” or “low” recursion repeated actions

  3. What CS is really about thinking like a machine thinking for a machine variables library 42 storage int guess deciding how to use these tools sequences functions 'w' 'a' 'r' 't' str s[1] str s[2] str s[3] str s[0] creating your own tools ... if…elif…else classes making decisions creating your own data structures ... “high” or “low” (later) recursion repeated actions

  4. Top-down program design Given: a description of the problem translation! Wanted: a function that solves it

  5. Top-down program design Given: a description of the problem translation! Wanted: a function that solves it with as much detail as possible 1. Visualize what the program will do 2. Break up the work into a set of smaller tasks 3. Compose solutions for these tasks (functions) variables, lists, if…elif…else, recursion • What do you need for each? • Are these tasks still too big? if so, go to step 1… 1. Visualize what the function will do ...

  6. Top-down program design Given: a description of the problem translation! Wanted: a function that solves it with as much detail as possible 1. Visualize what the program will do 2. Break up the work into a set of smaller tasks How to do this… 3. Compose solutions for these tasks (functions) variables, lists, if…elif…else, recursion • What do you need for each? • Are these tasks still too big? if so, go to step 1… 1. Visualize what the function will do ...

  7. Monte Carlo Monty Hall Suppose you always switch to the other door... What are the chances that you will win the car ? Run it (randomly) 1000 times and see! How can we write MCMH? MCMH seems way too complicated for me!

  8. Monte Carlo Monty Hall How can we write MCMH? What data do we need to keep track of? What is the input/output of your function? MCMH seems way too complicated for me!

  9. Monte Carlo Monty Hall How can we write MCMH? What specific actions does your function need to take? Still seems way too complicated for me!

  10. Monte Carlo Monty Hall How can we write MCMH? Put it all together into an algorithm… OK, maybe it's not sooo complicated

  11. Monte Carlo Monty Hall Then translate the algorithm to code! def MCMH( init, sors, N ): """ plays the same "Let's make a deal" game, N times returns the number of times you win the car """ if N == 0: return 0 # don't play, can't win carDoor = choice([1,2,3]) # where is the car? if init == carDoor and sors == 'stay': result = 'Car!' elif init == carDoor and sors == 'switch': result = 'Spam.' elif init != carDoor and sors == 'switch': result = 'Car!' else: result = 'Spam.' print 'You get the', result if result == 'Car!': return 1 + MCMH( init, sors, N-1 ) else: return 0 + MCMH( init, sors, N-1 ) Wow, that was easy!

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

  13. Sorting a List 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?

  14. Taking only one… defremoveOne( e, L ): """ this function removes the first element e from the top level of the list L """ iflen(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]) removeOne('p', 'computer programming') 'comuter programming' [5,7,8,42]

  15. max A recipe for life ? and python already has it for us… The hard part is knowing what we want to maximize!

  16. Google Inc to themax If we want the highest price… Yikes! max( [449.5, 580.0, 562.4, 481.3, 498.3, 414.5] ) 'apr' 'may' 'jun' 'jul' 'aug' 'sep' What if the months are in there, as well? max([ [449.5,'apr'], [580.0,'may'], [562.4,'jun'], [481.3,'jul'], [498.3,'aug'], [414.5,'sep'] ]) If we want the final word: L = [’WSU', ’Vancouver', '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 if we want the initial word: min(L)

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

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

More Related