Skip to content

Instantly share code, notes, and snippets.

@bhnascar
Created March 22, 2016 06:59
Show Gist options
  • Save bhnascar/e3da46f9ecc98c600fd3 to your computer and use it in GitHub Desktop.
Save bhnascar/e3da46f9ecc98c600fd3 to your computer and use it in GitHub Desktop.
Solutions to ArrayList practice questions
import java.util.ArrayList;
public class Solutions
{
public static void main(String[] args) {
// An example list of words to test against.
ArrayList<String> words = new ArrayList<String>(10);
words.add("foo");
words.add("bar");
words.add("baz");
words.add("slinky");
words.add("dinky");
words.add("blinky");
words.add("bleh");
words.add("meh");
words.add("heh");
// Test solutions to questions below.
System.out.println("Sup? Good luck on your exam. Hope these practice questions help. :)");
System.out.println("Given the list of words: foo bar baz slinky dinky blinky bleh meh heh");
// Test removeEveryOther.
ArrayList<String> result = new ArrayList<String>(words);
removeEveryOther(result);
System.out.println(
"\nRemoving every other word should give you: [foo, baz, dinky, bleh, heh]\n"
+ "Your answer is: " + result
);
assert result.toString().equals("[foo, baz, dinky, bleh, heh]");
// Test removeEh.
result = new ArrayList<String>(words);
removeEh(result);
System.out.println(
"\nRemoving words that end with \"eh\" give you: [foo, bar, baz, slinky, dinky, blinky]\n"
+ "Your answer is: " + result
);
assert result.toString().equals("[foo, bar, baz, slinky, dinky, blinky]");
// Test removeWordsWithSuffix.
result = new ArrayList<String>(words);
removeWordsWithSuffix(result, "linky");
System.out.println(
"\nRemoving words that end with \"linky\" give you: [foo, bar, baz, dinky, bleh, meh, heh]\n"
+ "Your answer is: " + result
);
assert result.toString().equals("[foo, bar, baz, dinky, bleh, meh, heh]");
// Test removeInky.
result = new ArrayList<String>(words);
removeInky(result);
System.out.println(
"\nRemoving words that end with \"inky\" should give you: [foo, bar, baz, bleh, meh, heh]\n"
+ "Your answer is: " + result
);
assert result.toString().equals("[foo, bar, baz, bleh, meh, heh]");
// Test shortestString.
result = new ArrayList<String>(words);
String shortestString = shortest(result);
System.out.println(
"\nThe shortest string should be \"heh\" (if you break ties by taking the one closest to the end).\n"
+ "Your answer is: " + shortestString
);
assert shortestString.equals("heh");
// Test toCaps.
result = new ArrayList<String>(words);
toCaps(result);
System.out.println(
"\nIf you convert everything to caps you should have: [FOO, BAR, BAZ, SLINKY, DINKY, BLINKY, BLEH, MEH, HEH]\n"
+ "Your answer is: " + result
);
assert result.toString().equals("[FOO, BAR, BAZ, SLINKY, DINKY, BLINKY, BLEH, MEH, HEH]");
}
/* Removes every other word from the given list
* of words. For example, if the input consists of
* 10 words, remove the second, fourth, sixth, eigth,
* and tenth words.
*
* @param words - an ArrayList of Strings
*/
public static void removeEveryOther(ArrayList<String> words) {
for (int i = 1; i < words.size(); i += 2) {
words.remove(i);
i--;
}
// Alternate solution no. 1 (taking advantage of the fact that an index is skipped)
/*
for (int i = 1; i < words.size(); i++) {
words.remove(i);
}
*/
}
/* Removes every word that ends with the suffix "eh".
*
* @param words - an ArrayList of Strings
*/
public static void removeEh(ArrayList<String> words) {
for (int i = 1; i < words.size(); i++) {
if (words.get(i).length() < 2) {
continue;
}
int indexOfSecondToLastCharacter = words.get(i).length() - 2;
String substringFromLastTwoCharacters = words.get(i).substring(indexOfSecondToLastCharacter);
if (substringFromLastTwoCharacters.equals("eh")) {
words.remove(i);
i--;
}
}
/*
// Alternative solution no. 1 (not using variables)
for (int i = 1; i < words.size(); i++) {
if (words.get(i).length() < 2) {
continue;
}
if (words.get(i).substring(words.get(i).length() - 2).equals("eh")) {
words.remove(i);
i--;
}
}
*/
/*
// Alternative solution no. 2 (using the endsWith method - if you know this method exists)
for (int i = 1; i < words.size(); i++) {
if (words.get(i).endsWith("eh")) {
words.remove(i);
i--;
}
}
*/
}
/* Removes every word that ends with the given suffix.
* (this is really similar to the previous question).
*
* @param words - an ArrayList of Strings
* @param suffix - the suffix. Remove words that end with this suffix.
*/
public static void removeWordsWithSuffix(ArrayList<String> words, String suffix) {
for (int i = 1; i < words.size(); i++) {
if (words.get(i).length() < suffix.length()) {
continue;
}
int indexOfSuffixStart = words.get(i).length() - suffix.length();
String suffixOfCurrentWord = words.get(i).substring(indexOfSuffixStart);
if (suffixOfCurrentWord.equals(suffix)) {
words.remove(i);
i--;
}
}
/*
// Alternative solution no. 1 (not using variables)
for (int i = 1; i < words.size(); i++) {
if (words.get(i).length() < suffix.length()) {
continue;
}
if (words.get(i).substring(words.get(i).length() - suffix.length()).equals(suffix)) {
words.remove(i);
i--;
}
}
*/
// Alternative solution no. 2 (using the endsWith method - if you know this method exists)
/*
for (int i = 1; i < words.size(); i++) {
if (words.get(i).endsWith(suffix)) {
words.remove(i);
i--;
}
}
*/
}
/* Removes every word that ends with the suffix "inky".
* How can you use the previous method (removeWordsWithSuffix)
* to implement the solution for this one?
*
* @param words - an ArrayList of Strings
*/
public static void removeInky(ArrayList<String> words) {
removeWordsWithSuffix(words, "inky");
}
/* Finds the shortest word (where you compare words by the number
* of characters) in the given list of words.
*
* If there is a tie, then take the one that occurs LATER in the
* list.
*
* @param words - an ArrayList of Strings
*/
public static String shortest(ArrayList<String> words) {
String shortestSoFar = null;
for (String word : words) {
if (shortestSoFar == null || word.length() <= shortestSoFar.length()) {
shortestSoFar = word;
}
}
return shortestSoFar;
// Alternative solution - using the other kind of for-loop
/*
String shortestSoFar = null;
for (int i = 0; i < words.size(); i++) {
String word = words.get(i);
if (shortestSoFar == null || word.length() <= shortestSoFar.length()) {
shortestSoFar = word;
}
}
return shortestSoFar;
*/
}
/* Converts every word in the given list of words to caps.
* You can assume in a word contains only alphabetic characters.
*
* @param words - an ArrayList of Strings
*/
public static void toCaps(ArrayList<String> words) {
// Alternative solution no .1 (using toUpperCase method - if you know this method exists)
for (int i = 0; i < words.size(); i++) {
String currentWord = words.get(i);
char[] charactersOfCurrentWord = currentWord.toCharArray();
char[] charactersOfFixedWord = new char[currentWord.length()]; // We'll fill this up.
for (int j = 0; j < charactersOfCurrentWord.length; j++) {
charactersOfFixedWord[j] = (char)(charactersOfCurrentWord[j] + ('A' - 'a'));
}
String fixedWord = new String(charactersOfFixedWord);
words.set(i, fixedWord);
}
/*
// Alternative solution no .1 (using toUpperCase method - if you know this method exists)
for (int i = 0; i < words.size(); i++) {
words.set(i, words.get(i).toUpperCase());
}
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment