Maximum difference between adjacent elements of list: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(→‎{{header|XPL0}}: Revised for new requirements.)
Line 80: Line 80:
<lang XPL0>real List, Dist, MaxDist;
<lang XPL0>real List, Dist, MaxDist;
int I;
int I;
[List:= [2., -3., 0., 1., 1., -2.3, 0., 5.5, 8., 6., 2., 9., 11., 8., 1.];
[List:= [1., 8., 2., -3., 0., 1., 1., -2.3, 0., 5.5, 8., 6., 2., 9., 11., 10., 3.];
MaxDist:= 0.;
MaxDist:= 0.;
for I:= 0 to 15-2 do
for I:= 0 to 17-2 do
[Dist:= abs(List(I) - List(I+1));
[Dist:= abs(List(I) - List(I+1));
if Dist > MaxDist then
if Dist > MaxDist then
MaxDist:= Dist;
MaxDist:= Dist;
];
];
RlOut(0, MaxDist);
Format(1, 0);
for I:= 0 to 17-2 do
[Dist:= abs(List(I) - List(I+1));
if Dist = MaxDist then
[RlOut(0, List(I)); Text(0, ", "); RlOut(0, List(I+1));
Text(0, " ==> "); RlOut(0, MaxDist); CrLf(0);
];
];
]</lang>
]</lang>


{{out}}
{{out}}
<pre>
<pre>
7.00000
1, 8 ==> 7
2, 9 ==> 7
10, 3 ==> 7
</pre>
</pre>

Revision as of 15:26, 18 July 2021

Maximum difference between adjacent elements of list is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task
Find maximum difference between adjacent elements of list.


The list may have a negative value, a zero value, a real number.
List = [1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3]
Output would be:
2,9 ==> 7
1,8 ==> 7
10,3 ==> 7

Ring

<lang ring> see "working..." + nl strList = "[1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3]" see "Maximum difference between adjacent elements of list is:" + nl + nl see "Input list = " + strList + nl + nl see "Output:" + nl sList = [1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3] sortList = []

for n = 1 to len(sList)-1

   diff = fabs(sList[n]-sList[n+1])
   oldDiff = diff
   first = sList[n]
   second = sList[n+1]
   add(sortList,[oldDiff,first,second])

next

sortList = sort(sortlist,1) sortList = reverse(sortlist) flag = 1

for n=1 to len(sortList)-1

   oldDiff1 = sortlist[n][1]
   oldDiff2 = sortlist[n+1][1] 
   first1 = sortlist[n][2]
   second1 = sortlist[n][3]
   first2 = sortlist[n+1][2]
   second2 = sortlist[n+1][3]
   if n = 1 and oldDiff1 != oldDiff2
      see "" + first1 + "," + second1 + " ==> " + oldDiff1 + nl 
   ok
   if oldDiff1 = oldDiff2
      if flag = 1
         flag = 0
         see "" + first1 + "," + second1 + " ==> " + oldDiff1 + nl   
         see "" + first2 + "," + second2 + " ==> " + oldDiff2 + nl
      else
         see "" + first2 + "," + second2 + " ==> " + oldDiff2 + nl
      ok
   else
      exit
   ok

next

see "done..." + nl </lang>

Output:
working...
Maximum difference between adjacent elements of list is:

Input list = [1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3]

Output:
2,9 ==> 7
1,8 ==> 7
10,3 ==> 7
done...

XPL0

<lang XPL0>real List, Dist, MaxDist; int I; [List:= [1., 8., 2., -3., 0., 1., 1., -2.3, 0., 5.5, 8., 6., 2., 9., 11., 10., 3.]; MaxDist:= 0.; for I:= 0 to 17-2 do

   [Dist:= abs(List(I) - List(I+1));
   if Dist > MaxDist then
       MaxDist:= Dist;
   ];

Format(1, 0); for I:= 0 to 17-2 do

   [Dist:= abs(List(I) - List(I+1));
   if Dist = MaxDist then
       [RlOut(0, List(I));  Text(0, ", ");  RlOut(0, List(I+1));
       Text(0, " ==> ");  RlOut(0, MaxDist);  CrLf(0);
       ];
   ];

]</lang>

Output:
1, 8 ==> 7
2, 9 ==> 7
10, 3 ==> 7