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







<< PreviousNext >>

Code for Selection with Limits to Repetitions in VB.Net



Conditional Selection with Limits on Repetition.

So it's Christmas, and your mum delegates you, for some mysterious reason, to go buy nine (9) balloons - of any of the colours red, orange, yellow, green, blue, indigo, violet, pink, milk, white,- for home decoration.

But here's the catch. You are to buy:
  1. a minimum of 1 red balloon and a maximum of 4 red balloons;
  2. a minimum of 1 orange balloon and a maximum of 3 orange balloons;
  3. a minimum of 1 yellow balloon and a maximum of 2 yellow balloons;
  4. a minimum of 1 green balloon and a maximum of 4 green balloons;
  5. a minimum of 1 blue balloon and a maximum of 3 blue balloons;
  6. a minimum of 1 indigo balloon and a maximum of 2 indigo balloons;
  7. a minimum of 1 violet balloon and a maximum of 4 violet balloons;
  8. a minimum of 1 pink balloon and a maximum of 3 pink balloons;
  9. a minimum of 1 gray balloon and a maximum of 2 gray balloons;
  10. a minimum of 1 white balloon and a maximum of 4 white balloons.

With these conditions, every family member's favourite colour is covered for, and the decoration mix is also kept lively.

This is quite a handful for you to handle...



Code for Limited Repetitive Selection in VB.Net

The code for Selection with Conditioned Repetition will be based on that for Selection with boundless Repetition.

All that is needed after Selection with limitless Repetition is a Productive, as opposed to Summative, check of the results from the Selection with unconditioned Repetition for those options that meet our conditions.

This is how our Limited Repetitive Selection algorithm in VB.Net will work.

Create a new VB.Net class file; Call it ConditionalSelection.
Optionally, Create a new VB.Net module file; Call it Miscellaneous_ConditionalSelection.

Type out the adjoining VB.Net algorithm for Selection with Conditioned Repetition.







VB.Net Code for ConditionalSelection Class

Public Class ConditionalSelection
    Inherits Selection

    Private final_elements As List(Of String())

    ' pick only those groups that meet occurrence conditions
    Public Function limitedSelection(candidates As String(), size As Integer, minimum As Integer(), maximum As Integer()) As List(Of String())
        final_elements = New List(Of String())
        groupSelection(candidates, size)
        For i As Integer = 0 To complete_group.Count - 1
            Dim state As Boolean = False
            For j As Integer = 0 To words.Length - 1
                ' get 'word(j)' frequency/count in group
                Dim count As Integer = -1
                Dim k As Integer = -1
                Do
                    k += 1
                    count += 1
                    k = Array.IndexOf(complete_group(i), words(j), k)
                Loop While k > -1
                ' Productive boundary check
                If count >= minimum(j) And count <= maximum(j) Then
                    state = True
                Else
                    state = False
                    Exit For
                End If
            Next j
            ' skip if already in net
            If state Then
                final_elements.Add(complete_group(i))
            End If
        Next i
        Return final_elements
    End Function

End Class

Miscellaneous_ConditionalSelection

Module Miscellaneous_ConditionalSelection

    Sub Main()

        Dim goods As String() = {"0""1""2""3""4""5""6""7""8""9"}
        ' minimum number of occurrences
        Dim min As Integer() = {0, 0, 1, 0, 0, 1, 0, 0, 1, 0}
        ' maximum number of occurrences
        Dim max As Integer() = {4, 3, 2, 4, 3, 2, 4, 3, 2, 4}

        Dim choose As New ConditionalSelection
        Dim result As List(Of String()) = choose.limitedSelection(goods, 5, min, max)

        ' print choices and operation
        Console.Write("[ ")
        For Each choice As String In choose.words
            Console.Write(choice & "; ")
        Next
        Console.WriteLine("] conditioned selection " & choose.r & ":" & Environment.NewLine)

        ' print out selections nicely
        Dim i As Integer = 0
        For Each group As String() In result
            i += 1
            Console.Write(i & ": ")
            For Each member As String In group
                Console.Write(member & "; ")
            Next
            Console.WriteLine()
        Next
        Console.WriteLine(Environment.NewLine & "Number of ways is " & result.Count & ".")
    End Sub

End Module



<< PreviousNext >>