This seems like a recursive problem that afl can't handle. Howard seems to be the expert on Python on this board, perhaps he knows.
From: amibroker@yahoogroups.com [mailto:amibroker@yahoogroups.com]
Sent: Monday, February 15, 2016 3:52 PM
To: amibroker@yahoogroups.com
Subject: [amibroker] Finding all possible combinations of numbers to reach a given sum
I am trying to calculate what numbers in an array or a vector added up to a particular sum. I googled it and find example of it on stackoverflow.com.
http://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum
The examples are in python and other languages. They look very simple but I have not been able to convert it to afl. I thought some smart guy on this board maybe able to convert it to afl. Much thanks in advance.
def subset_sum(numbers, target, partial=[]):
s = sum(partial)
# check if the partial sum is equals to target
if s == target:
print ("sum(%s)=%s" % (partial, target))
if s >= target:
return # if we reach the number why bother to continue
for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[i+1:]
subset_sum(remaining, target, partial + [n])
if __name__ == "__main__":
subset_sum([1,2,4,8,16,32,64],104)
#Outputs:
#sum([1, 2])=3
Posted by: "TA" <tagroups@sbcglobal.net>
Reply via web post | • | Reply to sender | • | Reply to group | • | Start a New Topic | • | Messages in this topic (2) |
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
EmoticonEmoticon