|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Combinations
{
class Program
{
static void Main(string[] args)
{
string line, s;
int count;
do
{
Console.WriteLine("Select Demo:");
Console.WriteLine("1 - Combinations over integers.");
Console.WriteLine("2 - Combinations over a string.");
Console.WriteLine("3 - Combinations and permutations.");
Console.WriteLine("q - Quit.");
line = Console.ReadLine();
if (line == "q") break;
switch (line)
{
case "1":
CombinationsOverIntegers();
break;
case "2":
if (!InputParameters(out s, out count))
continue;
CombinationsOverString(s, count);
break;
case "3":
if (!InputParameters(out s, out count))
continue;
CombinationsPermutations(s, count);
break;
}
} while (line != "q");
}
private static bool InputParameters(out string s, out int count)
{
Console.WriteLine("Enter string:");
s = Console.ReadLine();
Console.WriteLine("How may characters to choose?");
if (!int.TryParse(Console.ReadLine(), out count)) return false;
return true;
}
private static void CombinationsPermutations(string s, int count)
{
foreach (var comb in Iterator.Combinations(s.ToCharArray().ToList(), count))
{
foreach (var perm in Iterator.Permutations(comb, count))
{
string r = new string(perm.Take(count).ToArray());
Console.Write("{0,-8}", r);
}
}
Console.WriteLine();
}
private static void CombinationsOverString(string s, int count)
{
foreach (var comb in Iterator.Combinations(s.ToCharArray().ToList(), count))
{
string r = new string(comb.Take(count).ToArray());
Console.Write("{0,-8}", r);
}
Console.WriteLine();
}
private static void CombinationsOverIntegers()
{
List<int> l = new List<int>() { 1, 2, 3, 4, 5, 6 };
int count = 4;
foreach (var comb in Iterator.Combinations(l, count))
{
string s = comb.Take(count).Aggregate<int, string>("", (x, y) => x + y);
Console.Write("{0,-8}", s);
}
Console.WriteLine();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.