David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards.

Slides:



Advertisements
Similar presentations
Cs1120 Fall 2009 David Evans Lecture 11: Generalizing List Procedures 1.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 13: Of On and Off Grounds Sorting.
Cs1120 Fall 2009 David Evans Lecture 15: Running Practice.
Cs1120 Fall 2009 David Evans Lecture 16: Power Analysis.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 18: The Story So Far.
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.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 14: Asymptotic Growth.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 11: 1% Pure Luck Make-up lab hours:
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 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 12: Decrypting Work Circle Fractal.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 11: CS Logo, by Lincoln Hamilton and.
Cs3102: Theory of Computation Class 24: NP-Completeness Spring 2010 University of Virginia David Evans.
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 Class 19: Golden Ages and Astrophysics CS200: Computer Science University of Virginia Computer Science.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 9: Strange Loops and Sinister Repeaters.
David Evans Class 21: The Story So Far (Quicksort, Continuing Golden Ages) CS200: Computer Science University of Virginia.
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 Class 16: NP-Completeness / The Story so Far CS150: Computer Science University of Virginia Computer Science.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 9: Of On and Off Grounds Sorting Coffee.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 15: Intractable Problems (Smiley.
David Evans Class 15: P vs. NP (Smiley Puzzles and Curing Cancer) CS150: Computer Science University of Virginia Computer.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
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.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 14: P = NP?
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Intractable Problems (Smiley Puzzles.
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 17: Environments CS200: Computer Science
Lecture 4: Evaluation Rules Recursion CS200: Computer Science
Lecture 13: Quicksorting CS200: Computer Science
Class 30: Models of Computation CS200: Computer Science
Lecture 7: List Recursion CS200: Computer Science
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 16: Quickest Sorting CS150: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Class 14: Intractable Problems CS150: Computer Science
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 28: Types of Types
Lecture 13: Cost of Sorts CS150: Computer Science
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
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
Class 24: Computability Halting Problems Hockey Team Logo
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 3: Rules of Evaluation CS200: Computer Science
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 23: Computability CS200: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

David Evans CS150: Computer Science University of Virginia Computer Science Lecture 10: Puzzling Pegboards

2 Lecture 10: Pegboard Puzzle Menu Problem Sets 2 and 3 Pegboard Puzzler

3 Lecture 10: Pegboard Puzzle Problem Sets Not just meant to review stuff you should already know –Get you to explore new ideas –Motivate what is coming up in the class The main point of the PSs is learning, not evaluation –Don’t give up if you can’t find the answer in the book (you won’t solve many problems this way) –Do discuss with other students

4 Lecture 10: Pegboard Puzzle PS2: Question 3 Why is (define (higher-card? card1 card2) (> (card-rank card1) (card-rank card2) better than (define (higher-card? card1 card2) (> (car card1) (car card2)) ?

5 Lecture 10: Pegboard Puzzle PS2: Question 8, 9 Predict how long it will take Identify ways to make it faster Most of next week and much of many later classes will be focused on how computer scientists predict how long programs will take, and on how to make them faster.

6 Lecture 10: Pegboard Puzzle Can we do better? (define (find-best-hand hole-cards community-cards) (car (sort (possible-hands hole-cards community-cards)) higher-hand?))

7 Lecture 10: Pegboard Puzzle Hmmm.... (define (find-closest goal lst closeness) (if (= 1 (length lst)) (car lst) (pick-closest closeness goal (car lst) (find-closest goal (cdr lst) closeness)))) (define (pick-closest closeness goal num1 num2) (if (< (closeness goal num1) (closeness goal num2)) num1 num2))

8 Lecture 10: Pegboard Puzzle find-bestest (define (find-bestiest lst bestiness) (if (= 1 (length lst)) (car lst) (pick-bestier bestiness (car lst) (find-bestiest goal (cdr lst) bestiness)))) (define (pick-bestier bestiness num1 num2) (if (bestiness num1 num2) num1 num2))

9 Lecture 10: Pegboard Puzzle find-best-hand (define (find-bestiest lst bestiness) (if (= 1 (length lst)) (car lst) (pick-bestier bestiness (car lst) (find-bestiest (cdr lst) bestiness)))) (define (pick-bestier bestiness num1 num2) (if (bestiness num1 num2) num1 num2)) (define (find-best-hand lst) (find-bestest lst higher-hand?)) Next week: how much better is this?

10 Lecture 10: Pegboard Puzzle PS3: Lindenmayer System Fractals

11 Lecture 10: Pegboard Puzzle L-Systems CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence

12 Lecture 10: Pegboard Puzzle L-System Rewriting Start: (F) Rewrite Rule: F  (F O(R30 F) F O(R-60 F) F) Work like BNF replacement rules, except replace all instances at once! Why is this a better model for biological systems? CommandSequence ::= ( CommandList ) CommandList ::= Command CommandList CommandList ::= Command ::= F Command ::= RAngle Command ::= OCommandSequence

Level 0 (F) Level 1 F  (F O(R30 F) F O(R-60 F) F) Start: (F) (F O(R30 F) F O(R-60 F) F)

14 Lecture 10: Pegboard Puzzle Level 2 Level 3

15 Lecture 10: Pegboard Puzzle The Great Lambda Tree of Ultimate Knowledge and Infinite Power (Level 5 with color)

16 Lecture 10: Pegboard Puzzle Rose Bush by Jacintha Henry and Rachel Kay Tie Dye by Bill Ingram

17 Lecture 10: Pegboard Puzzle 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

18 Lecture 10: Pegboard Puzzle 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?

19 Lecture 10: Pegboard Puzzle 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))))

20 Lecture 10: Pegboard Puzzle 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)))

21 Lecture 10: Pegboard Puzzle 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.

22 Lecture 10: Pegboard Puzzle 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)

23 Lecture 10: Pegboard Puzzle 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))

24 Lecture 10: Pegboard Puzzle 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 …

25 Lecture 10: Pegboard Puzzle Charge Next class: we’ll finish a pegboard puzzle solver and find out if how hard it is to be “genius” I have office hours now Make progress on PS3