You can use the following logic as a possible solution:
start sum = 0
for each row in datatable
sum= sum + row(“Amount”)
if sum > total amount :
sum= sum - row(“Amount”)
if sum = row(“Amount”):
break
Create a assign activity and name it as intTotalAmount.
Take For Each Row in Datatable activity
Pass your data table variable
Inside loop add one assign activity in that to section pass above intTotalAmount variable and in Value section If(IsNumeric(CurrentRow(“ColumnName”).ToString.Trim),intTotalAmount +CInt(CurrentRow(“ColumnName”).ToString.Trim),intTotalAmount)
After this add an IF activity
in that if activity check your condition
intTotalAmount = Totalamount
Use Invoke Code (VB.NET) to find all combinations that sum to total (e.g., 25):
result = New List(Of List(Of Integer))
Sub Find(start As Integer, target As Integer, current As List(Of Integer))
If target = 0 Then result.Add(New List(Of Integer)(current)) : Return
For i = start To listAmounts.Count - 1
If listAmounts(i) <= target Then
current.Add(listAmounts(i))
Find(i + 1, target - listAmounts(i), current)
current.RemoveAt(current.Count - 1)
End If
Next
End Sub
Find(0, 25, New List(Of Integer))
result will have all row combinations summing to the total. Use it to get matching rows from DT1.
here dt is the datatable and assumption is first column contains numbers and 25 is the sum it check ..you can add variable in place of it or anything
also first to know if already one combination has sum better use .count>0 then get the first combination..this give multiple combiantions if available use can use first or (0) to get the first combination
I see. this might be because Math.Pow and Enumerable.Range , which are limited by 32.
for this i would recommend you to use script like the Invoke code using VB or C#.
Can you share the value passed inside result variable?
//Inputs: dt (DataTable), amount (Decimal)
//Output: matchingCombos (List(Of List(Of DataRow)))
Dim matchingCombos As New List(Of List(Of DataRow))()
Sub FindCombinations(index As Integer, currentCombo As List(Of DataRow), currentSum As Decimal)
If currentSum = amount Then
matchingCombos.Add(New List(Of DataRow)(currentCombo))
Return
End If
If index >= dt.Rows.Count OrElse currentSum > amount Then
Return
End If
// Include current row
currentCombo.Add(dt.Rows(index))
FindCombinations(index + 1, currentCombo, currentSum + Convert.ToDecimal(dt.Rows(index)(12)))
currentCombo.RemoveAt(currentCombo.Count - 1)
//Exclude current row
FindCombinations(index + 1, currentCombo, currentSum)
End Sub
FindCombinations(0, New List(Of DataRow)(), 0D)
in the invoke code activity use below variables and its type |
dt as InArgument ( for this variable pass your DataTable variable )
amount as InArgument (for this variable pass your target sum, create a variable in variable panel as amount of Int then assign it a value, e.g., 25.)
matchingCombos as OutArgument (List(Of List(Of DataRow))) → your output argument.
Please try above reference and let us know your result.
It is working fine & finding the match, but it is not stop running if there is no match.
’ Copy UiPath arguments into local vars
Dim localDT As DataTable = dt
Dim targetAmount As Decimal = bank_amount
Dim results As New List(Of List(Of DataRow))()
Dim totalRows As Integer = localDT.Rows.Count
Dim RecursiveFind As Action(Of Integer, List(Of DataRow), Decimal) = Nothing
RecursiveFind = Sub(index As Integer, currentCombo As List(Of DataRow), currentSum As Decimal)
If currentSum = targetAmount Then
results.Add(New List(Of DataRow)(currentCombo))
Return
End If
If index >= totalRows OrElse currentSum > targetAmount Then
Return
End If
' Include current row
currentCombo.Add(localDT.Rows(index))
RecursiveFind(index + 1, currentCombo, currentSum + Convert.ToDecimal(localDT.Rows(index)(12)))
currentCombo.RemoveAt(currentCombo.Count - 1)
' Exclude current row
RecursiveFind(index + 1, currentCombo, currentSum)
End Sub
’ Start recursion
RecursiveFind(0, New List(Of DataRow)(), 0D)
’ Write result back to output argument
matchingCombos = results
Try add a check afterthe recursion to determine if any matches were found, and take appropriate action.
=========================================
//Start recursion
RecursiveFind(0, New List(Of DataRow)(), 0D)
// Return empty list or a custom message if no match is found
If results.Count = 0 Then
//Return an empty list
matchingCombos = New List(Of List(Of DataRow))()
LogMessage(“No matching combination found.”)
Else
matchingCombos = results // this means results is null in UiPath u can use if condition and pass condition matchingCombos.Count = 0 and in the statement pass what is your requirement.