David Evans CS150: Computer Science University of Virginia Computer Science Lecture 11: 1% Pure Luck Make-up lab hours:

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Advertisements

Class 21: Imperative Programming University of Virginia cs1120 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
CS 116 Tutorial 2 Functional Abstraction. Reminders Assignment 2 is due this Wednesday at Noon.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 16: Quicker Sorting.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 40: Computing with Glue and Photons.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 26: Printing and Stuff.
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans Class 12: Quickest Sorting CS150: Computer Science University of Virginia Computer Science Rose Bush by Jacintha.
David Evans Class 15: Golden Ages and Astrophysics CS200: Computer Science University of Virginia Computer Science.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
331 Final Spring Details 6-8 pm next Monday Comprehensive with more emphasis on material since the midterm Study example finals and midterm exams.
Cs3102: Theory of Computation Class 24: NP-Completeness Spring 2010 University of Virginia David Evans.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
David Evans Class 20: Quick Sorting CS200: Computer Science University of Virginia Computer Science Queen’s University,
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.
David Evans Class 19: Golden Ages and Astrophysics CS200: Computer Science University of Virginia Computer Science.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
Class 8: Recursing on Lists David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: QuickSorting Queen’s University,
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Mutation M. C. Escher, Day and.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 8: Crash Course in Computational Complexity.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Class 7: List Procedures David Evans cs1120 Fall 2009.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Recursing Recursively Richard Feynman’s.
Lecture 4: Metacircles Eval Apply David Evans
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Lecture 13: Quicksorting CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
6.001 SICP Data abstractions
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Lecture 18 Infinite Streams and
Lecture 11: All Sorts CS200: Computer Science University of Virginia
David Evans Lecture 9: The Great Lambda Tree of Infinite Knowledge and Ultimate Power CS200: Computer Science University.
Lecture 18.
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture #8 מבוא מורחב.
Lecture 22: P = NP? CS200: Computer Science University of Virginia
Lecture 10: Quicker Sorting CS150: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
Lecture 9: The Great Lambda Tree of Knowledge and Power
6.001 SICP Data abstractions
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 11: 1% Pure Luck Make-up lab hours: 4:30-6 today

2 Lecture 11: 1% Luck Pegboard Puzzle 1,1 2,1 2,2 3,1 3,2 3,3 4,1 4,2 4,3 4,4 5,1 5,2 5,3 5,4 5,5

3 Lecture 11: 1% Luck Solving the Pegboard Puzzle How to represent the state of the board? –Which holes have pegs in them How can we simulate a jump? –board state, jump positions  board state How can we generate a list of all possible jumps on a given board? How can we find a winning sequence of jumps?

4 Lecture 11: 1% Luck Data Abstractions (define (make-board rows holes) (cons rows holes)) (define (board-holes board) (cdr board)) (define (board-rows board) (car board)) (define (make-position row col) (cons row col)) (define (get-row posn) (car posn)) (define (get-col posn) (cdr posn)) (define (same-position pos1 pos2) (and (= (get-row pos1) (get-row pos2)) (= (get-col pos1) (get-col pos2))))

5 Lecture 11: 1% Luck Removing a Peg ;;; remove-peg evaluates to the board you get by removing a ;;; peg at posn from the passed board (removing a peg adds a ;;; hole) (define (remove-peg board posn) (make-board (board-rows board) (cons posn (board-holes board))))

6 Lecture 11: 1% Luck Adding a Peg ;;; add-peg evaluates to the board you get by ;;; adding a peg at posn to board (adding a ;;; peg removes a hole) (define (add-peg board posn) (make-board (board-rows board) (remove-hole (board-holes board) posn)))

7 Lecture 11: 1% Luck Remove Hole (define (remove-hole lst posn) (if (same-position (car lst) posn) (cdr lst) (cons (car lst) (remove-hole (cdr lst) posn)))) What if we had a procedure (filter proc lst) that removes from lst all elements for which proc (applied to that element) is false? Could we define remove-hole using map ? No. (length (map f lst)) is always the same as (length lst), but remove-hole needs to remove elements from the list.

8 Lecture 11: 1% Luck Filter (define (filter proc lst) (if (null? lst) null (if (proc (car lst)) ; proc is true, keep it (cons (car lst) (filter proc (cdr lst))) (filter proc (cdr lst))))) ; proc is false, drop it > (filter (lambda (x) (> x 0)) (list )) (1 4 2)

9 Lecture 11: 1% Luck Filter Remove (define (filter proc lst) (if (null? lst) null (if (proc (car lst)) ; proc is true, keep it (cons (car lst) (filter proc (cdr lst))) (filter proc (cdr lst))))) ; proc is false, drop it (define (remove-hole lst posn) (filter (lambda (pos) (not (same-position pos posn))) lst))

10 Lecture 11: 1% Luck Jumps ;;; move creates a list of three positions: a start (the posn that the ;;; jumping peg starts from), a jump (the posn that is being jumped ;;; over), and end (the posn that the peg will end up in) (define (make-move start jump end) (list start jump end)) (define (get-start move) (first move)) (define (get-jump move) (second move)) (define (get-end move) (third move)) ;;; execute-move evaluates to the board after making move ;;; move on board. (define (execute-move board move) (add-peg (remove-peg (remove-peg board (get-start move)) (get-jump move)) (get-end move)))

11 Lecture 11: 1% Luck Solving the Peg Board Game Try all possible moves on the board Try all possible moves from the positions you get after each possible first move Try all possible moves from the positions you get after trying each possible move from the positions you get after each possible first move …

12 Lecture 11: 1% Luck Finding a Winning Strategy Start How is winning 2-person games (e.g., chess, poker) different?... Winning position!

13 Lecture 11: 1% Luck Pegboard Puzzle 1,1 2,1 2,2 3,1 3,2 3,3 4,1 4,2 4,3 4,4 5,1 5,2 5,3 5,4 5,5 How do we find all possible jumps that land in a given target hole?

14 Lecture 11: 1% Luck Pegboard Puzzle 1,1 2,1 2,2 3,1 3,2 3,3 4,1 4,2 4,3 4,4 5,1 5,2 5,3 5,4 5,5 How do we find all possible jumps that land in a given target hole?

15 Lecture 11: 1% Luck Pegboard Puzzle 1,1 2,1 2,2 3,1 3,2 3,3 4,1 4,2 4,3 4,4 5,1 5,2 5,3 5,4 5,5 How do we find all possible jumps that land in a given target hole?

16 Lecture 11: 1% Luck All Moves into Target ;;; generate-moves evaluates to all possible moves that move a peg into ;;; the position target, even if they are not contained on the board. (define (generate-moves target) (map (lambda (hops) (let ((hop1 (car hops)) (hop2 (cdr hops))) (make-move (make-position (+ (get-row target) (car hop1)) (+ (get-col target) (cdr hop1))) (make-position (+ (get-row target) (car hop2)) (+ (get-col target) (cdr hop2))) target))) (list (cons (cons 2 0) (cons 1 0)) ;; right of target, hopping left (cons (cons -2 0) (cons -1 0)) ;; left of target, hopping right (cons (cons 0 2) (cons 0 1)) ;; below, hopping up (cons (cons 0 -2) (cons 0 -1)) ;; above, hopping down (cons (cons 2 2) (cons 1 1)) ;; above right, hopping down-left (cons (cons -2 2) (cons -1 1)) ;; above left, hopping down-right (cons (cons 2 -2) (cons 1 -1)) ;; below right, hopping up-left (cons (cons -2 -2) (cons -1 -1)))))) ;; below left, hopping up-right

17 Lecture 11: 1% Luck All Possible Moves (define (all-possible-moves board) (append-all (map generate-moves (board-holes holes)))) (define (append-all lst) (if (null? lst) null (append (car lst) (append-all (cdr lst))))) But…only legal if: start and end are positions on the board containing pegs! Note: could use (apply append...) instead of append-all.

18 Lecture 11: 1% Luck Legal Move (define (legal-move? move) ;; A move is valid if: ;; o the start and end positions are on the board ;; o there is a peg at the start position ;; o there is a peg at the jump position ;; o there is not a peg at the end position (and (on-board? board (get-start move)) (on-board? board (get-end move)) (peg? board (get-start move)) (peg? board (get-jump move)) (not (peg? board (get-end move)))))

19 Lecture 11: 1% Luck All Legal Moves (define (legal-moves board) (filter legal-move? (all-possible-moves board))) (define (legal-move? move) ;; A move is valid if: ;; o the start and end positions are on the board ;; o there is a peg at the start position ;; o there is a peg at the jump position ;; o there is not a peg at the end position (and (on-board? board (get-start move)) (on-board? board (get-end move)) (peg? board (get-start move)) (peg? board (get-jump move)) (not (peg? board (get-end move))))) (define (all-possible-moves board) (append-all (map generate-moves (board-holes holes))))

20 Lecture 11: 1% Luck Becoming a “Genius”! Start... Winning position! Try all possible legal moves

21 Lecture 11: 1% Luck Winning Position How do we tell if a board is in a winning position?

22 Lecture 11: 1% Luck is-winning-position? (define (board-squares board) (count-squares (board-rows board))) (define (count-squares nrows) (if (= nrows 1) 1 (+ nrows (count-squares (- nrows 1))))) (define (is-winning-position? board) (= (length (board-holes board)) (- (board-squares board) 1)))

23 Lecture 11: 1% Luck Solve Pegboard (define (solve-pegboard board) (find-first-winner board (legal-moves board))) (define (find-first-winner board moves) (if (null? moves) (if (is-winning-position? board) null ;; Found winning game, no moves needed #f) ;; A losing position, no more moves (let ((result (solve-pegboard (execute-move board (car moves))))) (if result ;; winner (not #f) (cons (car moves) result) ; this move leads to winner! (find-first-winner board (cdr moves)))))) ; try rest

24 Lecture 11: 1% Luck All Cracker Barrel Games (starting with peg 2 1 missing) Pegs Left Number of Ways Fraction of Games IQ Rating “You’re Genius” “You’re Purty Smart” “Just Plain Dumb” “Just Plain Eg-no-ra-moose” Leaving 10 pegs requires much more brilliance than leaving 1!?

25 Lecture 11: 1% Luck Charge By luck alone, you can be a genius 1% of the time! By trying all possibilities, you can always be a genius –Next week and later: do we have time for this? PS3 due Monday –Extra Lab hours: today (4:30-6) –Regularly scheduled lab hours: Sunday (4-5:30, 8-9:30)