I use to code algorithm by hand trying to be readable and understandable.
Here is an old geek fashion code with the following algoritm:
if a1,a2 are in b1,b2,b3 respective products are a1a2 and a1a2the third element.
the thirdElement is exactly b1b2b3//a1a2 if the remainder is 0. (notice integer division).
As a1a2 can be equal to several bxby , check too that a1+a2 == bx+by (where bx+by is (b1+b2+b3 - third element) ).
(a3 is set to None here, could be "" depending of your data )
tests = [ \
["2022-02-28",95,11,2,3,22,67,25],\
["2022-02-27",85,84,5,None,72,23,15],\
["2022-02-26",87,6,7,8,5,84,89],\
["2022-02-25",72,9,10,44,55,78,41],\
["2022-02-24",66,19,57,None ,50,60,51],\
["2022-02-23",88,20,48,67,19,66,57]\
]
# first collect products and sums of all b
storeb1b2b3 =[]
storesumb1b2b3 = []
for test in tests:
storeb1b2b3.append(test[5]*test[6]*test[7])
storesumb1b2b3.append(test[5]+test[6]+test[7])
# now loop on a1 a2 and check in all b
for ia in range(0,len(tests)):
test = tests[ia]
# check a3 empty
if (test[4] != None):
continue
a1= test[2]
a2 = test[3]
a1a2 =a1*a2
# check against all b
for ib in range(0,len(storeb1b2b3)):
b1b2b3= storeb1b2b3[ib]
sumb1b2b3 = storesumb1b2b3[ib]
if (b1b2b3 % a1a2 == 0):
candidate =(b1b2b3//a1a2)
if ((sumb1b2b3 - candidate) == (a1+a2)):
print ( "for",a1,a2,"at",tests[ia][0],"found: ",candidate, "at ",tests[ib])
Notes :
products and sum of b are calculated once at begining to optimize. If a too long list to be stored, can be calculated on the fly in a1a2 loops.
Use indices ia, ib to be able to print source lines.
for 84 5 at 2022-02-27 found: 89 at ['2022-02-26', 87, 6, 7, 8, 5, 84, 89]
for 19 57 at 2022-02-24 found: 66 at ['2022-02-23', 88, 20, 48, 67, 19, 66, 57]
Hope you enjoy coding too :)
addendum : someone asks me to prove that if ab=a'b' and a+b=a'+b' then pair [a,b] is unique.
Demonstration.
ab = a'b' and a+b = a'+b'
if a = a' , as (a+b = a'+b') => b = b' : they are same numbers.
if a != a' => a' = a + x, as (a+b = a'+b') => b' = b - x
products are :
ab = (a+x)*(b-x) = ab - xa + xb -x2 = ab +x(-a+b-x).
x(-a+b-x) = 0.
two cases :
1) x = 0 so a'= a+0 and b' = b-0 still same values a,b.
2) -a+b-x = 0
x = b-a
so a'= a+x = b.
b'= b-x = a
just a permutation of a,b in b,a
=> in any case if (ab = a'b')and (a+b=a'+b') this is always the same pair [a,b].
QED