WarmUp Java - CodingBat.docx
WarmUp Java - CodingBat.docx
string.
//stringTimes("Hi", 2) → "HiHi"
//stringTimes("Hi", 3) → "HiHiHi"
//stringTimes("Hi", 1) → "Hi"
return temp;
}
//---------------------------------
// Given a string, return true if the first instance of "x" in the string is immediately followed by
another "x".
// doubleX("axxbb") → true
// doubleX("axaxax") → false
// doubleX("xxxxx") → true
if(str.charAt(i) == 'x'){
if(str.charAt(i+1) == 'x'){
result = true;
break;
} else {
result = false;
break;
}
} else {
result = false;
// break;
}
}
} else {
return false;
}
return result;
}
//---------------------------------
// Given a string, return the count of the number of times that a substring length 2 appears in
the string and also as the last 2 chars of the string, so "hixxxhi" yields 1 (we won't count the
end substring).
// last2("hixxhi") → 1
// last2("xaxxaxaxx") → 1
// last2("axxxaaxx") → 2
return count;
}
//---------------------------------
//Given an array of ints, return true if the sequence of numbers 1, 2, 3 appears in the array
somewhere.
//---------------------------------
Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9 ... so "kittens" yields
"kien".
altPairs("kitten") → "kien"
altPairs("Chocolate") → "Chole"
altPairs("CodingHorror") → "Congrr"
return result;
}
//---------------------------------
//---------------------------------
// Given a string and a non-negative int n, we'll say that the front of the string is the first 3
chars, or whatever is there if the string is less than length 3. Return n copies of the front;
// frontTimes("Chocolate", 2) → "ChoCho"
// frontTimes("Chocolate", 3) → "ChoChoCho"
// frontTimes("Abc", 3) → "AbcAbcAbc"
return temp;
}
//---------------------------------
// Given a string, return a new string made of every other char starting with the first, so
"Hello" yields "Hlo".
// stringBits("Hello") → "Hlo"
// stringBits("Hi") → "H"
// stringBits("Heeololeo") → "Hello"
}
return temp;
}
//---------------------------------
arrayCount9([1, 2, 9]) → 1
arrayCount9([1, 9, 9]) → 2
arrayCount9([1, 9, 9, 3, 9]) → 3
for(int i : nums){
if(i == 9){
cont++;
}
}
return cont;
}
//---------------------------------
// arrayCount9([1, 2, 9]) → 1
// arrayCount9([1, 9, 9]) → 2
// arrayCount9([1, 9, 9, 3, 9]) → 3
for(int n: nums){
if(n == 9){
count++;
}
}
return count;
}
//---------------------------------
// Given 2 strings, a and b, return the number of the positions where they contain the same
length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az"
substrings appear in the same place in both strings.
// stringMatch("xxcaazz", "xxbaaz") → 3
// stringMatch("abc", "abc") → 2
// stringMatch("abc", "axc") → 0
return count;
}
//---------------------------------
// Suppose the string "yak" is unlucky. Given a string, return a version where all the "yak" are
removed, but the "a" can be any char. The "yak" strings will not overlap.
// stringYak("yakpak") → "pak"
// stringYak("pakyak") → "pak"
// stringYak("yak123ya") → "123ya"
return result;
}
//---------------------------------
// Given an array of ints, return true if it contains a 2, 7, 1 pattern: a value, followed by the
value plus 5, followed by the value minus 1. Additionally the 271 counts even if the "1" differs
by 2 or less from the correct value.
//---------------------------------
// Count the number of "xx" in the given string. We'll say that overlapping is allowed, so "xxx"
contains 2 "xx".
// countXX("abcxx") → 1
// countXX("xxx") → 2
// countXX("xxxx") → 3
//---------------------------------
// stringSplosion("Code") → "CCoCodCode"
// stringSplosion("abc") → "aababc"
// stringSplosion("ab") → "aab"
return temp;
}
//---------------------------------
return false;
}
//---------------------------------
// Given a string, return a version where all the "x" have been removed. Except an "x" at the
very start or end should not be removed.
// stringX("xxHxix") → "xHix"
// stringX("abxxxcd") → "abcd"
// stringX("xabxxxcdx") → "xabcdx"
//---------------------------------
// Given an array of ints, return the number of times that two 6's are next to each other in the
array. Also count instances where the second "6" is actually a 7.
// array667([6, 6, 2]) → 1
// array667([6, 6, 2, 6]) → 1
// array667([6, 7, 2, 6]) → 1
return count;
}
//---------------------------------
//---------------------------------