-1

Get all combinations which declares an established amount sum

I managed to convert the code written to C #, and I would need some help. I don't know how to let them display the appropriate values in my Textbox.

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' Call (Main) - How to call in a Textbox1.Text
    End Sub

Code:

Class SurroundingClass
    Private Sub Main()
        Dim numbers As Integer() = {3, 9, 8, 4, 5, 7, 10}
        Dim target As Integer = 15
        sum_up(New List(Of Integer)(numbers.ToList()), target)
    End Sub

    Private Shared Sub sum_up_recursive(ByVal numbers As List(Of Integer), ByVal target As Integer, ByVal part As List(Of Integer))
        Dim s As Integer = 0

        For Each x As Integer In part
            s += x
        Next

        If s = target Then
            Console.WriteLine("sum(" & String.Join(",", part.[Select](Function(n) n.ToString()).ToArray()) & ")=" + target)
        End If

        If s >= target Then
            Return
        End If

        For i As Integer = 0 To numbers.Count - 1
            Dim remaining = New List(Of Integer)()
            Dim n As Integer = numbers(i)

            For j As Integer = i + 1 To numbers.Count - 1
                remaining.Add(numbers(j))
            Next

            Dim part_rec = New List(Of Integer)(part)
            part_rec.Add(n)
            sum_up_recursive(remaining, target, part_rec)
        Next
    End Sub

    Private Shared Sub sum_up(ByVal numbers As List(Of Integer), ByVal target As Integer)
        sum_up_recursive(numbers, target, New List(Of Integer)())
    End Sub
End Class

1 Answers1

0

When you run the code you will see that it outputs the sets of results one set at a time in the line Console.WriteLine("sum(" & String.Join(",", part.[Select](Function(n) n.ToString()).ToArray()) & ")=" + target).

As you want to get the result sets in one go, you will need to accumulate them at that point instead of writing to the console, and the Sub will need to be a Function with another parameter for the accumulator.

Making those modifications:

Public Class Form1

    ' Derived from the code at https://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum

    Function SumUpRecursive(numbers As List(Of Integer), target As Integer, part As List(Of Integer), solutions As List(Of List(Of Integer))) As List(Of List(Of Integer))
        Dim s = part.Sum()

        If s = target Then
            ' MsgBox("sum(" & String.Join(",", part.[Select](Function(n) n.ToString()).ToArray()) & ")=" & target)
            solutions.Add(part)
        End If

        If s >= target Then
            Return Nothing
        End If

        For i As Integer = 0 To numbers.Count - 1
            Dim remaining = New List(Of Integer)()
            Dim n As Integer = numbers(i)

            For j As Integer = i + 1 To numbers.Count - 1
                remaining.Add(numbers(j))
            Next

            Dim part_rec = New List(Of Integer)(part)
            part_rec.Add(n)
            SumUpRecursive(remaining, target, part_rec, solutions)

        Next

        Return solutions

    End Function


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim numbers As Integer() = {3, 9, 8, 4, 5, 7, 10}
        Dim target As Integer = 15
        Dim nums = SumUpRecursive((numbers.ToList()), target, New List(Of Integer), New List(Of List(Of Integer)))

        If nums Is Nothing Then
            MsgBox("Failure :(")
        Else
            MsgBox(String.Join(vbCrLf, nums.Select(Function(b) String.Join(", ", b))))
            ' TextBox1.Lines = nums.Select(Function(b) String.Join(", ", b)).ToArray()
        End If

    End Sub

End Class

I'm sure there's a neater way of writing it.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84