Added a function to get all permutations of a
string
This commit is contained in:
Binary file not shown.
@@ -1,7 +1,7 @@
|
|||||||
//Java/JavaClasses/Algorithms.java
|
//Java/JavaClasses/Algorithms.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-02-19
|
// Created: 03-02-19
|
||||||
//Modified: 03-03-19
|
//Modified: 03-24-19
|
||||||
//This class holds many algorithms that I have found it useful to keep around
|
//This class holds many algorithms that I have found it useful to keep around
|
||||||
//As such all of the functions in here are static and meant to be used as stand alone functions
|
//As such all of the functions in here are static and meant to be used as stand alone functions
|
||||||
/*
|
/*
|
||||||
@@ -729,6 +729,7 @@ public class Algorithms{
|
|||||||
//Return the product of all elements
|
//Return the product of all elements
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
|
//This function returns true if key is found in ary
|
||||||
public static Boolean isFound(ArrayList<Integer> ary, Integer key){
|
public static Boolean isFound(ArrayList<Integer> ary, Integer key){
|
||||||
//Look through every element in the array, looing for the key element
|
//Look through every element in the array, looing for the key element
|
||||||
for(Integer num : ary){
|
for(Integer num : ary){
|
||||||
@@ -762,4 +763,50 @@ public class Algorithms{
|
|||||||
//If you made it to the end of the array without finding a match return false because the element was not found
|
//If you made it to the end of the array without finding a match return false because the element was not found
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
//This is a function that creates all permutations of a string and returns a vector of those permutations.
|
||||||
|
public static ArrayList<String> getPermutations(String master){
|
||||||
|
return getPermutations(master, 0);
|
||||||
|
}
|
||||||
|
private static ArrayList<String> getPermutations(String master, Integer num){
|
||||||
|
ArrayList<String> perms = new ArrayList<String>();
|
||||||
|
//Check if the number is out of bounds
|
||||||
|
if((num >= master.length()) || (num < 0)){
|
||||||
|
//Do nothing and return an empty arraylist
|
||||||
|
}
|
||||||
|
//If this is the last possible recurse just return the current string
|
||||||
|
else if(num == (master.length() - 1)){
|
||||||
|
perms.add(master);
|
||||||
|
}
|
||||||
|
//If there are more possible recurses, recurse with the current permutation
|
||||||
|
else{
|
||||||
|
ArrayList<String> temp = new ArrayList<String>();
|
||||||
|
temp = getPermutations(master, num + 1);
|
||||||
|
perms.addAll(temp);
|
||||||
|
//You need to swap the current letter with every possible letter after it
|
||||||
|
//The ones needed to swap before will happen automatically when the function recurses
|
||||||
|
for(Integer cnt = 1;(num + cnt) < master.length();++cnt){
|
||||||
|
master = swapString(master, num, (num + cnt));
|
||||||
|
temp = getPermutations(master, num + 1);
|
||||||
|
perms.addAll(temp);
|
||||||
|
master = swapString(master, num, (num + cnt));
|
||||||
|
}
|
||||||
|
|
||||||
|
//The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning
|
||||||
|
if(num == 0){
|
||||||
|
Collections.sort(perms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Return the arraylist that was built
|
||||||
|
return perms;
|
||||||
|
}
|
||||||
|
public static String swapString(String str, Integer first, Integer second){
|
||||||
|
char[] tempStr = str.toCharArray();
|
||||||
|
char temp = tempStr[first];
|
||||||
|
tempStr[first] = tempStr[second];
|
||||||
|
tempStr[second] = temp;
|
||||||
|
|
||||||
|
String swappedString = new String(tempStr);
|
||||||
|
return swappedString;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//Java/JavaClasses/testAlgorithms.java
|
//Java/JavaClasses/testAlgorithms.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-02-19
|
// Created: 03-02-19
|
||||||
//Modified: 03-02-19
|
//Modified: 03-24-19
|
||||||
//This program runs tests on all function in the Algorithms library
|
//This program runs tests on all function in the Algorithms library
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2019 Matthew Ellison
|
Copyright (C) 2019 Matthew Ellison
|
||||||
@@ -88,6 +88,11 @@ public class testAlgorithms{
|
|||||||
timer.stop();
|
timer.stop();
|
||||||
System.out.println("It took " + timer.getStr() + " to run this test\n");
|
System.out.println("It took " + timer.getStr() + " to run this test\n");
|
||||||
|
|
||||||
|
timer.start();
|
||||||
|
testGetPermutations();
|
||||||
|
timer.stop();
|
||||||
|
System.out.println("It took " + timer.getStr() + " to run this test\n");
|
||||||
|
|
||||||
//Print a closing message
|
//Print a closing message
|
||||||
System.out.println("Tests completed");
|
System.out.println("Tests completed");
|
||||||
}
|
}
|
||||||
@@ -571,4 +576,56 @@ public class testAlgorithms{
|
|||||||
System.out.println("isFound passed all tests");
|
System.out.println("isFound passed all tests");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//This function tests the getPermutations function
|
||||||
|
private static void testGetPermutations(){
|
||||||
|
Boolean failed = false;
|
||||||
|
//Test 1
|
||||||
|
String permString = "012";
|
||||||
|
ArrayList<String> correctAnswer = new ArrayList<String>(Arrays.asList("012", "021", "102", "120", "201", "210"));
|
||||||
|
ArrayList<String> answer = Algorithms.getPermutations(permString);
|
||||||
|
if(!answer.equals(correctAnswer)){
|
||||||
|
System.out.println("getPermutations failed the first test");
|
||||||
|
System.out.println(answer.toString());
|
||||||
|
failed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Print a message if all of the tests passed
|
||||||
|
if(!failed){
|
||||||
|
System.out.println("getPermutations passed all tests");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Results:
|
||||||
|
getPrimes passed all tests
|
||||||
|
It took 5.209 milliseconds to run this test
|
||||||
|
|
||||||
|
getNumPrimes passed all tests
|
||||||
|
It took 1.464 milliseconds to run this test
|
||||||
|
|
||||||
|
getFactors passed all tests
|
||||||
|
It took 456.699 microseconds to run this test
|
||||||
|
|
||||||
|
getDivisors passed all tests
|
||||||
|
It took 428.300 microseconds to run this test
|
||||||
|
|
||||||
|
getFib passed all tests
|
||||||
|
It took 6.107 milliseconds to run this test
|
||||||
|
|
||||||
|
getAllFib passed all tests
|
||||||
|
It took 708.100 microseconds to run this test
|
||||||
|
|
||||||
|
getSum passed all tests
|
||||||
|
It took 1.736 milliseconds to run this test
|
||||||
|
|
||||||
|
getProd passed all tests
|
||||||
|
It took 541.600 microseconds to run this test
|
||||||
|
|
||||||
|
isFound passed all tests
|
||||||
|
It took 537.700 microseconds to run this test
|
||||||
|
|
||||||
|
getPermutations passed all tests
|
||||||
|
It took 336.500 microseconds to run this test
|
||||||
|
|
||||||
|
Tests completed
|
||||||
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user