【问题标题】:Issue with repetition in Permute functionPermute 函数中的重复问题
【发布时间】:2016-05-18 11:05:46
【问题描述】:

我正在运行一个 Permute 函数,但我在获得与此相同的结果时遇到问题,我该如何防止这种情况发生?

red|red|white
white|red|red

 

Public buffer As New List(Of String)
Public Sub Permute(ByVal Root As String, ByVal Depth As Integer, ByVal Buffer1 As List(Of String))
    Dim data_array As String() = {"red", "blue", "white"}
    For Each myStr As String In data_array
        If Depth <= 1 Then
            Buffer1.Add(Root & myStr)
        Else
            Permute(Root & myStr & ",", Depth - 1, Buffer1)
        End If
    Next
End Sub

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    Bjørn-Roger Kringsjå 在this great answer 中将一些 C++ 代码转换为 VB,为此做了繁重的工作。

    原始作品用于置换字符串字符。我修改它以使用字符串(我一直想做的事情)并允许您指定分隔符。您可以只返回 List(Of String()) 或类似名称,然后在调用代码中使用 String.Join。

    Public NotInheritable Class Permutation
    
        Public Shared Function Create(array As String(), sep As String) As List(Of String)
            Return Permutation.Create(array, False, sep)
        End Function
    
        Public Shared Function Create(array As String(), sort As Boolean,
                                      sep As String) 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, sep)
            If (sort) Then
                list.Sort()
            End If
            Return list
        End Function
    
        Private Shared Sub Permute(list As List(Of String), array As String(),
                                   start As Int32, ndx As Int32, sep As String)
            Permutation.Print(list, array, ndx, sep)
            If (start < ndx) Then
                Dim i, j As Integer
                For i = (ndx - 2) To start Step -1
                    For j = (i + 1) To (ndx - 1)
                        Permutation.Swap(array, i, j)
                        Permutation.Permute(list, array, (i + 1), ndx, sep)
                    Next
                    Permutation.RotateLeft(array, i, ndx)
                Next
            End If
        End Sub
    
        Private Shared Sub Print(list As List(Of String), array As String(),
                                 size As Int32, sep As String)
            Dim tmp As New List(Of String)
            If (array.Length <> 0) Then
                For i As Integer = 0 To (size - 1)
                    tmp.Add(array(i))
                Next
                list.Add(String.Join(sep, tmp))
            End If
        End Sub
    
        Private Shared Sub Swap(array As String(), i As Int32, j As Int32)
            Dim tmp As String
            tmp = array(i)
            array(i) = array(j)
            array(j) = tmp
        End Sub
    
        Private Shared Sub RotateLeft(array As String(), start As Int32, n As Int32)
            Dim tmp As String = array(start)
            For i As Integer = start To (n - 2)
                array(i) = array(i + 1)
            Next
            array(n - 1) = tmp
        End Sub
    End Class
    

    注意:我的模组将与 Kringsjå 先生的原始代码作为重载共存。用法:

    Dim data = {"red", "blue", "white"}
    
    Dim combos = Permutation.Create(data, ", ")
    

    结果:

    红、蓝、白
    红、白、蓝
    蓝色、红色、白色
    蓝色、白色、红色
    白色、红色、蓝色
    白、蓝、红

    【讨论】:

      【解决方案2】:

      您正在做的是选择 { "red", "blue", "white" } Depth 次之一。所以如果你打电话给Permute("", 5, buffer),你会得到以下信息:

      红色,红色,红色,红色,红色 红,红,红,红,蓝 ... 白色,白色,白色,白色,蓝色 白色,白色,白色,白色,白色

      3 的 5 次方选项。

      如果您只想获得{ "red", "blue", "white" } 的所有可能排列,请执行以下操作:

      Public Function Permute(ByVal data As IEnumerable(Of String)) As IEnumerable(Of String)
          If data.Skip(1).Any() Then
              Return data.SelectMany( _
                  Function (x) Permute(data.Except({ x })).Select( _
                      Function (y) x & "," & y))
          Else
              Return data
          End If
      End Function
      

      ...然后这样称呼它:

      Dim results = Permute({ "red", "blue", "white" })
      

      我得到的结果是:

      红、蓝、白 红、白、蓝 蓝色,红色,白色 蓝色,白色,红色 白色,红色,蓝色 白色,蓝色,红色

      然后将Root 值添加到这些结果之前将是微不足道的。

      【讨论】:

        猜你喜欢
        • 2015-12-14
        • 2018-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-07
        • 2015-10-28
        • 2014-08-15
        • 2020-07-11
        相关资源
        最近更新 更多