comparing code javabat wrapup. solutions seemed cleaner for team than individual assignment. ...

15
Comparing code JAVABAT WRAPUP

Upload: janis-gordon

Post on 18-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

Comparing code

JAVABATWRAPUP

Page 2: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

Solutions seemed cleaner for team than individual assignment.

Refactor – clean up code without changing functionality

GENERAL NOTE

Page 3: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

a String is xy-balanced if for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string

XYBALANCE – NO LOOP

public boolean xyBalance(String str) { int tempX = -1; int tempY = -1; tempX = str.lastIndexOf('x'); tempY = str.lastIndexOf('y'); if (tempX > tempY) { return false; } else { return true; }

Page 4: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

Many similar to the following

XYBALANCE – LOOP PLUS STUFF

public boolean xyBalance(String str) { int lastY = -1; int lastX = -1; //find the last x; make sure there is at least one y after it for (int i = 0; i < str.length(); i++){ if (str.charAt(i) == 'x') lastX = i; else if (str.charAt(i) == 'y') lastY = i; else continue; } if (lastY > lastX) return true; else if (lastY == -1 && lastX == -1) return true; else if ((lastY == -1 && lastX != -1) || (lastX == -1 && lastY != -1)) return false; else return false;}

Page 5: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

XYBALANCE – SINGLE LOOP

public boolean xyBalance(String str) { boolean isClear = true; for(int i = 0; i < str.length(); i++){ if(str.charAt(i) == 'x'){ isClear = false; } else if(str.charAt(i) == 'y'){ isClear = true; } } return isClear;}

Page 6: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

XYBALANCE – NESTED LOOP

public boolean xyBalance(String str) { int xpos=0; boolean check=true; for( int i=0; i<str.length(); i++) {

if (str.charAt(i)=='x') { xpos=i; check=false; for(int j=xpos+1; j<str.length();j++)

{ if(str.charAt(j)=='y') check=true; }

} } return check; }

Page 7: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

A sandwich is two pieces of bread with something in between. Return the string that is between the fi rst and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread.

GET_SANDWICH

Page 8: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

Clean syntax… probably reasonably effi cient

USING JAVA FUNCTIONALITY

public String getSandwich(String str) {

final String BREAD = "bread";

int start = str.indexOf(BREAD) + BREAD.length(); int end = str.lastIndexOf(BREAD); if (end <= start) return ""; return str.substring(start, end); }

Page 9: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

Some students wrote their own loops instead of indexOf/ lastIndexOf. Generally harder to read.

MAGIC NUMBER

public String getSandwich(String str) { int first = str.indexOf("bread"); if(first == -1){ return ""; } String second = str.substring(first+5); int third = second.lastIndexOf("bread"); if(third == -1){ return ""; } String fourth = second.substring(0,third); return fourth; }

Page 10: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

Return true if the array contains, somewhere, three increasing adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25.

TRIPLE_UP

Page 11: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

public boolean tripleUp(int[] nums) { for(int i = 0; i < nums.length - 2; i++){ if(nums[i+1] == nums[i] + 1 && nums[i+2] == nums[i+1] + 1){ return true; } } return false;}

public boolean tripleUp(int[] nums) { int counter = 0; for(int i=1; i < nums.length; i++){ i f (nums[i] == nums[i-1] + 1) { counter++; i f (counter == 2) return true; } else { counter = 0; } } return false;}

TRIPLE UP

Page 12: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

Given an array of scores sorted in increasing order, return true if the array contains 3 adjacent scores that diff er from each other by at most 2, such as with {3, 4, 5} or {3, 5, 5}.

SCORES CLUMP

Page 13: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

public boolean scoresClump(int[] scores) { for(int i = 0; i < scores.length-2; i++){ if(scores[i+2]-scores[i]<=2) return true; } return false;}

MAKE USE OF WHAT YOU KNOW ABOUT THE PROBLEM

Page 14: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

public boolean scoresClump(int[] scores) { if(scores.length < 3){return false;} int x = scores[0]; int y = scores[1]; int z = scores[2]; for(int i = 0; i < scores.length ; i++){ if(i+2 < scores.length){ x = scores[i]; y = scores[i +1]; z = scores[i +2]; } else {return false;} if((Math.abs(x - y) <= 2) && (Math.abs(x - z) <=2) ){ return true; } } return false;}

EXTRA VARIABLES (NOT ALWAYS BAD)

Page 15: Comparing code JAVABAT WRAPUP.  Solutions seemed cleaner for team than individual assignment.  Refactor – clean up code without changing functionality

public boolean scoresClump(int[] scores) { for(int i = 0; i < scores.length - 2; i++) { if(Math.abs(scores[i] - scores[i+1]) <= 2 && Math.abs(scores[i] - scores[i+2]) <= 2 && Math.abs(scores[i+1] - scores[i+2]) <= 2)

return true; } return false;}

OK IF DON’T TRUST INPUT DATA