Skip to content

Instantly share code, notes, and snippets.

@Drhealsgood
Created April 22, 2012 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drhealsgood/2462017 to your computer and use it in GitHub Desktop.
Save Drhealsgood/2462017 to your computer and use it in GitHub Desktop.
Tutorials
package anagram;
import java.io.*;
import java.util.Set;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
public class AnagramFinder {
public static void wordArranger(String dict_location) {
// Generates the alphabetically sorted format for each word in the dictionary
// and store options in a Multimap. Ignores multiple entries.
// Outputs anagrams of words found within dictionary.
Multimap<String, String> words = ArrayListMultimap.create();
// precondition: We have a sorted file of words
// Add words to a multimap with key being sorted word
try {
BufferedReader word_list = new BufferedReader(new FileReader(dict_location));
// Loop invariant:
// Check if word has an anagram within the dictionary
while(word_list.ready()){
// trim each word and set to lower case
String word = word_list.readLine().trim().toLowerCase();
// sort word alphabetically
char[] chars = word.toCharArray();
java.util.Arrays.sort(chars);
// convert word back to string
String sorted_word = new String(chars);
// add key and value to map
words.put(sorted_word, word);
}
// we have a formatted set of words as keys in a multimap with orig_words as values
// output values associated with each key
// postcondition: An output of each word in dictionary that can be made up of a set of letters.
// provides a set of all the keys
Set<String> keys = words.keySet();
Object[] word_keys = keys.toArray();
java.util.Arrays.sort(word_keys);
for (int i=0; i<word_keys.length; i++){
String word_next = (String) word_keys[i];
System.out.print(word_next);
System.out.println(": " + words.get(word_next));
}
} catch (IOException ex) {
// do nufing.
}
} // end method
public static void main(String[] args){
wordArranger("C:\\words");
}
} // end class
// EDITS: Fixed some commenting errors and spelling mistakes.
package luke.mckee;
import java.util.*;
public class Knapsack {
// items, values, and weights
// String[] items = {"Beer", "Food", "Books", "Music"};
// int[] values = {30, 14, 16, 9};
// int[] weights = {6, 3, 4, 2};
String[] items;
int[] values;
int[] weights;
public void nextItem(int weight) {
// weight is amount user can carry.
// items, values, and weights are expected to have same size
if ((items.length == values.length) && (items.length == weights.length)) {
} else {
// throw exception
}
}
/*
* method to find highest value within values and return its index
*/
public int highestValue(int[] value_poss, int weight) {
int highest_val =0;
int index =0;
// if there is an array
if (value_poss.length > 0) {
// if value_poss has one number return it
if (value_poss.length == 1) {
return index;
} else {
// find highest value in value_poss, that is less than weight
for (int i=0; i<value_poss.length; i++) {
if ((value_poss[i] > highest_val)&&(weights[i]<weight)) {
index = i;
} else {
// set value to zero as it cannot be contained anymore.
}
}
// return index
return index;
}
}
// I think this should probably throw an out of bounds exception
return index;
}
/*
* gets and sets for items
*/
public void setItems(String[] items_new) {
this.items = items_new;
}
public String[] getItems() {
return items;
}
/*
* gets and sets for values
*/
public void setValues(int[] values_new) {
this.values = values_new;
}
public int[] getValues() {
return values;
}
/*
* get and sets for weights
*/
public void setWeights(int[] weights_new) {
this.weights = weights_new;
}
public int[] getWeights() {
return weights;
}
}
package lab6;
import java.util.*;
import java.io.*;
public class NearestPair {
public static double[][] sort_array(double[][] points) {
// method to sort array into non-descending order of x values
if (points.length<2){
return points;
}
// loop over rows
for (int i=0; i<points.length; i++){
// set a smallest value
double[] position = new double[2];
double[] values_curr = points[i];
double[] temp = new double[2];
// loop over rows from current pos
for (int j=0; j<points.length; j++) {
position = points[j];
// swap x and pos if pos is less than x\
if ((values_curr[0]>position[0])) {
temp = values_curr;
values_curr = points[j];
points[j] = temp;
}
}
}
return points;
}
public static void main (String[] args) {
double[][] array = { {1,1}, {2,2}, {5,5}, {10,10}, {3,3} };
sort_array(array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment