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

Post on 18-Jan-2016

217 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Comparing code

JAVABATWRAPUP

Solutions seemed cleaner for team than individual assignment.

Refactor – clean up code without changing functionality

GENERAL NOTE

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; }

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;}

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;}

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; }

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

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); }

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; }

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

TRIPLE_UP

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

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

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

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)

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

top related