+ 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.
7 odpowiedzi
+ 2
Your example specifies 3 set objects with 4 members each, but sets do not support [] indexing.
+ 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 ;)
+ 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
+ 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))))
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.
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
0
this is good