Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
908 views

CodingBat Answers

The document contains code snippets for various string and logic methods. It includes methods to manipulate strings like returning substrings, checking for matching start/end characters, and removing duplicate adjacent characters. It also includes logic methods for things like determining if numbers sum to another number, checking speeds and birthdays, and summing numbers with constraints. Many methods use recursion to break down and solve problems by reducing them to sub-problems.

Uploaded by

blueb3rryi3
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
908 views

CodingBat Answers

The document contains code snippets for various string and logic methods. It includes methods to manipulate strings like returning substrings, checking for matching start/end characters, and removing duplicate adjacent characters. It also includes logic methods for things like determining if numbers sum to another number, checking speeds and birthdays, and summing numbers with constraints. Many methods use recursion to break down and solve problems by reducing them to sub-problems.

Uploaded by

blueb3rryi3
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Warmup 2 - stringTimes

public String stringTimes(String str, int n) { String copy = ""; for (int i=0; i<n; i++) { copy = copy + str; } return copy; }

String 1 - middleThree
public String middleThree(String str) { return str.substring((str.length() / 2) - 1, (str.length() / 2) + 2); }

String 1 - frontAgain
blic boolean frontAgain(String str) { if(str.length() >= 2 && str.substring(0, 2).equals(str.substring(str.length() - 2, str.length()))) { return true; } else { return false; } }

String 1 - withoutEnd2
public String withouEnd2(String str) { if(str.length() <= 2) { return ""; } else { return str.substring(1, str.length() - 1); } }

Logic 1 - twoAsOne
public boolean twoAsOne(int a, int b, int c) { if(a + b == c) { return true; } else if(b + c == a) { return true; } else if(c + a == b) { return true; } else { return false; } }

Logic 1 - sortaSum
public int sortaSum(int a, int b) { if(a + b >= 10 && a + b <= 19) { return 20; } else { return a + b; } }

Logic 1 - answerCell
public boolean answerCell(boolean isMorning, boolean isMom, boolean isAsleep) {

if(isAsleep){ return false; } if(isMorning && isMom){ return true; } if(isMorning && !isMom){ return false; } if(!isMorning){ return true; }

else{ return true; } }

Logic 1 - caughtSpeeding
public int caughtSpeeding(int speed, boolean isBirthday) {

if(!isBirthday && speed <= 60){ return 0; } if(!isBirthday && speed >= 61 && speed <= 80){ return 1; } if(!isBirthday && speed >= 81){ return 2; } if(isBirthday && speed <= 65){ return 0; } if(isBirthday && speed >= 66 && speed <= 85){ return 1; } if(isBirthday && speed >= 86){ return 2; } else{

return 0;} }

Logic 1 - love6
public boolean love6(int a, int b) { if(a == 6 || b == 6 || a + b == 6 || Math.abs(a - b) == 6) { return true; } else { return false; } }

Logic 1 - maxMod5
public int maxMod5(int a, int b) { if ( a == b) return 0; if ( a % 5 == b % 5 ) { if ( a > b) return b; else return a; } if ( a > b) return a; else return b;

Logic 1 - redTicket
public int redTicket(int a, int b, int c) { if(a + b + c == 6){ return 10; } if(a == b && a == c && b == c){ return 5; } if(a != b && a != c){ return 1; } else{ return 0; } }

Logic 1 - sumLimit
public int sumLimit(int a, int b) { if(String.valueOf(a + b).length() != String.valueOf(a).length()) { return a; } else { return a + b; } }

String 2 - sameStarChar
public boolean sameStarChar(String str) { boolean check = true; for(int i = 0; i < str.length(); i++) { if(i != str.length() - 1 && Character.toString(str.charAt(i)).equals("*")) { if(i != 0 && Character.toString(str.charAt(i - 1)).equals(Character.toString(str.charAt(i + 1)))) { } else if(i == 0) { } else { return false; } } } return true; }

Recursion 1 - count7
public int count7(int n) { int result = 0; if(n == 0) { return 0; }

if(n % 10 == 7) { result++; } result += count7(n / 10); return result;

Recursion 1 - pairStar
public String pairStar(String str) { String result = ""; if(str.length() <= 1) { return str; } if(str.charAt(0) == str.charAt(1)) { result += (Character.toString(str.charAt(0)) + "*"); } else { result += Character.toString(str.charAt(0)); } result += pairStar(str.substring(1, str.length())); return result; }

Recursion 1 - countHi2
public int countHi2(String str) { int result = 0; if(str.length() < 2) { return 0; } else if(str.length() == 2) { if(str.equals("hi")) { return 1; } else { return 0; } } else { if(str.charAt(0) == 'x') { if(str.length() <= 3) {

return 0; } else if(str.substring(1, 3).equals("hi")) { str = str.substring(3, str.length()); } else { str = str.substring(1, str.length()); } } else { if(str.substring(0, 2).equals("hi")) { result++; str = str.substring(2, str.length()); } else { str = str.substring(1, str.length()); } } } result += countHi2(str); return result; }

Recursion 1 - triangle
public int triangle(int rows) { if(rows == 0) { return 0; } return rows + triangle(rows - 1); }

Recursion 1 - sumDigits
public int sumDigits(int n) { if(n < 10) { return n; } return (n % 10) + sumDigits(n / 10); }

Recursion 1 - stringClean
public String stringClean(String str) { if(str.length() <= 1) { return str; } String result = "", c = Character.toString(str.charAt(0)), n = ""; do { if(n.equals(c)) { str = str.substring(1, str.length()); if(str.length() <= 1) { break; } } n = Character.toString(str.charAt(1)); } while(n.equals(c) && str.length() > 1); if(str.length() < 1) { } else { str = str.substring(1, str.length()); }

result += c; result += stringClean(str); return result; }

Logic 2 - noTeenSum
public int noTeenSum(int a, int b, int c) { return fixTeen(a) + fixTeen(b) + fixTeen(c); } public int fixTeen(int n) { if(n >= 13 && n <= 19 && n != 15 && n != 16) { return 0; } else { return n; } }

Logic 2 - luckySum
public int luckySum(int a, int b, int c) { if(a==13){ return 0; } if(b==13){ return a; } if(c==13){ return a+b;

} else{ return a+b+c; } }

You might also like