Added a function to get all permutations of a

string
This commit is contained in:
2019-03-24 15:43:22 -04:00
parent 8f4d8664f4
commit 8fe685f7e0
3 changed files with 107 additions and 3 deletions

View File

@@ -1,7 +1,7 @@
//Java/JavaClasses/testAlgorithms.java
//Matthew Ellison
// Created: 03-02-19
//Modified: 03-02-19
//Modified: 03-24-19
//This program runs tests on all function in the Algorithms library
/*
Copyright (C) 2019 Matthew Ellison
@@ -88,6 +88,11 @@ public class testAlgorithms{
timer.stop();
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
System.out.println("Tests completed");
}
@@ -571,4 +576,56 @@ public class testAlgorithms{
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
*/