In [ ]:
allowed_set = [int(str(x), 16) for x in range(100)]
targets = [int("18", 16), int("00", 16), int("18", 16), int("8B", 16)]
In [67]:
fhex = lambda x: "{0:#0{1}x}".format(x, 4)
def subset_sum(allowed_set, target, partial=[], results=[]):
desired_length = 3
if target == 0x00:
return [['0x55', '0x55', '0x55']]
s = sum(partial)
# check if the partial sum is equals to target
if s == target:
if len(partial) == desired_length:
results.append([ fhex(x) for x in partial])
if s >= target:
return # if we reach the number why bother to continue
for i in range(len(allowed_set)):
if len(results) == 1:
break
n = allowed_set[i]
remaining = allowed_set[i+1:]
subset_sum(remaining, target, partial + [n])
return results
In [69]:
results = []
for desired_sum in targets:
res = subset_sum(allowed_set, desired_sum)
results.append(res[0])
print(fhex(desired_sum), ":", res[0])
In [70]:
import numpy as np
print("="*22)
for row in np.array(results).T:
for elem in row:
print(elem, ' ', end='')
print("")
print("="*22)
for desired_sum in targets:
print(fhex(desired_sum), ' ', end='')