Moved getPermutations to the correct file

This commit is contained in:
2021-07-05 01:36:06 -04:00
parent dd676bcdc8
commit 41cb497943
4 changed files with 52 additions and 52 deletions

View File

@@ -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<string> GetPermutations(string master){
return GetPermutations(master, 0);
}
public static List<string> GetPermutations(string master, int num){
List<string> perms = new List<string>();
//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<string> 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);
}
}
}

View File

@@ -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<string> GetPermutations(string master){
return GetPermutations(master, 0);
}
public static List<string> GetPermutations(string master, int num){
List<string> perms = new List<string>();
//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<string> 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);
}

View File

@@ -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<string> correctAnswer = new List<string>(){"012", "021", "102", "120", "201", "210"};
List<string> answer = mee.ArrayAlgorithms.GetPermutations(permString);
CollectionAssert.AreEqual(correctAnswer, answer, "GetPermutations failed");
}
}
}

View File

@@ -28,6 +28,14 @@ using System.Collections.Generic;
namespace TestStringAlgorithms{
[TestClass]
public class TestStringAlgorithms{
[TestMethod]
public void TestGetPermutations(){
//Test 1
string permString = "012";
List<string> correctAnswer = new List<string>(){"012", "021", "102", "120", "201", "210"};
List<string> answer = mee.StringAlgorithms.GetPermutations(permString);
CollectionAssert.AreEqual(correctAnswer, answer, "GetPermutations failed");
}
[TestMethod]
public void TestFindNumOccurrence(){
//Test 1