All pastes #957593 Raw Edit

Unnamed

public text v1 · immutable
#957593 ·published 2008-03-26 01:54 UTC
rendered paste body
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;


public class Methods {

	String words[] = new String[10];
	BufferedReader bufReader = null;
	HashSet<String> wordList = null;
	
	public void getNumbers() throws IOException {
	try {
		bufReader = new BufferedReader (new FileReader("wordlist.txt"));
	}
	catch (Exception e) {
	System.out.println ("File not found.\n");
	}
	String line = "";
	while((line = bufReader.readLine()) != null) {
		wordList.add(line);
	}
	for(int i=0; i<10; i++) {
		if(i==0)
			words[i]=SavitchIn.readLine().substring(2);
		else 
			words[i]=SavitchIn.readLine().substring(3);
	}
	}
	
	ArrayList<String> permutationsOf(String s) {
    ArrayList<String> result = new ArrayList<String>();
         if (s.length() == 1) { // base case
             // return a new ArrayList containing just s
             result.add(s);
             return result;
          }
          // continued...
         else {
             // separate the first character from the rest
         char first = s.charAt(0);
         String rest = s.substring(1); 
             // get all permutationsOf the rest of the characters
         ArrayList<String> simpler = permutationsOf(rest);  // recursive step 
             // for each permutation,
         for (String permutation : simpler) { // extra work 
             // add the first character in all possible positions, and
             ArrayList additions = insertAtAllPositions(first, permutation); 
             // put each result into a new ArrayList
             result.addAll(additions);
         }
         return result;
         }
     }
     //Given one String representing one permutation of n-1 characters, we want to return all permutations resulting from inserting a given character in all n possible positions 
     private ArrayList<String> insertAtAllPositions(char ch, String s) {
         ArrayList<String> result = new ArrayList<String>();
         for (int i = 0; i <= s.length(); i++) {
             String inserted = s.substring(0, i) + ch + s.substring(i);
             result.add(inserted); 
          }
         return result; 
     }
     
     public void comparetoList() {
     for(int i=0; i<10; i++) {
    	 for(int z=0; z<(permutationsOf(words[i]).size()); z++) {
    		 if(wordList.contains((permutationsOf(words[i])).get(z))) {
    			 //What do I print out to see the unscrambled word?
    		 }
    	 }
     }
     }
     
     

 
}