List of numbers to add - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: List of numbers to add (/thread-21264.html) |
List of numbers to add - Planeuge - Sep-21-2019 So, this should be kinda simple to program, but I haven't been coding for a long time, and I'd need that project done the fastest possible, so I'm sorry if I'm being rude. Anyway, I have a list an initial number (let's call it A) and a list of 20 Numbers (some of which are negative). I can only add them, and I'd like to get from A to 0 using only those Numbers (andI can only use them once). I Don't know if I'm really clear, but I tried my best. P.S: It might seem like a school work, but it's not. If it was, it would be just stupid from me to just ask people on the internet. RE: List of numbers to add - Yoriz - Sep-21-2019 Please post a minimal code sample (in python code tags) of what you have so far, explain what you expect to happen and what is actually happening and any errors received in error tags. RE: List of numbers to add - Planeuge - Sep-21-2019 So, what I was going for, is first of all test if A + one of the Numbers was equal to the total I wanted, and got to this: testlist=[5,8,23,-12] total=15 init=7 def main(lis, valInit, valsearched): for i in lis: valInit+=i if valInit==total: return i valInit=7 print(main(testlist, init, total))I picked the values so as they work. Now, what I had planned was to set init (the initial value) to init+testlist[1] and then try with adding testlist[2], testlist[3] and testlist[4]. If it doesn't work, set init to init+testlist[1]+testlist[2] and try adding that value to one of the other values, and so on. Again, excuse me if I'm not clear enough in my explanations So, I thought it might be much easier, if I keep using for loops, to use something like that: for i in lis: for j in range(1, len(lis)): valInit=valInit+i+j if valInit==total: return i RE: List of numbers to add - Yoriz - Sep-21-2019 You could use https://docs.python.org/3/library/itertools.html#itertools.combinations Wrote:itertools.combinations(iterable, r) to get the various combinations of the list values to add to the initial value itertools also has the following recipe def powerset(iterable): "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" s = list(iterable) return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) RE: List of numbers to add - Planeuge - Sep-21-2019 Thanks a lot ! I didn't give it a look though, but I'll definitely do it tomorrow, it looks pretty powerful :D In the end, I just wrote one of the least efficient code I've ever created: def main(lis, valInit, valcherchee): valref=valInit for a in range (len(lis)): valInit+=lis[a] if -1<valInit<1: return a else : for b in range(a+1, len(lis)): valInit+=lis[b] if -1<valInit<1: return a,b else: for c in range(b+1, len(lis)): valInit+=lis[c] if -1<valInit<1: return a,b,c else: for d in range(c+1, len(lis)): valInit+=lis[d] if -1<valInit<1: return a,b,c,d valInit=valref+lis[a]+lis[b]+lis[c] valInit=valref+lis[a]+lis[b] valInit=valref+lis[a] valInit=valrefwhith a few more lines. It worked anyway, I got where I wanted, but it must be the least efficient way to do it, so I'll work towards improving it, and thanks again for your help ! |