1 / 46

C S 5

C S 5. C SCIENCE 5!. NO CS 5 next week…. Final 2 Labs: up to you. C SKILLS 5?. Looking back / Looking ahead. ACM results…. Week 12 (this week):. Recursion. Week 14:. Software engineering + algorithms. Week 15:. Is there science in CS ?. due Sunday, 11/21 at midnight.

brilliant
Download Presentation

C S 5

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. CS 5 C SCIENCE 5! NO CS 5 next week… Final 2 Labs: up to you C SKILLS 5? • Looking back / Looking ahead ACM results… Week 12 (this week): Recursion Week 14: Software engineering + algorithms Week 15: Is there science in CS ? due Sunday, 11/21 at midnight M/T sections • HW 12 (2 problems) due Monday, 11/22 at midnight W/Th sections Recitation for HW12 -- Friday 8:00am Reading: weeks 12/13: Recursion

  2. Recursion: factorial example public static double factorial(int N) { if (N < 2) { return 1.0; } else { return N * factorial(N-1); } } Base Case Recursive Step

  3. Natural scenes mountain terrain seascapes the 2d algorithm...

  4. public double[] plyN(Board b, int N) { N Ply double[]s= new double[7]; for (int c=0 ; c<7 ; ++c) { if (b.allowsMove(c)) { b.addMove(c,me()); if (b.isOver()) s[c] = evaluate(b); else { Player foe = new Player(you(),N-1,2); double[] s2 = foe.findScores(b); s[c] = 100 - getMax(s2); } b.removeMove(c); } else s[c] = -1.0; } return s; } self-reference?

  5. Problem 2 / Final Project A “film database” application: 5 classes class name data member data member type ??

  6. Hw 12 Problem 2 -- Two tasks • Implement the main menu (in the CS5App class) Please choose an action from the following list: 0 Display All Directors 1 Display All Films 2 Display Films by Title 3 Display Films by Director 4 Display Films by Year 5 Display Films by Rating (G, PG, …) 6 Display Films by Review (0 to 10) 7 Add a New Film 8 Save film database to file 9 Load film database from file 42 Quit • Create skeleton versions of the necessary classes...

  7. Problem 2 - skeleton code + comments Data Members Methods Classes all private ! CS5App static FilmDB F static DirectorDB D public static void main(String[] args) public static void printMenu() public static void saveDB(String file) public static void readDB(String file) public static void readFilm() Film public Film(String title, int year, String rating, double review, Director dir) public String getTitle() public int getYear() public String getRating() public double getReview() public void display() public void save() String title int year String rating double review Director dir // comment here public String getRating() { return }

  8. Data Members Methods Classes all private ! Director String fname String lname FilmDB filmDB public Director(String fn, String ln, int capacity) public String getFullName() public String getFirstName() public String getLastName() public FilmDB getFilmDB() // comment here! public FilmDB getFilmDB() { return } DirectorDB similar to FilmDB below – see the assignment description… FilmDB public FilmDB(int capacity) public boolean isFull() public int getCount() public void addFilm(Film f) public boolean checkForFilm(String fulltitle) public void saveAllFilms() public void displayAllFilms() public void displayFilmsByTitle(String titlepiece) public void displayFilmsByYear(int year) public void displayFilmsByRating(String rating) public void displayFilmsByReview(double minreview) int count Film[] Films

  9. static static stuff belongs to a class, not an object USING STATIC METHODS printMenu(); in the CS5App class getMax(double[] s); in the Player class Math.sqrt(Math.PI + Math.E); If static stuff is in another class, use that class’s name! H.pl(“I’m a static method”); STATIC DATA static double PI, E; in the Math class seen globally in all of the class’s methods… static FilmDB F; in the CS5App class NONSTATIC DATA char checker; in the Player class data that is different for every object of the class type String title; in the Film class

  10. Self-referential data ? A film database application: class Director class Director { private String fname; private String lname; private FilmDB filmDB; // method skeletons… FilmDB filmDB String fname FilmDB filmDB String lname class Film class Film { private String title; private int year; … private Director dir; // method skeletons String rating String title double review Director int year Director dir dir class FilmDB class FilmDB { private int count; private Film[] films; // method skeletons int count Film[] films[0] films[1] Film[] films films

  11. Recursion is defining something in terms of itself factorial 5! = 5 * 4 * 3 * 2 * 1 N! = N * (N-1) * (N-2) * … * 3 * 2 * 1

  12. Recursion -- warning ! public static double factorial(int N) { return N * factorial(N-1); } No base case -- the calls to factorial will never stop! Make sure you have a base case, then worry about the recursive step...

  13. Recursion: factorial example public static double factorial(int N) { if (N < 2) { return 1.0; } else { return N * factorial(N-1); } } Base Case Recursive Step shorter?

  14. Recursion: factorial example public static double factorial(int N) { if (N < 2) return 1.0; else return N * factorial(N-1); } Base Case Recursive Step Slightly shorter version without curly braces... Even shorter versions soon!

  15. Recursion -- how it works factorial(5) 5 * factorial(4) 4 * factorial(3) 3 * factorial(2) 2 * factorial(1) 1.0 5 * 4 * 3 * 2 * 1.0 is 120.0

  16. Recursion -- why ? Exploits self-similarity Less work ! Produces short, elegant code Skeptical ? use the ? : operator : double factorial(int N) { } thought the ratio: code

  17. Recursion Mantra Let recursion do the work for you. I could do that in my sleep! Says stunned CS 5 student: I nodded off at the keyboard and when I woke up, my program was done! Delighted CodeWarrior user plans on more Sunday night naps!

  18. Recursion for concise coding public static void main(String[] args) { double d = exp(2.0,10); } public static double exp(double x, int n) { if (n < 0) return 0.0; return power(x,n)/fac(n) + exp(x,n-1); }

  19. Recursion with arrays double[] A = { 4, 7, -10, 4, 1 }; array initialization 4 7 -10 4 1 0 1 2 3 4 sum(A, 0, 4)returns 6.0 sum(A, 3, 4)returns 5.0 Base Case: Recursive Step:

  20. Recursion with arrays Recursively summing an array: double sum(double[] A, int L, int U)

  21. Recursion -- not just numbers Self-similarity elsewhere... Natural phenomena Relationships What is an “ancestor” ? Names -- acronyms GNU how much stem?

  22. A String of recursive methods How many A’s are in a particular string? RJACKSON GATTACA Base Case: Recursive Step:

  23. substring String s = “RECURSION”; s.substring(3, 5) is the string“UR” s.substring(0, 3) is the string is the string“ECURSION” is the string “CURSE” 4 6 1 3 8 0 2 5 7 Hint Use +

  24. A String of recursive methods returns the number of A’s in the String s int countAs(String s)

  25. “Quiz” Names: returns a string that is the same as s, except without any A’s String removeAs(String s) { } returns true if s is in alphabetical order, false otherwise (ties OK) boolean isAlph(String s) { } Extra: what’s a six-letter English word for which isAlph returns true?

  26. “Quiz” A 5.1 4.2 7.7 42 1.7 page 2 A[0] A[1] A[2] A[3] A[4] write recursive code to return the minimum value in the array A between indices L and U (inclusive). min(A,0,3) should return4.2 double min(double[] A, int L, int U) {

  27. Recursion can do anything! returns a string that is the same as s, except without any A’s String removeAs(String s) removeAs(“GATTACA”) returns “GTTC” returns true if s is in alphabetical order, false otherwise (ties OK) boolean isAlph(String s) isAlph(“BART”) returns false isAlph(“LOOPY”) returns false isAlph(“BERT”) returns true

  28. raises b to the N power double power(double b, int N) adds 1/1 + 1/2 + … + 1/N double harmonic(int N) true if s is a palindrome; false otherwise boolean isPalindrome(String s) returns the reversal of s String reverse(String s) void moveMinToLeft(double[] A, int L, int U) swaps elements so that the smallest element between index L and U is at location L sorts A between index L and U (inclusive) void sort(double[] A, int L, int U) Problem 1 Write recursive methods for double factorial(int N)

  29. Problem 1 Create a menu that allows a user to call each method: Welcome to the recursive roster! Options: (1) factorial (2) power (3) harmonic series (4) palindrome checker (5) string reverser (6) min mover (7) list sorter (8) longest common subsequence [Extra] (9) quit each of these will be a recursive method in the CS5App class

  30. Lab Today Take advantage of us! Extra credit: a biological application Given two strings, what is a longest common subsequence? public static String LCS(String s1, String s2) s1 = “hamburger” s1 = “GATTTCAAGTGAC” s2 = “cheeseburger” s2 = “CTTAGACATAGGT” LCS(s1,s2) returns “hburger” LCS(s1,s2) returns “TTCAAGG”

  31. Testing N Ply ‘X’ ‘O’ 4 6 1 3 0 2 5 With a 4-ply lookahead, X will think everything looks equivalent in this case With a 5-ply lookahead, X will know to move to column 3

  32. Recursion Mantra Let recursion do the work for you.

  33. Natural scenes mountain terrain seascapes the 2d algorithm...

  34. Recursive Data Can data contain other data of the same type? class Wart { int idNumber; Wart neighbor; } Wart wally int idNumber Wart neighbor

  35. wally.neighbor .neighbor.neighbor wally.neighbor .neighbor wally wally.neighbor int int int int idNumber idNumber idNumber idNumber Wart Wart Wart Wart neighbor neighbor neighbor neighbor wally.neighbor .neighbor.neighbor wally.neighbor .neighbor wally wally.neighbor int int int int idNumber idNumber idNumber idNumber Wart Wart Wart Wart neighbor neighbor neighbor neighbor

  36. Recursive Data Can data contain other data of the same type? class Wart { int idNumber; Wart neighbor; } Wart wally int idNumber Wart neighbor wally.neighbor .neighbor.neighbor wally.neighbor .neighbor wally wally.neighbor int int int int idNumber idNumber idNumber idNumber Wart Wart Wart Wart neighbor neighbor neighbor neighbor

  37. Ridiculously short code! Using the ? : operator... double factorial(int n) { return n<2 ? 1 : n*factorial(n-1); } double sum(double[] arr, int L, int H) { return L>H ? 0.0 : arr[L]+sum(arr,L+1,H); }

  38. addMove ‘X’ new‘X’ ‘O’ Class: Board Object: b changes b by adding checker ‘X’ into row 3 b.addMove(3,‘X’) b before b after

  39. Self-referential data ? A film database application: class FilmDB int count Film[] films[0] films[1] Film[] films films class Film String rating String title double review Director int year Director dir dir class Director FilmDB filmDB String fname FilmDB filmDB String lname

  40. (Non) deterministic recursion Deterministic recursion Koch snowflake Recursion with randomness

  41. Recursion can do anything! returns a string that is the same as s, except without any A’s String removeAs(String s) returns true if s is in alphabetical order, false otherwise boolean isAlph(String s)

  42. s = “GATTACA”; double sum(double[] arr, int L, int H) { return L>H ? 0.0 : arr[L]+sum(arr,L+1,H); } No base case -- the calls to factorial will never stop! Make sure you have a base case ! When writing recursive code Check that the base case is correct !! As with mathematical induction, base cases are critical...

  43. Computer Science 5 • Looking back / Looking ahead Week 12 (this week): Recursion Week 14: Software engineering + algorithms Week 15: Is there science in CS ? • Connect Four Results... • Midterm 2 Results... • HW 12 (2 problems + E.C.) due Sunday, 11/24 at midnight M/T sections due Monday, 11/25 at midnight W/Th sections Recitation for HW12 -- Friday 8:00am Reading: Week 12 re: recursion

  44. Recursion -- not just numbers Self-similarity elsewhere... Natural phenomena Relationships What is an “ancestor” ? Names -- acronyms EMACS EINE ZWEI all stem!

  45. Extra credit: N-ply search Board Evaluation advantage: black advantage: red Depth of Search 1-ply search 2-ply search 3-ply search N-ply search chooseMove1Ply chooseMove2Ply chooseMove3Ply evaluate (N-1) - ply ... 2 - ply 1 - ply int chooseMove(Board b, char ox, int ply)

  46. Problem 2 / Final Project CS5App (for main) class name data member data member type

More Related