I’m making a calculator program and I’m currently on implementing negatives. So if I input 1--1--1
into the program, it’ll first split it into a series of operations and integers, so that will turn into [1, '-', '-', 1, '-', '-', 1]
. Then the functions I’m writing now are supposed to put the negatives where they go, so the preferred output in this case would be [1, '-', -1, '-', -1]
. Here are the functions so far:
def join_neg(lst):
tempstr = ''
tempint = 0
neg_ndx = 256000
for num in lst:
if num == '-':
neg_ndx = lst.index(num)
break
if neg_ndx != 256000:
neg = lst[neg_ndx]
potential_int = lst[neg_ndx + 1]
if isinstance(potential_int, int):
if potential_int > 0:
tempstr = neg + str(potential_int)
del lst[neg_ndx:neg_ndx + 2]
tempint = int(tempstr)
lst.insert(neg_ndx, tempint)
elif potential_int <= 0:
print(neg_ndx)
return join_neg_two(lst, neg_ndx)
elif isinstance(potential_int, str):
neg = lst[neg_ndx + 1]
potential_int = lst[neg_ndx + 2]
tempstr = neg + str(potential_int)
del lst[neg_ndx + 1:neg_ndx + 3]
tempint = int(tempstr)
lst.insert(neg_ndx + 1, tempint)
return join_neg(lst)
elif neg_ndx == 256000:
return lst
def join_neg_two(lst, ndx):
tempstr = ''
tempint = 0
neg_ndx = 256000
ndx += 1
print(ndx)
for num in lst[ndx:]:
if num == '-':
neg_ndx = lst.index(num)
break
print(neg_ndx)
if neg_ndx != 256000:
neg = lst[neg_ndx]
potential_int = lst[neg_ndx + 1]
if isinstance(potential_int, int):
if potential_int > 0:
tempstr = neg + str(potential_int)
del lst[neg_ndx:neg_ndx + 2]
tempint = int(tempstr)
lst.insert(neg_ndx, tempint)
elif potential_int <= 0:
return lst
elif isinstance(potential_int, str):
neg = lst[neg_ndx + 1]
potential_int = lst[neg_ndx + 2]
tempstr = neg + str(potential_int)
del lst[neg_ndx + 1:neg_ndx + 3]
tempint = int(tempstr)
lst.insert(neg_ndx + 1, tempint)
return join_neg(lst)
elif neg_ndx == 256000:
return lst
One thing to note is that the functions are supposed to do this with any minus it finds, provided the next index contains an integer, otherwise it continues on. So [1, '-', 1]
should output [1, -1]
. Another thing I think I should address is the fact that the variable neg_ndx
is assigned to the value 256000
, this is how the program knows that it’s finished combining all the negatives, and can move onto the next set of functions. The issue I’m currently having is that in the join_neg_two
function, (using the example [1, '-', '-', 1, '-', '-', 1]
which will be passed to this function looking like this: [1, '-', -1, '-', '-', 1]
) the for
loop should start iterating at the -1
object in the list, so (I think) the variable neg_ndx
should become 3
which will then be passed to the rest of the function, but it doesn’t. It gets turned into 1
which messes up the rest of the function. Is there something wrong with the way I’m iterating through the list starting at a different index other than 0 or is it something else?
Last thing, I seem to do a bad job formatting code since the last time I asked a question I got a lot of people telling me that variables, style, etc. were pretty bad, so if you find that my style is incomprehensible and need clarification or anything worse than what you’d expect from a beginner, please let me know. Although I’d rather the discussion not focus completely around stylistic issues, as I still need help with the core problem.