Download presentation
Presentation is loading. Please wait.
Published byJanis Gordon Modified over 9 years ago
1
Comparing code JAVABAT WRAPUP
2
Solutions seemed cleaner for team than individual assignment. Refactor – clean up code without changing functionality GENERAL NOTE
3
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; }
4
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; }
5
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; }
6
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; }
7
A sandwich is two pieces of bread with something in between. Return the string that is between the first and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread. GET_SANDWICH
8
Clean syntax… probably reasonably efficient 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); }
9
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; }
10
Return true if the array contains, somewhere, three increasing adjacent numbers like.... 4, 5, 6,... or 23, 24, 25. TRIPLE_UP
11
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++){ if (nums[i] == nums[i-1] + 1) { counter++; if (counter == 2) return true; } else { counter = 0; } return false; } TRIPLE UP
12
Given an array of scores sorted in increasing order, return true if the array contains 3 adjacent scores that differ from each other by at most 2, such as with {3, 4, 5} or {3, 5, 5}. SCORES CLUMP
13
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
14
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)
15
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.