Skip to content

Instantly share code, notes, and snippets.

@rioshen
Last active August 29, 2015 14:01
Show Gist options
  • Save rioshen/88e0af3faf4088cb5302 to your computer and use it in GitHub Desktop.
Save rioshen/88e0af3faf4088cb5302 to your computer and use it in GitHub Desktop.
CodingBat.com, java solutions.
/**
* The parameter weekday is true if it is a weekday, and the parameter vacation
* is true if we are on vacation. We sleep in if it is not a weekday or we're
* on vacation. Return true if we sleep in.
*/
public boolean sleepIn(boolean weekday, boolean vacation) {
return !weekday || vacation;
}
/**
* We have two monkeys, a and b, and the parameters aSmile and bSmile indicate
* if each is smiling. We are in trouble if they are both smiling or if neither of
* them is smiling. Return true if we are in trouble.
*/
public boolean monkeyTrouble(boolean aSmile, boolean bSmile) {
return aSmile == bSmile;
}
/**
* Given two int values, return their sum. Unless the two values are the same
* then return double their sum.
*/
public int sumDouble(int a, int b) {
return (a == b) ? 2 * (a + b) : (a + b);
}
/**
* Given an int n, return the absolute difference between n and 21,
* except return double the absolute difference if n is over 21.
*/
public int diff21(int n) {
return n > 21 ? 2 * Math.abs(n - 21) : Math.abs(n - 21);
}
/**
* The "hour" parameter is the current hour time in the range 0..23.
* We are in trouble if the parrot is talking and the hour is before 7
* or after 20. Return true if we are in trouble.
*/
public boolean parrotTrouble(boolean talking, int hour) {
return talking && (hour < 7 || hour > 20);
}
/**
* Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.
*/
public boolean makes10(int a, int b) {
return a == 10 || b == 10 || a + b == 10;
}
/**
* Given an int n, return true if it is within 10 of 100 or 200.
*/
public boolean nearHundred(int n) {
return Math.abs(n - 100) <= 10 || Math.abs(n - 200) <= 10;
}
/**
* Given 2 int values, return true if one is negative and one is positive.
* Except if the parameter "negative" is true, then return true only if both are negative.
*/
public boolean posNeg(int a, int b, boolean negative) {
return negative ? a < 0 && b < 0 : a * b < 0;
}
/**
* Given a string, return a new string where "not " has been added to the front.
* However, if the string already begins with "not", return the string unchanged.
*/
public String notString(String str) {
return str.indexOf("not") == 0 ? str : "not " + str;
}
/**
* Given a non-empty string and an int n, return a new string where the char at
* index n has been removed.The value of n will be a valid index of a char in
* the original string.
*/
public String missingChar(String str, int n) {
String result = "";
for (int i = 0; i < str.length(); i++) {
if (i != n) {
result += str.charAt(i);
}
}
return result;
}
/**
* Given a string, return a new string where the first and last chars have been exchanged.
*/
public String frontBack(String str) {
if (str == null || str.length() <= 1) return str;
int length = str.length();
char[] content = new char[length];
content[0] = str.charAt(length - 1);
content[length - 1] = str.charAt(0);
for (int i = 1; i < length - 1; i++) content[i] = str.charAt(i);
return new String(content);
}
/**
* Given a string, we'll say that the front is the first 3 chars of the string.
* If the string length is less than 3, the front is whatever is there.
* Return a new string which is 3 copies of the front.
*/
public String front3(String str) {
String front = "";
if (str.length() > 3) {
front = str.substring(0, 3);
} else {
front = str;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 3; i++) {
sb.append(front);
}
return sb.toString();
}
/**
* Given a string, take the last char and return a new string with the last
* char added at the front and back, so "cat" yields "tcatt".
* The original string will be length 1 or more.
*/
public String backAround(String str) {
StringBuilder builder = new StringBuilder();
char last = str.charAt(str.length() - 1);
builder.append(last);
for (int i = 0; i < str.length(); i++) {
builder.append(str.charAt(i));
}
builder.append(last);
return builder.toString();
}
/**
* Return true if the given non-negative number is a multiple of 3 or a multiple of 5.
*/
public boolean or35(int n) {
return n % 3 == 0 || n % 5 == 0;
}
/**
* Given a string, take the first 2 chars and return the string
* with the 2 chars added at both the front and back
*/
public String front22(String str) {
String result, fronts = "";
if (str.length() <= 2) {
fronts = str;
} else {
fronts = str.substring(0, 2);
}
result = fronts + str + fronts;
return result;
}
/**
* Given a string, return true if the string starts with "hi" and false otherwise.
*/
public boolean startHi(String str) {
return str.indexOf("hi") == 0;
}
/**
* Given two temperatures, return true if one is less than 0 and the other is greater than 100.
*/
public boolean icyHot(int temp1, int temp2) {
return temp1 * temp2 < -100;
}
/**
* Given 2 int values, return true if either of them is in the range 10..20 inclusive.
*/
public boolean in1020(int a, int b) {
return (a >= 10 && a <= 20) || (b >= 10 && b <= 20);
}
/**
* We'll say that a number is "teen" if it is in the range 13..19 inclusive.
* Given 3 int values, return true if 1 or more of them are teen.
*/
public boolean hasTeen(int a, int b, int c) {
return (a >= 13 && a <= 19)
|| (b >= 13 && b <= 19)
|| (c >= 13 && c <= 19);
}
/**
* We'll say that a number is "teen" if it is in the range 13..19 inclusive.
* Given 2 int values, return true if one or the other is teen, but not both.
*/
public boolean loneTeen(int a, int b) {
return ((a >= 13 && a <= 19) && (b < 13 || b > 19))
|| ((a < 13 || a > 19) && (b >= 13 && b <= 19));
}
public String stringTimes(String str, int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) sb.append(str);
return sb.toString();
}
public String frontTimes(String str, int n) {
String front = "";
if (str.length() < 3) {
front = str;
} else {
front = str.substring(0, 3);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) sb.append(front);
return sb.toString();
}
public String helloName(String name) {
return "Hello " + name + "!";
}
public String makeAbba(String a, String b) {
return a + b + b + a;
}
public static int factorial(int n) {
return n == 0 ? 1 : factorial(n - 1) * n;
}
public static int bunnyEars(int bunnies) {
return bunnies == 0 ? 0 : bunnyEars(bunnies - 1) + 2;
}
public static int fibonacci(int n) {
return n <= 1 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
public static int bunnyEars2(int bunnies) {
if (bunnies == 0) return 0;
return bunnies % 2 != 0 ? bunnyEars2(bunnies - 1) + 2 : bunnyEars2(bunnies - 1) + 3;
}
public static int sumDigits(int n) {
return n < 10 ? n : sumDigits(n / 10) + n % 10;
}
public static int count7(int n) {
if (n == 0) return 0;
return n % 10 == 7 ? 1 + count7(n / 10) : count7(n / 10);
}
public static int countX(String str) {
if (str.length() == 0) return 0;
return str.charAt(0) == 'x' ? 1 + countX(str.substring(1)) : countX(str.substring(1));
}
public static int countHi(String str) {
if (str.length() < 2) return 0;
return str.substring(0, 2).equals("hi") ?
1 + countHi(str.substring(2)) : countHi(str.substring(1));
}
public static String changeXY(String str) {
if (str.length() == 0) return str;
return str.charAt(0) == 'x' ? "y" + changeXY(str.substring(1)) : str.charAt(0) + changeXY(str.substring(1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment