I hope this helps you . See the knapsack problem. What I would do is to generate all the sums that you can get with payables, then all the sums you can get with receivables and then look where they overlap and take the largest value. code :
def knapsack(items) {
sums = {0: null}
for item in items:
for sum, last_item in sum:
// Skip if you've already used item or the you can get the sum some other way.
if last_item == item or sum + item.value in sums: continue
else:
sums[sum+item.value] = item
return sums
payable_sums = knapsack(payables)
receivable_sums = knapsack(receivables)
for sum, item in payable_sums:
if sum in receivable_sums:
print "Success, you can get ", sum, " on both sides."
Share :

Select rows which do not have the first column matching any other rows
By : Fred Petillo
Date : March 29 2020, 07:55 AM
will help you I need to select the rows that do not have the first column matching. For example, from the data below; code :
SELECT Person, MIN(Room) AS Room
FROM YourTable
GROUP BY Person
SELECT Person, Room
FROM YourTable
GROUP BY Person

Algorithm to efficiently select rows from a matrix such that column totals are equal
By : Afraz Khan
Date : March 29 2020, 07:55 AM
To fix this issue Assuming each entry in the matrix can either be 0 or 1, this problem seems to be in the same family as the Partition Problem which only has a pseudopolynomial time algorithm. Let r be the number of rows in the matrix and c be the number of columns in the matrix. Then, encode each row to a cdigit number of base r+1. This is to ensure when adding each encoding, there is no need to carry, thus equivalent numbers in this base will equate to two sets of rows whose column sums are equivalent. So in your example, you would convert each row into a 4digit number of base 9. This would yield the numbers (converted into base 10): 10109 => 73810 code :
# Sorted (descending) => 810, 810, 738, 738, 90, 82, 82, 10
from queue import PriorityQueue
def karmarkar_karp_partition(arr):
pqueue = PriorityQueue()
for e in arr:
pqueue.put_nowait((e, e))
for _ in range(len(arr)1):
_, first = pqueue.get_nowait()
_, second = pqueue.get_nowait()
diff = first  second
pqueue.put_nowait((diff, diff))
return pqueue.get_nowait()[1]

In SQL, how to select rows with matching values in one column, based on earliest date in another column
By : dfghjkl
Date : March 29 2020, 07:55 AM
Does that help A simple MIN will do: code :
SELECT
customer_name,
MIN(date) AS earliest_date
FROM myschema.mytable
GROUP BY customer_name;

SQL Select: Do rows matching id all have the same column value
By : Sammie Riddell
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I have a table like this , You could use the following method: code :
select t.sub_id
from YourTable t
group by t.sub_id
having max(t.reference) <> min(t.reference)

How to efficiently select rows based on multiple column values present in a list (of tuples)?
By : Joofy Joof
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further There are ways to do this, some more hacky than others. My recommendation is to generate a MultiIndex, these work nicely with a list of tuples: code :
# <=0.23
idx = pd.MultiIndex.from_arrays([
products['store_id'], products['product_store_id']])
# 0.24+
idx = pd.MultiIndex.from_frame(products[['store_id', 'product_store_id']])
products.loc[idx.isin(products_list)]
products_list_concat = [''.join(l) for l in products_list]
mask = ((products['store_id'] + products['product_store_id'])
.isin(products_list_concat))
products.loc[mask]

