Skip to content

Instantly share code, notes, and snippets.

@trplll
Created May 31, 2017 16:47
Show Gist options
  • Save trplll/0f6410f0661163f81bfc9ff22010a60e to your computer and use it in GitHub Desktop.
Save trplll/0f6410f0661163f81bfc9ff22010a60e to your computer and use it in GitHub Desktop.
//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 ? true: false;
}
//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 ? Math.abs(21-n)*2 :21-n;
}
//Given an int n, return true if it is within 10 of 100 or 200. Note: Math.abs(num) computes the absolute value of a number.
public boolean nearHundred(int n) {
int targetTolerance=10;
int[] targetNumbers= new int[2];
targetNumbers[0]=100;
targetNumbers[1]=200;
for (int o : targetNumbers){
if (Math.abs(o-n)<=targetTolerance) {
return true;
}
}
return false;
}
//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 (i.e. n will be in the range 0..str.length()-1 inclusive).
public String missingChar(String str, int n) {
StringBuilder StringLessCharAtIndex= new StringBuilder();
for(int i=0; i<str.length(); i++){
if(i!=n){
StringLessCharAtIndex.append(str.charAt(i)) ;
}
}
return StringLessCharAtIndex.toString();
}
public String missingChar(String str, int n) {
String front = str.substring(0, n);
String back = str.substring(n+1, str.length());
return front + back;
}
//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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment