How do you find the max difference between 5 numbers? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 2

How do you find the max difference between 5 numbers?

We have a numerical dataset consisting of 5 columns. Goal is to find the max differnce between these columns and output the column having the maximum difference. eg. a = { 1,2,3,4} b = { 1,4,5,6} c = { 7,3,5,6} Here max differnce is between c[0] - a[0] = 6 I want to write a code to achieve the same. Ps : hopefully using pandas.

7th Jan 2017, 10:45 PM
PYTHONISTA
PYTHONISTA - avatar
7 Respuestas
+ 2
Your example specifies 3 set objects with 4 members each, but sets do not support [] indexing.
7th Jan 2017, 10:57 PM
Free Boro
Free Boro - avatar
+ 2
You must use square brackets to use indexed list ^^ ( a = [ 0, 1, 2, 3, 4 ] ) However, algoritm proposed by @Nathan is a good solution... Notice that you'll be advise to store your arrays into another one, so you can iterate over them ;)
8th Jan 2017, 3:56 AM
visph
visph - avatar
+ 1
This is just an example of what i wanna achieve.. can u gimme some logic or possibly a pseudo code? Thanks for the response
7th Jan 2017, 11:00 PM
PYTHONISTA
PYTHONISTA - avatar
+ 1
# if your data is a group of lists # experiment with zip. # it will give you the # equivilent grouping of the column values. # quickly. a = [1,2,3,4,5] b = [4,0,4,2,7] c = [3,5,6,7,8] for ind,i in enumerate(zip(a,b,c)): print('max diff in a column',ind,' is ',(abs(max(i)-min(i))))
8th Jan 2017, 8:54 AM
richard
0
Loop through the dataset, one column at a time, tracking the min and max value for that column. After finishing each column, calculate the range of the column from (max - min). Finally, as moving from column to column, track the index of the column with the largest range.
7th Jan 2017, 11:47 PM
Nathan
Nathan - avatar
0
pseudo-code showing algorithm logic: int FindColMaxRange(rowA, rowB, rowC) int colWithMax = -1 double maxRange = 0 FOR col = 0 TO (rowA.Length() - 1) colMin = rowA[col] colMax = rowA[col] IF rowB[col] < colMin colMin = rowB[col] ENDIF IF rowC[col] < colMin colMin = rowC[col] ENDIF // Then do the same to update max colRange = colMax - colMin IF colRange > maxRange colWithMax = col maxRange = colRange ENDIF ENDFOR return colWithMax
8th Jan 2017, 1:46 PM
Nathan
Nathan - avatar
0
this is good
9th Jan 2017, 11:40 AM
Ivan_Ivanov
Ivan_Ivanov - avatar