From 8fe685f7e01c85cfb712aa4abf947506d940f090 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Sun, 24 Mar 2019 15:43:22 -0400 Subject: [PATCH] Added a function to get all permutations of a string --- mattrixwv/Algorithms.class | Bin 8918 -> 9922 bytes mattrixwv/Algorithms.java | 49 ++++++++++++++++++++++++++++- testAlgorithms.java | 61 +++++++++++++++++++++++++++++++++++-- 3 files changed, 107 insertions(+), 3 deletions(-) diff --git a/mattrixwv/Algorithms.class b/mattrixwv/Algorithms.class index 3ef991280dc1219d25ab47ed6b279a05d3cf71dd..b3432af5747dedf3d7d0bb3c36472e72b5583a72 100644 GIT binary patch delta 2923 zcmaJ@dvH|M8ULMo?&gxqW%JlKahB1;4<#(=eKNer>e2Ezi< zDnX@ySQWj#MZp)Sl!8SQ8hp3b`s#n}Or3EoolgI7`WT(*=&XtM`_8?|?ucn7^PPLn z`Tc(1<9zqr{d33LtB2nIX7qIc^YM|1CL|0Dn9z_g(TuGoEZoFqkk_`ENMbvi9VS|^ zlhz((qFz{mjHIxYa}eyFH}Ke;Ut*CT7yW9upzl#^&}Rws)}EYvAWR?lW-- z_Os}2emcPBPTn}kYj^R}FWB4NZ0?zkwYZnfA>O!;!S|bJgw4AT@bp2RK4jow9v?CA zsDZ}>vKI9x`jf5VzEAq~#|<1lbA@nVBtkJPvL}xU*e=dUb+G+6UpAb-a!k$V&*BF zHt>>#U*n90m+=adh6TK7d~_hu$Ezb2&f+&LKgz!Js6c`Jl3qZ8oI}*2_FTZGmCa=q zUgfpd@VbRJaLmG+IBDT6ylvsPIBnr~c*nx;8Pbc*7T)Dh{($!^+=!o8cpo1KW2|44(P7aYBnxyTp4FeaX%A5bNn_i^m0mZgsaO`{Rx6 z1Mzt8CT@wAn)L$HlLPIWV}osjgRxx#dUef)b^%ZIhW46k5T=%FQm;ih1Ua2c_EP(l zS?Hfft64MyVX`@6>flT5kIfTep1p5+*Mfy=XWBRd1~dcBV=#r9w1~PCy!k2UDR{7m zU>X)H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zu>myTB4&CgbA zSi=j-Qn8jr>vY_Mdv*A*-cDTpQ+c&vYGu=KpN=5}>XOHpS09V`0VQ@Nnv4z7{e(|`5Z5o~!IoDtEq=upKHOgm-@{}>#R%hR? zAW?68RZ?a2+XBkd#!g$avSa+Dt=VR*a71h~aiKBj@Lw>DodUbCTVNR70?%MX!?OYg zo)dVU!!TYDcoBPyQ;w?YQBK^8eHva8*pHV5UcsxJ^P2IS!|ff$0fB>fo%M$pmR8^~ z235}-fj5vAXu@m`Ne=A-hxyqU-V}HXBLYXTN8oL|Bk(Tv2^__H0>_XZ-&^{T+Knj9 zrBTi|2nFPyVuIEox+-+FH+%@?7~LqCMRA&Z7)2=;rO73Xv*L&{%h+Ak*Vat$qi{^Z zr$N=wG67wYi&kZ32F}V1N;0tHB1%(nu|y;#BJBK}5Z^odIv5dWgntU*(%_p!SVODQ zLXdI^ahi}e32B#*jpfG)(p+JIR9hfH=_Dwf%(I=l z_a>WI5J1V_h{3K_ni`$?LtO182z#L^kC)7Y)zEasCu zV3~WDY4t$WAj#vtq|$6G_tjdwrOXpD?Bkv3@h?np0UxGKfOb`_oP=AJ5FN1$eH{wt zB4s)|>E|hI4OoPvP~B5P-`sdX$Y&vCdddd)OdhVb2)!iqPNGypkp2@SoNn3wdFZBt zoiJ)^wvq5qOas^u~ zFDPc|+Po#du`1R|qS1n=n5`wIturyURUgk+DQ0Uau9JOih z{T4CxEM} ary, Integer key){ //Look through every element in the array, looing for the key element 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 return false; } + //This is a function that creates all permutations of a string and returns a vector of those permutations. + public static ArrayList getPermutations(String master){ + return getPermutations(master, 0); + } + private static ArrayList getPermutations(String master, Integer num){ + ArrayList perms = new ArrayList(); + //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 temp = new ArrayList(); + 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; + } } diff --git a/testAlgorithms.java b/testAlgorithms.java index 45ee1bc..5f7d9f6 100644 --- a/testAlgorithms.java +++ b/testAlgorithms.java @@ -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"); } } -} \ No newline at end of file + //This function tests the getPermutations function + private static void testGetPermutations(){ + Boolean failed = false; + //Test 1 + String permString = "012"; + ArrayList correctAnswer = new ArrayList(Arrays.asList("012", "021", "102", "120", "201", "210")); + ArrayList 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 +*/