there a "better" way to add *adjacent* numbers in the list and replace adjacent indexes with result of addition?
Expected:
[['a', 'b'], 3, ['a', 'b'], 3]
Problem is, list can be of any length and cannot guarantee *adjacent* numbers always. I mean, the solution is only for adjacent numbers
well just loop over it
Do you have a pseudo code?
new = [] while i < len(array): if array[i] is integer and array[i+1] is integer: new.append(array[i] + array[i+1]) i += 2 else: new.append(array[i]) i += 1
Here, i+1 eventually cause indexOUTerror
then handle 0th index separately and start i at 1
Who knows 0th index is not number
new = [] if array[0] is int and array[1] is int: new.append(array[0] + array[1]) i = 2 else: new.append(array[0]) i = 1 while i < len(array): if array[i] is integer and array[i-1] is integer: new.append(array[i] + array[i-1]) i += 2 else: new.append(array[i-1]) i += 1
Обсуждают сегодня