Skip to content

Instantly share code, notes, and snippets.

@quxiaowei
Created November 16, 2022 16:17
Show Gist options
  • Save quxiaowei/ccb676bf2b66a4b0f9a35b959e0e7d09 to your computer and use it in GitHub Desktop.
Save quxiaowei/ccb676bf2b66a4b0f9a35b959e0e7d09 to your computer and use it in GitHub Desktop.
import decimal
from decimal import Decimal
a = ['23.17', '3.2', '1.22', '0.32']
b = ['7.36', '4.16', '3.2', '1.69', '1.28', '1.28', '0.96', '0.96', '0.90', '0.64', '0.64', '0.64', '0.50', '0.50', '0.32', '0.32', '0.32', '0.32', '0.32', '0.32', '0.32', '0.32', '0.32', '0.32']
a = ["52.7", "8.96"]
b = ["21.44", "6.72", "5.44", "5.12", "4.48", "3.20", "2.24", "1.92", "1.92", "1.92", "1.28", "1.28", "1.00", "0.96", "0.50", "0.32", "0.32", "0.32", "0.32", "0.32", "0.32", "0.32"]
a = ['17.76','62.13','26.67'] #'62.13','17.76',]
b = ['24.92','5.88','5.04','3.64','3.45','3.36','2.8','2.8','2.52','2.24','2.24','2.24','1.96','1.96','1.8','1.68','1.4','1.4','1.4','1.2','1.2','1.15','1.12','1.12','1.12','1.12','1.12','0.84','0.84','0.84','0.84','0.84','0.84','0.84','0.84','0.84','0.56','0.56','0.56','0.56','0.56','0.56','0.56','0.56','0.56','0.56','0.56','0.56','0.4','0.4','0.4','0.4','0.4','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28','0.28']
a = [Decimal(aa) for aa in a]
b = [Decimal(bb) for bb in b]
a.sort()
b.sort()
def _sum( a ):
_s = Decimal(0)
for i in a:
_s += i
return _s
print("sum(a):", _sum(a))
print("sum(b):", _sum(b))
def main(a):
for i, aa in enumerate(a[:4]):
print(f"a = { aa } , len(b) = { len(b) }, sum(b) = { _sum(b) }")
# print(b)
findAinB(aa, 0)
def findAinB(a1, ib):
# print(ib)
if ib >= len(b):
return False
b1 = b[ib]
if a1 < b1:
return False
elif a1 == b1:
print('\t', b1)
b.pop(ib)
return True
elif a1 > b1:
ii = ib+1
for i, bb in enumerate( b[ib+1:] ):
if bb > b1 :
ii += i
break
if findAinB(a1, ii):
return True
elif findAinB(a1-b1, ib+1):
print('\t', b1)
b.pop(ib)
return True
return False
main(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment