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.
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.
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
You could use
https://docs.python.org/3/library/iterto...mbinations Wrote:itertools.combinations(iterable, r)
Return r length subsequences of elements from the input iterable.
Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.
Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.
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))
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=valref
whith 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 !