I am trying to create a function that will find the maximum absolute value for each entry in three lists and combine it into one list.
Given the three lists:
S12 = [-3, 24, 8, 13, 24, 43, 33, 19, -14, -36, -30, -16, -21]
S23 = [13, 5, -67, -98, 37, 30, 9, 11, -4, 2, 43, 26, -24]
S13 = [ 9, 40, -2, -26, -25, -19, -25, -31, 36, 23, 39, 47, 29]
I want the code to grab the maximum absolute value, first of -3, 13 and 9 then of 24, 5 and 40 and so on.
I have the following code:
def maxOfThree(s1, s2, s3):
maxOfThree = s1
for i in range(len(s1)):
if abs(s2[i]) > abs(maxOfThree[i]):
maxOfThree[i] = s2[i]
elif abs(s3[i]) > abs(maxOfThree[i]):
maxOfThree[i] = s3[i]
return maxOfThree
maximumValueInThreeLists = maxOfThree(S12,S23,S13)
print(maximumValueInThreeLists)
The expected result is
[13, 40, -67, -98, 37, 43, 33, -31, 36, -36, 43, 47, 29]
but the output I get is
[13, 40, -67, -98, 37, 43, 33, -31, 36, -36, 43, 26, -24]
I cant figure out what is going wrong with my code, but some of the entries are not correct (in this case the final two, but that is just a coincedence).