diff --git a/CSClasses/ArrayAlgorithms.cs b/CSClasses/ArrayAlgorithms.cs index 9fac4bc..2e4337a 100644 --- a/CSClasses/ArrayAlgorithms.cs +++ b/CSClasses/ArrayAlgorithms.cs @@ -78,48 +78,5 @@ namespace mee{ } return prod; } - //This is a function the creates all permutations of a string and returns a list of those permutations - public static List GetPermutations(string master){ - return GetPermutations(master, 0); - } - public static List GetPermutations(string master, int num){ - List perms = new List(); - //Check if the number is out of bounds - if((num >= master.Length) || (num < 0)){ - //Do nothing and return an empty list - } - //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, the recurse with the current permutation - else{ - List temp = GetPermutations(master, num + 1); - perms.AddRange(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(int cnt = 1;(num + cnt) < master.Length;++cnt){ - master = SwapString(master, num, (num + cnt)); - temp = GetPermutations(master, num + 1); - perms.AddRange(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){ - perms.Sort(); - } - } - - //Return the list that was build - return perms; - } - public static string SwapString(string str, int first, int second){ - char[] tempStr = str.ToCharArray(); - char temp = str[first]; - tempStr[first] = tempStr[second]; - tempStr[second] = temp; - return new string(tempStr); - } } } \ No newline at end of file diff --git a/CSClasses/StringAlgorithms.cs b/CSClasses/StringAlgorithms.cs index c826326..acaf256 100644 --- a/CSClasses/StringAlgorithms.cs +++ b/CSClasses/StringAlgorithms.cs @@ -28,7 +28,50 @@ using System.Linq; namespace mee{ public class StringAlgorithms{ - //This function return sht enumber of times the character occurs in the string + //This is a function the creates all permutations of a string and returns a list of those permutations + public static List GetPermutations(string master){ + return GetPermutations(master, 0); + } + public static List GetPermutations(string master, int num){ + List perms = new List(); + //Check if the number is out of bounds + if((num >= master.Length) || (num < 0)){ + //Do nothing and return an empty list + } + //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, the recurse with the current permutation + else{ + List temp = GetPermutations(master, num + 1); + perms.AddRange(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(int cnt = 1;(num + cnt) < master.Length;++cnt){ + master = SwapString(master, num, (num + cnt)); + temp = GetPermutations(master, num + 1); + perms.AddRange(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){ + perms.Sort(); + } + } + + //Return the list that was build + return perms; + } + public static string SwapString(string str, int first, int second){ + char[] tempStr = str.ToCharArray(); + char temp = str[first]; + tempStr[first] = tempStr[second]; + tempStr[second] = temp; + return new string(tempStr); + } + //This function returns the number of times the character occurs in the string public static long FindNumOccurrence(string str, char c){ return str.Count(ch => ch == c); } diff --git a/TestCSClasses/TestArrayAlgorithms.cs b/TestCSClasses/TestArrayAlgorithms.cs index 2cedc06..734e7b3 100644 --- a/TestCSClasses/TestArrayAlgorithms.cs +++ b/TestCSClasses/TestArrayAlgorithms.cs @@ -79,13 +79,5 @@ namespace TestCSClasses{ BigInteger bigAnswer = mee.ArrayAlgorithms.GetProd(bigNumbers); Assert.AreEqual(bigCorrectAnswer, bigAnswer, "GetProd BigInteger failed"); } - [TestMethod] - public void TestGetPermutations(){ - //Test 1 - string permString = "012"; - List correctAnswer = new List(){"012", "021", "102", "120", "201", "210"}; - List answer = mee.ArrayAlgorithms.GetPermutations(permString); - CollectionAssert.AreEqual(correctAnswer, answer, "GetPermutations failed"); - } } } \ No newline at end of file diff --git a/TestCSClasses/TestStringAlgorithms.cs b/TestCSClasses/TestStringAlgorithms.cs index ab83cd2..f0b342b 100644 --- a/TestCSClasses/TestStringAlgorithms.cs +++ b/TestCSClasses/TestStringAlgorithms.cs @@ -28,6 +28,14 @@ using System.Collections.Generic; namespace TestStringAlgorithms{ [TestClass] public class TestStringAlgorithms{ + [TestMethod] + public void TestGetPermutations(){ + //Test 1 + string permString = "012"; + List correctAnswer = new List(){"012", "021", "102", "120", "201", "210"}; + List answer = mee.StringAlgorithms.GetPermutations(permString); + CollectionAssert.AreEqual(correctAnswer, answer, "GetPermutations failed"); + } [TestMethod] public void TestFindNumOccurrence(){ //Test 1