usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Code for Selection with Repetitions in C#



Possible Selections With Repetition.

Imagine being given the opportunity to pick six (6) exercise books from a book-shop having 10 different brands of exercise books - Ben 10, Chelsea F.C., Superman, Tottenham F.C., Indomitables, Manchester City F.C., Spider Man, Power Rangers, Liverpool F.C. and Bat Man exercise books.
If you happen to be a big Power Rangers fan, nothing stops you from selecting all 6 exercise books to be Power Rangers exercise books.
But you can as well decide to pick only 3 Power Rangers exercise books and make up for the other 3 with any other brands of exercise book.



Code for Repetitive Selection in C#

The algorithm for Selection with Repetition will be a lot similar to that of combination.

  1. Beginning with the first member in the mother set, match it separately with every member - including itself - until the required Selection group-size (r) is reached.
  2. When every possible Selection with this member is exhausted, move to the next member in the mother set and repeat Step I.

This is how our Repetitive Selection code in C# will work.

Create a new C# class file;
Call it Selection.
Type out the adjoining C# code for Selection with Repetition.








C# Code for Selection Class

using System.Collections.Generic;

namespace Miscellaneous
{
    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
{
    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 + ".");
        }
    }
}



<< PreviousNext >>