algorithm - print all product combination of array -
an array of integer a[] = {3, 5, 7}
// array elements unique print product combination:
input :
3, 5, 7 output:
3, 5, 7, 15, 21, 35, 105
recently have been asked question in interview. couldn't think of approach. please suggest approach/code.
you can simple recursive function:
def all_products(s,a,base=1): """add products of base of operations times elements array set s""" s.add(base) if a: all_products(s,a[1:],base) all_products(s,a[1:],base*a[0]) s=set() all_products(s, [3, 5, 7]) print sorted(s)
this approach includes result (1) of multiplying none of elements together.
algorithm combinations
No comments:
Post a Comment