0

how should I set a condition (all the permutations of 3 of the set of 5. of any 2 of 5, and etc.

if I have:

Me.TextBox1.Text = String.Join(Environment.NewLine, Permutation.Create({"1"c, "2"c, "3"c, "4"c, "5"c}, sort:=False))

Output: (thousand show me permutations of 5 of 5).)

12345
12354
12435
12453
12534 and so on...

Expected Output: Value = 3 (all the permutations of 3 of the set of 5.

123
124
125 
and so on...

how should i set this condition?

Public NotInheritable Class Permutation

    Public Shared Function Create(array As Char()) As List(Of String)
        Return Permutation.Create(array, False)
    End Function

    Public Shared Function Create(array As Char(), sort As Boolean) As List(Of String)
        If (array Is Nothing) Then
            Throw New ArgumentNullException("array")
        ElseIf ((array.Length < 0) OrElse (array.Length > 13)) Then
            Throw New ArgumentOutOfRangeException("array")
        End If
        Dim list As New List(Of String)
        Dim n As Integer = array.Length
        Permutation.Permute(list, array, 0, array.Length)
        If (sort) Then
            list.Sort()
        End If
        Return list
    End Function

    Private Shared Sub Permute(list As List(Of String), array As Char(), start As Integer, n As Integer)
        Permutation.Print(list, array, n)
        If (start < n) Then
            Dim i, j As Integer
            For i = (n - 2) To start Step -1
                For j = (i + 1) To (n - 1)
                    Permutation.Swap(array, i, j)
                    Permutation.Permute(list, array, (i + 1), n)
                Next
                Permutation.RotateLeft(array, i, n)
            Next
        End If
    End Sub

    Private Shared Sub Print(list As List(Of String), array As Char(), size As Integer)
        If (array.Length <> 0) Then
            Dim s As Char() = New Char(size - 1) {}
            For i As Integer = 0 To (size - 1)
                s(i) = array(i)
            Next
            list.Add(s)
        End If
    End Sub

    Private Shared Sub RotateLeft(array As Char(), start As Integer, n As Integer)
        Dim tmp As Char = array(start)
        For i As Integer = start To (n - 2)
            array(i) = array(i + 1)
        Next
        array(n - 1) = tmp
    End Sub

    Private Shared Sub Swap(array As Char(), i As Integer, j As Integer)
        Dim tmp As Char
        tmp = array(i)
        array(i) = array(j)
        array(j) = tmp
    End Sub

End Class

Because of the Int32.MaxValue limit this class will support levels 1 through 13.

s=1, n=1
s=2, n=2
s=3, n=6
s=4, n=24
s=5, n=120
s=6, n=720
s=7, n=5040
s=8, n=40320
s=9, n=362880
s=10, n=3628800
s=11, n=39916800
s=12, n=479001600
s=13, n=6227020800

Usage: Me.TextBox1.Text = String.Join(Environment.NewLine, Permutation.Create({"c"c, "f"c, "a"c, "m"c}, sort:=False))

  • Do you need to use the `Permutation` class that you have shown in your question? It seems like something that could be achieved in a few lines if you don't have to use that code. – theduck Dec 17 '19 at 09:28
  • I can try to see what happens. https://stackoverflow.com/questions/21084651/vb-net-permutation-of-string-permutation-or-combination - I used everything that is here. – Martin Dorin Dec 17 '19 at 09:37

1 Answers1

0

You can do this with some recursion:

Sub Main()
    Dim result = GetCombinations({"1"c, "2"c, "3"c, "4"c, "5"c}.ToList(), 3)

    For Each item In result
        Console.WriteLine(String.Concat(item))
    Next
End Sub

Private Function GetCombinations(Of T)(sourceList As IEnumerable(Of T), length As Integer) As IEnumerable(Of IEnumerable(Of T))
    If length = 1 Then Return sourceList.[Select](Function(x) New T() {x})
    Return GetCombinations(sourceList, length - 1).SelectMany(Function(x) sourceList, Function(x1, x2) x1.Concat(New T() {x2}))
End Function

The GetCombinations() function works by getting all the combinations that are one less length that the current length of the list. It does this recursively until the length of the list is 1 - at which point it stops recursing.

To get the output comma delimited use something like:

For Each item In result
    Console.WriteLine(String.Join(",", item))
Next

To change the length of the output string, change the initial call to GetCombinations() for instance to get 2 character strings use:

Dim result = GetCombinations({"1"c, "2"c, "3"c, "4"c, "5"c}.ToList(), 2)
theduck
  • 2,589
  • 13
  • 17
  • 23
  • I understand. works well, does something like this come out to me, 112 113 114, can I somehow put "," between them or space? like 1,1,2 and 1,1,3 / 1,1,4 - TextBox1.AppendText(vbCrLf & String.Concat(item)) – Martin Dorin Dec 17 '19 at 09:50
  • You can use `string.join` to add commas between the characters. For example: `String.Join(",", item)` will give you comma delimited values in the For Each loop. – theduck Dec 17 '19 at 09:56