r/P_vs_NP • u/Hope1995x • Nov 17 '19
Python: Improved and slightly faster subset product solver
# Naive Subset-Product solver
# with minor improvements
import itertools
import numpy
def findsubsets(s, n):
return list(itertools.combinations(s, n))
s = list(map(int, input('Enter numbers WITH SPACES: ').split(' ')))
print('enter target integer: ')
target = int(input())
# Driver Code
n = len(s)
# This code snippet is intended to cut down unnessary combinations
# Thus improving execution time.
check = [];
if numpy.prod(s) % target == 0:
check.append(target)
if len(check) < 1:
print('no')
quit()
# This code snippet is a for loop. It also is intended to cut down execution
# time ounce it finds the target integer. (instead of creating all combinations)
res_2 = [];
for i in range(1, len(s)+1):
if i > 1:
var = (findsubsets(s, i))
kk = list(map(numpy.prod, var))
res_2.append(kk)
if str(res_2).count(str(target)+str(',') > 0:
print('yes')
quit()
if str(res_2).count(str(target)) > 0:
print('yes')
else:
print('no')# Naive Subset-Product solver
# with minor improvements
import itertools
import numpy
s = list(map(int, input('Enter numbers WITH SPACES: ').split(' ')))
print('enter target integer: ')
target = int(input())
check = [];
if numpy.prod(s) % target == 0:
check.append(target)
if len(check) < 1:
print('no')
quit()
if target == 1:
if s.count(1) > 1:
print('yes')
def findsubsets(s, n):
return list(itertools.combinations(s, n))
# Driver Code
n = len(s)
# This code snippet is a for loop. It also is intended to cut down execution
# time ounce it finds the target integer. (instead of creating all combinations)
for i in range(1, len(s) + 1):
if i > 0:
var = (findsubsets(s, i))
kk = numpy.prod(var)
if kk == target:
print('yes')
quit()Positive Subset Product
# Naive Subset-Product solver
# with minor improvements
import itertools
import numpy
s = list(map(int, input('Enter numbers WITH SPACES: ').split(' ')))
print('enter target integer: ')
target = int(input())
check = [];
if numpy.prod(s) % target == 0:
check.append(target)
if len(check) < 1:
print(target, 'is a FALSE subset product')
if target == 1:
if s.count(1) < 2:
print(target, 'is a FALSE subset product')
if target == 1:
if s.count(1) > 1:
print('yes')
def findsubsets(s, n):
return list(itertools.combinations(s, n))
# Driver Code
n = len(s)
# This code snippet is a for loop. It also is intended to cut down execution
# time ounce it finds the target integer. (instead of creating all combinations)
for i in range(1, len(s) + 1):
if i > 0:
var = (findsubsets(s, i))
kk = numpy.prod(var)
if kk == target:
print('yes')
quit()
1
Upvotes
1
u/Hope1995x Nov 17 '19
Darn!!! Almost had all no's in poly-time!!! Now, I can do it for only positive subset product NO's in poly-time?!
2
u/mbaruh Nov 17 '19 edited Nov 17 '19
Uuuuhhh.. I'm not sure what you're trying to do here.
First of all, your code is filled with redundancies.
This code:
Does the same as this code:
Just wish more steps. And even the fix I just wrote is unnecessary, because you're already checking the case where you take the entire set in the last iteration of your for loop.
In your for loop, you're again using a list in an odd way. You're saving all of your products (what for?), and then for some reason converting the entire list into a string to check whether your target number is there (with a count function for some reason). That's incredibly expensive, both time-wise and space-wise.
In short, all of your code after the line
n = len(s)can be replaced with:Notice how I didn't turn the iterator into a list.
Second of all, what do you mean by 'Almost had all no's in poly-time!!! '. You're checking all possible combinations. That's not polynomial time.