Last active
March 10, 2018 12:54
-
-
Save feload/d5286b0b2ba594b04fc592c6a8168451 to your computer and use it in GitHub Desktop.
CSharp Permutator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class MainClass { | |
public static void Main (string[] args) { | |
// Credits https://www.codeproject.com/Articles/37215/Permutations-in-C-Using-Recursion | |
Console.Write("Input String>"); | |
string inputLine = Console.ReadLine(); | |
Recursion rec = new Recursion(); | |
rec.InputSet = rec.MakeStringArray(inputLine); | |
rec.CalcPermutation(0); | |
Console.Write("# of Permutations: " + rec.PermutationCount); | |
} | |
} | |
class Recursion | |
{ | |
private int elementLevel = -1; | |
private int numberOfElements; | |
private int[] permutationValue = new int[0]; | |
private string[] inputSet; | |
public string[] InputSet | |
{ | |
get { return inputSet; } | |
set { inputSet = value; } | |
} | |
private int permutationCount = 0; | |
public int PermutationCount | |
{ | |
get { return permutationCount; } | |
set { permutationCount = value; } | |
} | |
public string[] MakeStringArray(string InputString) | |
{ | |
string[] charString = InputString.Split(' '); | |
Array.Resize(ref permutationValue, charString.Length); | |
numberOfElements = charString.Length; | |
return charString; | |
} | |
public void CalcPermutation(int k) | |
{ | |
elementLevel++; | |
permutationValue.SetValue(elementLevel, k); | |
if (elementLevel == numberOfElements) | |
{ | |
OutputPermutation(permutationValue); | |
} | |
else | |
{ | |
for (int i = 0; i < numberOfElements; i++) | |
{ | |
if (permutationValue[i] == 0) | |
{ | |
CalcPermutation(i); | |
} | |
} | |
} | |
elementLevel--; | |
permutationValue.SetValue(0, k); | |
} | |
private void OutputPermutation(int[] value) | |
{ | |
foreach (int i in value) | |
{ | |
Console.Write(inputSet.GetValue(i - 1)); | |
} | |
Console.WriteLine(); | |
PermutationCount++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment