Created
May 19, 2015 06:41
-
-
Save kulicuu/ad01c5e61c6e9b186c7e to your computer and use it in GitHub Desktop.
permutation thing in java did this some years ago
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This | |
*/ | |
import java.util.*; | |
public class StringHandler2{ | |
//public ArrayList<String> arrayPurms = new ArrayList<String>(); | |
public static ArrayList<String> SetPermute(HashSet <String> resultSet){ | |
System.out.println("We're in SetPermute. With argument " + resultSet.toString()); | |
ArrayList <String> arrayPurms = new ArrayList <String> (); | |
if (resultSet.size() == 1) { | |
Iterator <String> itr5 = resultSet.iterator(); | |
arrayPurms.add(itr5.next()); | |
return arrayPurms; | |
} | |
Iterator <String> itr = resultSet.iterator(); | |
while (itr.hasNext()){ | |
HashSet <String> newSet = new HashSet <String>(); | |
newSet =(HashSet <String>)resultSet.clone(); | |
String cursor = itr.next(); | |
System.out.println("cursor= " + cursor); | |
newSet.remove(cursor); | |
//Iterator <String> itr2 = SetPermute(newSet).arrayPurms.iterator(); | |
ArrayList <String> arrayPurmsB = SetPermute(newSet); | |
Iterator <String> itr2 = arrayPurmsB.iterator(); | |
ArrayList <String> arrayPurms2 = new ArrayList <String> (); | |
while(itr2.hasNext()){ | |
String cursor2 = itr2.next(); | |
System.out.println("cursor 2 = " + cursor2); | |
arrayPurms2.add(cursor+cursor2); | |
} | |
Iterator <String> itr3 = arrayPurms2.iterator(); | |
while(itr3.hasNext()){ | |
String cursor3 = itr3.next(); | |
System.out.println("cursor3 = " + cursor3); | |
arrayPurms.add(cursor3); | |
} | |
} | |
return arrayPurms; | |
} | |
public static void main(String args[]) { | |
if (args.length == 0 ) { System.out.println(" No string provided, try again."); System.exit(0); }//end if | |
System.out.println("This is initialisation. Hi, this is StringHandler2."); String s = args[0]; | |
//Split the string and send to a char Array and then to a HashSet from the charArray | |
char[] charArray = s.toCharArray(); HashSet <String> resultSet = new HashSet<String>(); | |
for ( int i= 0; i< charArray.length; i++) resultSet.add(Character.toString(charArray[i])); | |
//now some testing | |
testModule1(s, charArray, resultSet); | |
ArrayList<String> arrayPermutations = SetPermute(resultSet); | |
System.out.println("This is the size of arrayPermutations: " + arrayPermutations.size()); | |
System.out.println("It should be equal to the factorial of " + resultSet.size() + "."); | |
Iterator <String> itr4 = arrayPermutations.iterator(); | |
int m = 1; | |
while (itr4.hasNext()){ | |
System.out.println("This is from the string array ["+m+"]: " + itr4.next()); | |
m++; | |
} | |
}//end void main | |
// testing bs; pulling together multiple methods into a single method for call from main | |
public static void testModule1(String s, char[] charArray, HashSet resultSet){ | |
testingInitialisation(s, charArray, resultSet); | |
iteration(resultSet); | |
} | |
//some testing bs | |
public static void testingInitialisation(String s, char[] charArray, HashSet resultSet) { | |
System.out.println("We are now into testingInitialisation."); | |
System.out.println("This is the string entered: " + s); | |
for (int i = 0 ; i < charArray.length; i++) { | |
System.out.println("This is charArray["+i+"] " + charArray[i]); | |
} | |
System.out.println("this is the toString of charArray" + charArray.toString() + "which as you can see is just some object identifier, nothing interesting"); | |
System.out.println("This is resultSet: " + resultSet); | |
} | |
//test method | |
//another disposable test method | |
public static void iteration(HashSet resultSet){ | |
Iterator itr = resultSet.iterator(); | |
int i = 0; | |
while(itr.hasNext()) { | |
System.out.println("i = " + i +" and this index tags value " + itr.next()); | |
i++; | |
} // end while | |
} // end permuteee | |
}// end Class StringHandler2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment