Skip to content

Instantly share code, notes, and snippets.

@anhnguyen1618
Created December 16, 2016 22:32
Show Gist options
  • Save anhnguyen1618/cffde447c70d7f3918429a1631267800 to your computer and use it in GitHub Desktop.
Save anhnguyen1618/cffde447c70d7f3918429a1631267800 to your computer and use it in GitHub Desktop.
import numpy as np
def find_combination(choices, total):
new_array = list(reversed(sorted(choices)))
new = compare(new_array, total)[1]
result=[]
for number in choices:
if number in new:
new.pop(new.index(number))
result.append(1)
else:
result.append(0)
return np.asarray(result)
def compare(array, total):
if total == 0:
return (True,[])
elif total < 0 or len(array) == 0:
return (False,[])
number = array[0]
if total > number:
have = compare(array[1:], total- number)
dont_have = compare(array[1:], total)
if have[0]:
have[1].append(number)
return (True,have[1])
if dont_have[0]:
return (True,dont_have[1])
have[1].append(number)
return (False,have[1])
if total < number:
return compare(array[1:],total)
return (True ,[number])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment