I have a list:
l = [1,3,4,6,7,8,9,11,13,...]
and the number n.
How to effectively check whether a number can nbe expressed as the sum of two numbers (duplicates are allowed) in the list l.
If the number is in the list, it is not taken into account if it cannot be expressed as two numbers (for example, for l = [2,3,4] 3 will not be counted, but 4 will be.
This, embarrassingly, is what I tried:
def is_sum_of_2num_inlist(n, num_list):
num_list = filter(lambda x: x < n, num_list)
for num1 in num_list:
for num2 in num_list:
if num1+num2 == n:
return True
return False
thank
source
share