Skip to content

Instantly share code, notes, and snippets.

@bhnascar
Last active February 13, 2017 06:32
Show Gist options
  • Save bhnascar/69b8bef09622d9117aa61973ebb4bd52 to your computer and use it in GitHub Desktop.
Save bhnascar/69b8bef09622d9117aa61973ebb4bd52 to your computer and use it in GitHub Desktop.
More arraylist practice problems
import java.util.*;
public class MoreArrayList
{
// Think!
//
// What are the repeatable units of work?
// This is what goes in the for loop.
//
// If you see the keyword "every" or "for every X do Y"
// there's probably a loop involved.
//
// What are the subproblems? If X is hard to do,
// would it be easier if you already have Y? If yes, then
// solving Y first is a subproblem.
public static void normalize(ArrayList<Double> input) {
// Normalize the input:
// This means scale all the values in the input
// so that they fit from 0 to 1 (this means dividing
// each element by the max element).
}
public static void shuffle(ArrayList<Integer> numbers) {
// Rearrange the items in numbers so that they are in a
// random order.
}
public static void removeDuplicates(ArrayList<String> words) {
// Remove all duplicate Strings that appear in |words|.
// This means that if a word shows up twice, remove
// the second appearance of that word from |words|. If it
// shows up more than twice, remove ALL duplicate appearances
// of that word from |words|.
//
// Be careful of going out of bounds!
}
public static int findLongestIncreasingSequence(ArrayList<Integer> numbers) {
// Okay, so first think: How can you break this problem
// down into easier sub-problems? (Hint: What if you had an
// ArrayList of ArrayLists....woahhh, so meta).
return 0;
}
// Next question is very very similar to what you did last week!
private static class Item {
int value;
int count;
}
public static ArrayList<Item> buildHistogram(ArrayList<Integer> numbers) {
// Build a histogram of how many times a number appears in
// numbers.
//
// For each unique number that appears in numbers,
// create an "Item" object where |value| is that number and
// |count| is the number of times that number is scene in numbers.
return null;
}
public static ArrayList<Integer> simpleSort(ArrayList<Integer> input)
{
// Returns a sorted version of the input.
// Use the following strategy:
// Take each int in input one at a time and
// insert them into a new ArrayList that is
// originally empty. Every time you add a new
// int, you add it into the right place so that
// the new ArrayList remains sorted.
return null;
}
// Challenge question:
public static void insertionSort(ArrayList<Integer> input)
{
// A fancier version of simple sort. Use a SINGLE ArrayList.
// Destructively modify the ArrayList that was given to you
// instead of creating a new sorted ArrayList.
//
// The solution to this problem is known as insertion sort.
// (We'll revisit this later in the searching and sorting
// chapter).
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment