Skip to content

Instantly share code, notes, and snippets.

@bhnascar
Created March 23, 2016 05:44
Show Gist options
  • Save bhnascar/c4f40d817a1f9bee1d48 to your computer and use it in GitHub Desktop.
Save bhnascar/c4f40d817a1f9bee1d48 to your computer and use it in GitHub Desktop.
Practice problems from today
import java.util.ArrayList;
public class Practice
{
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");
words.add("he");
removeShortestWord(words);
System.out.println(words);
System.out.println(calculateAverageLength(words));
System.out.println(isEveryWordTheSame(words));
swapFirstAndLast(words);
System.out.println(words);
swapIAndJ(words, 3, 5);
System.out.println(words);
}
/* Removes the shortest word in the given list.
*
* @param words - the ArrayList of words
*/
public static void removeShortestWord(ArrayList<String> words) {
String shortestWord = words.get(0);
int indexOfShortestWord = 0;
for (int i = 1; i < words.size(); i++) {
if (words.get(i).length() < shortestWord.length()) {
shortestWord = words.get(i);
indexOfShortestWord = i;
}
}
words.remove(indexOfShortestWord);
}
/* Calculates the average word length in the given list of
* words.
*
* @param words - the ArrayList of words
*/
public static int calculateAverageLength(ArrayList<String> words) {
int sumOfLengths = 0;
for (int i = 0; i < words.size(); i++) {
sumOfLengths += words.get(i).length();
}
return sumOfLengths / words.size();
// Alternative solution. Using a for-each loop. Remember, it's
// okay to do this because we're not (1) removing anything
// from words, (2) adding anything to words, or (3) changing
// anything inside words. Basically we're not modifying words,
// and as long as you're not modifying words you can use the
// for-each syntax.
/*
int sumOfLengths = 0;
for (String word : words) {
sumOfLengths += word.length();
}
return sumOfLengths / words.size();
*/
}
/* Returns true if every word in the given list is the same.
* Returns false otherwise.
*
* @param words - the ArrayList of words
*/
public static boolean isEveryWordTheSame(ArrayList<String> words) {
String firstWord = words.get(0);
for (int i = 0; i < words.size(); i++) {
if (!words.get(i).equals(firstWord)) {
return false;
}
}
return true;
// Alternative solution. Again, since we're not modifying
// words, we can use the for-each loop. We're only reading
// stuff from words.
/*
String firstWord = words.get(0);
for (String word : words) {
if (!word.equals(firstWord)) {
return false;
}
}
return true;
*/
}
/* Swaps the first and last words in the given list. You can
* assume there are at least two elements.
*
* @param words - the ArrayList of words
*/
public static void swapFirstAndLast(ArrayList<String> words) {
String firstWord = words.get(0);
String lastWord = words.get(words.size() - 1);
words.set(0, lastWord);
words.set(words.size() - 1, firstWord);
// Alternative solution: using the method you wrote below.
// swapIAndJ(words, 0, words.size() - 1);
}
/* Swaps the two words at index i and index j. You can assume
* i and j are valid indices (they won't be longer than the array
* or less than 0).
*
* @param words - the ArrayList of words
*/
public static void swapIAndJ(ArrayList<String> words, int i, int j) {
String firstWord = words.get(i);
String secondWord = words.get(j);
words.set(i, secondWord);
words.set(j, firstWord);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment