I have an arbitrary list of positive integers and some number X. I want to check if it is possible to retrieve X using basic operations such as summation and difference.
E. g.:
input_list=[1, 7, 3]
X = 4
result: TRUE
(e.g.: 7-3)
input_list=[1, 7, 3]
X = 5
result: FALSE
I have attempted to utilize the approach from this question: Find all combinations of a list of numbers with a given sum,
specifically this part:
[seq for i in range(len(numbers), 0, -1)
for seq in itertools.combinations(numbers, i)
if sum(seq) == target]
My idea was to concat the initial list with the list of the opposite integers:
new_list = input_list+list(map(lambda x: -x, w))
But this takes too much time, what I do not like is that there is some sort of duplication in this approach, itertools.combinations may take 1 and it’s opposite -1 twice, but I have no idea how to fix that.
What is the most effecient way of solving such problem?