Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Generates partial permutations for a given string:
- private HashSet<string> GeneratePartialPermutation(string word)
- {
- return new HashSet<string>(Enumerable.Range(0, word.Length)
- .Select(i => word.Remove(i, 1).Insert(0, word[i].ToString())));
- }
- // Entry point for the recipe execution:
- void Main()
- {
- // Word with all characters to compute its power set:
- string word = "abc";
- // Create a set with the n-initial permutations:
- HashSet<string> perms = GeneratePartialPermutation(word);
- // Produces all the element pairs for each partial permutation
- // (e.g., for the word 'abc' there are three element pairs
- // {"a", "bc"}, {"ab", "c"}, {"abc"}:
- Enumerable.Range(0, word.Length)
- .ToList()
- .ForEach(x =>
- Enumerable.Range(0, word.Length)
- .ToList()
- .ForEach(z =>
- {
- // First element pair:
- perms.Add(perms.ElementAt(x).Substring(0, z));
- // Second element pair:
- perms.Add(perms.ElementAt(x).Substring(z+1));
- }));
- // Sorts and removes duplicates from all the generated element pairs:
- perms.Select(p => new string(p.ToCharArray()
- .OrderBy(x => x)
- .ToArray()))
- .Distinct()
- .OrderBy(p => p.Length)
- .Dump("Power set of 'abc'");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement