Our Mathematical Selection code in C#.
Mathematical Selection Class File:
using System.Collections.Generic;
namespace Miscellaneous_CS
{
class Selection
{
public string[] words;
public int r; // min length of word
protected List<string[]> complete_group;
private int i;
public Selection()
{
}
public List<string[]> groupSelection(string[] candidates, int size) {
words = candidates;
r = size;
complete_group = new List<string[]>();
i = 0;
recursiveFillUp(new List<string>());
return complete_group;
}
// pick elements recursively
protected void recursiveFillUp(List<string> temp) {
List<string>[] picked_elements = new List<string>[words.Length];
int j = i;
while (j < words.Length) {
picked_elements[j] = new List<string>();
picked_elements[j].AddRange(temp);
picked_elements[j].Add(words[j]);
// recoil factor
if (i >= words.Length) {
i = j;
}
// satisfied yet?
if (picked_elements[j].Count == r) {
complete_group.Add(picked_elements[j].ToArray());
}
else if (picked_elements[j].Count < r) {
recursiveFillUp(picked_elements[j]);
}
j++;
}
if (picked_elements[--j] != null && picked_elements[j].Count == r) {
i++; // keep recoil factor straightened out
}
}
}
}
Main Class:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Miscellaneous_CS
{
class Program
{
static void Main(string[] args)
{
string[] goods = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
Selection pick = new Selection();
List<string[]> result = pick.groupSelection(goods, 3);
// print choices and operation
Console.Write("[ ");
foreach (string choice in pick.words)
{
Console.Write(choice + "; ");
}
Console.WriteLine("] selection " + pick.r + ":" + Environment.NewLine);
// print out selections nicely
int i = 0;
foreach (string[] set in result)
{
Console.Write(++i + ": ");
foreach (string member in set)
{
Console.Write(member + "; ");
}
Console.WriteLine();
}
Console.WriteLine(Environment.NewLine + "Number of ways is " + result.Count + ".");
}
}
}
Try it out!
Elegance (0.0)