Presentation is loading. Please wait.

Presentation is loading. Please wait.

Demo Exam 2.

Similar presentations


Presentation on theme: "Demo Exam 2."— Presentation transcript:

1 Demo Exam 2

2 Find the Palindrome (moed alef, last year)

3

4 Solution section 1 public static boolean isPalindrome(Node head) {
int length = listLength(head); for (int i = 0; i < length / 2; i++) { if (!identical(i, head, length)) { return false; } return true; Notice the top-down design. It also secure some points.

5 You must explain your code, however javadoc style is not required.
/** * Check to see if the value in the given location of the list is identical * to the value at location length-location-1. (If it is not then we don't * have a palindrome) */ private static boolean identical(int location, Node head, int length) { int i; // our current location in the list. int value; // will hold the value at location i. // travel up to the given location in the list: for (i = 0; i < location; i++) { head = head.getNext(); } /* * (previous part could be made more efficient if we don't travel from * the beginning of the list every time) value = head.getData(); for (; i < length - location - 1; i++) { return head.getData() == value; You must explain your code, however javadoc style is not required.

6 /** * Find the length of the given list. */ public static int listLength(Node head) { int length = 0; while (head != null) { head = head.getNext(); length++; } return length;

7

8 public static boolean isPalindrome(Node head) {
int length = listLength(head); if (length==1){ return true; } Node current = head; //have current travel to before the mid point: for(int i=0; i<length/2-1; i++){ current = current.getNext(); //get the second list Node secondList = current.getNext(); // cut off the first list from the second part current.setNext(null); //reverse the second part Node reversedList = reverseList(secondList); //and compare the two parts. return compareLists(head,reversedList);

9 private static boolean compareLists(Node firstList, Node secondList) {
/** * Compares two lists until the first list (which is assumed to be * shorter) ends. returns true if they are the same. */ private static boolean compareLists(Node firstList, Node secondList) { while(firstList != null){ if (firstList.getData()!= secondList.getData()){ return false; } firstList = firstList.getNext(); secondList = secondList.getNext(); return true; Note that firstList is never longer than secondList

10 /** * Reverses the given list, and returns its new head. */ private static Node reverseList(Node head) { Node current = head; Node newTail = null; while (current.getNext()!=null){ Node temp = current.getNext(); current.setNext(newTail); newTail = current; current = temp; } return current;

11 moed b

12 public static int ladder(int n){ if (n==1) return 1; if (n==2)
return ladder(n-1)+ladder(n-2); } And this reminds us of … So the bound is… O(?)

13 moed a

14 1 3 2 6 1 5 4 3 2 15 10 6 35 20 70 1 4 3 2 10 6 20

15 Version 1 public static int countPaths(int n){ return countPaths(n,n);
} public static int countPaths(int m, int n){ if (m==2) return n; if (n==2) return m; return countPaths(m-1, n)+countPaths(m, n-1);

16 Version 2 (explicit backtracking)
public static int countPaths2(int n){ return countPaths2(n,n); } public static int countPaths2(int m, int n){ if (m==2) return n; if ( n==2) return m; int counts = 0; m = m-1; counts+=countPaths2(m, n); m = m+1; n = n-1; return counts;

17 moed a

18

19 Many ways to describe extramum
public class Extramum { //(row, col) the coordinates of the begining of the extramum //size - the length of the extramum sequence public int _row, _col, _size; boolean _horizintal; //these members are public for the ease of writing //for better encapsulation they should be declared private //and be accessed with appropriate getters. public Extramum (int row, int col, int size, boolean h){ _row = row; _col =col; _size = size; _horizintal = h; } public void printExtramum(){ System.out.print("r: "+_row+ " c: "+_col+" size: "+ _size); if (_horizintal) System.out.println(" horizontal"); else System.out.println(" vertical");

20 public class Finder { private int[][] _a; private int _size; public Finder(){} public Extramum findExtramum(int[][] a, int size){} private boolean checkExtramum(Extramum e){} private boolean checkMinimum(Extramum e){} private boolean checkMaximum(Extramum e){} private int findMin(Extramum e){} private int findMax(Extramum e){} private void printMatrix(){} }

21 Exhaustive search for a beginning of an extramum
public Extramum findExtramum(int[][] a, int size){ _a = a; _size = size; int rows = a.length; int cols = a[0].length; for (int i = 0; i<rows; i++){ for (int j = 0; j<cols; j++){ //checking for horizintal extramum starting at i,j if (cols-j > size){ Extramum e = new Extramum(i,j,size, true); if (checkExtramum(e)) return e; } //checking for vertical extramum starting at i,j if (rows -i > size){ Extramum e = new Extramum(i,j, size, false); return null; Exhaustive search for a beginning of an extramum Do we have enough room for the desired size of the extramum?

22 private boolean checkExtramum(Extramum e){
return ( checkMaximum(e) || checkMinimum(e)); }

23 private boolean checkMinimum(Extramum e){
int max = findMax(e); if (e._horizintal){ for (int j = e._col-1; j<=e._col+_size; j++){ if (e._row-1>=0 && j>=0 && j<_a[0].length) if (max>=_a[e._row-1][j]) return false; if (e._row+1<_a.length && j>=0 && j<_a[0].length){ if (max>=_a[e._row+1][j]) } //checking the other two spots if (e._col-1>=0) if(max>=_a[e._row][e._col-1]) if (e._col+_size<_a[0].length) if(max>=_a[e._row][e._col+_size]) else{ //extramum is vertical for (int i = e._row-1; i<e._row+_size; i++){ if (e._col-1>=0 && i>=0 && i<_a.length) if (max >= _a[i][e._col-1]) if (e._col+1<_a[0].length && i>=0 && i<_a.length) if (max >= _a[i][e._col+1]) //NEED TO CHECK THE TWO OTHER CASES HERE return true;

24 Zooming in to the previous slide
if (e._horizintal){ for (int j = e._col-1; j<=e._col+_size; j++){ if (e._row-1>=0 && j>=0 && j<_a[0].length) if (max>=_a[e._row-1][j]) return false; if (e._row+1<_a.length && j>=0 && j<_a[0].length){ if (max>=_a[e._row+1][j]) } //checking the other two spots if (e._col-1>=0) if(max>=_a[e._row][e._col-1]) if (e._col+_size<_a[0].length) if(max>=_a[e._row][e._col+_size])

25 else{ //extramum is vertical
for (int i = e._row-1; i<e._row+_size; i++){ if (e._col-1>=0 && i>=0 && i<_a.length) if (max >= _a[i][e._col-1]) return false; if (e._col+1<_a[0].length && i>=0 && i<_a.length) if (max >= _a[i][e._col+1]) } //NEED TO CHECK THE TWO OTHER CASES HERE

26 private boolean checkMinimum(Extramum e){
int max = findMax(e); if (e._horizintal){ for (int j = e._col-1; j<=e._col+_size; j++){ if (e._row-1>=0 && j>=0 && j<_a[0].length) if (max>=_a[e._row-1][j]) return false; if (e._row+1<_a.length && j>=0 && j<_a[0].length){ if (max>=_a[e._row+1][j]) } //checking the other two spots if (e._col-1>=0) if(max>=_a[e._row][e._col-1]) if (e._col+_size<_a[0].length) if(max>=_a[e._row][e._col+_size]) else{ //extramum is vertical for (int i = e._row-1; i<e._row+_size; i++){ if (e._col-1>=0 && i>=0 && i<_a.length) if (max >= _a[i][e._col-1]) if (e._col+1<_a[0].length && i>=0 && i<_a.length) if (max >= _a[i][e._col+1]) //NEED TO CHECK THE TWO OTHER CASES HERE return true;

27 //returns the maximal value in the extramum sequence
private int findMax(Extramum e){ int max = _a[e._row][ e._col]; if (e._horizintal){ for (int j = e._col; j<e._col+_size; j++){ if (_a[e._row][j]>max) max = _a[e._row][j]; } else{ //the extramum is vertical for (int i = e._row; i<e._row+_size; i++){ if (_a[i][e._col]>max) max = _a[i][e._col]; return max;

28 private boolean checkMaximum(Extramum e)
is essentially the same as checkMinimum(). We first find: int min = findMin(e); And comparisons are: min<= instead of max>= private int findMin(Extramum e) is essentially the same as findMax…


Download ppt "Demo Exam 2."

Similar presentations


Ads by Google