mirror of
https://bitbucket.org/Mattrixwv/csclasses.git
synced 2025-12-06 18:23:58 -05:00
Moved getPermutations to the correct file
This commit is contained in:
@@ -78,48 +78,5 @@ namespace mee{
|
|||||||
}
|
}
|
||||||
return prod;
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,50 @@ using System.Linq;
|
|||||||
|
|
||||||
namespace mee{
|
namespace mee{
|
||||||
public class StringAlgorithms{
|
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){
|
public static long FindNumOccurrence(string str, char c){
|
||||||
return str.Count(ch => ch == c);
|
return str.Count(ch => ch == c);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,13 +79,5 @@ namespace TestCSClasses{
|
|||||||
BigInteger bigAnswer = mee.ArrayAlgorithms.GetProd(bigNumbers);
|
BigInteger bigAnswer = mee.ArrayAlgorithms.GetProd(bigNumbers);
|
||||||
Assert.AreEqual(bigCorrectAnswer, bigAnswer, "GetProd BigInteger failed");
|
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -28,6 +28,14 @@ using System.Collections.Generic;
|
|||||||
namespace TestStringAlgorithms{
|
namespace TestStringAlgorithms{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
public class TestStringAlgorithms{
|
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]
|
[TestMethod]
|
||||||
public void TestFindNumOccurrence(){
|
public void TestFindNumOccurrence(){
|
||||||
//Test 1
|
//Test 1
|
||||||
|
|||||||
Reference in New Issue
Block a user