Created
December 16, 2016 22:32
-
-
Save anhnguyen1618/cffde447c70d7f3918429a1631267800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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