Maximum difference between adjacent elements of list

From Rosetta Code
Revision as of 19:45, 18 July 2021 by rosettacode>Lscrd (Removed extra lines.)
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

ALGOL 68

<lang algol68>BEGIN # find the maximum difference between consecutive list elements #

   # returns the maximum difference between pairs of elements of list        #
   OP   MAXDELTA = ( []REAL list )REAL:
        BEGIN
           REAL max diff := 0;
           FOR i FROM LWB list TO UPB list - 1 DO
               REAL delta = ABS ( list[ i ] - list[ i + 1 ] );
               IF delta > max diff THEN
                   max diff := delta # found a higher difference             #
               FI
           OD;
           max diff
        END; # MAXDELTA #
   # returns r converted to a string with trailing 0 decimal places removed  #
   OP   TOSTRING = ( REAL r )STRING:
        BEGIN
           STRING result  := fixed( r, 0, 10 );
           IF result[ LWB result ] = "." THEN result := "0" + result FI;
           INT    r end   := UPB result;
           INT    r start := LWB result;
           WHILE IF r end <= r start THEN FALSE ELSE result[ r end ] = "0" FI DO
              r end -:= 1
           OD;
           IF r end > LWB result THEN
               # if all decimal places were "0", remove the "." as well      #
               IF result[ r end ] = "." THEN r end -:= 1 FI
           FI;
           result[ r start : r end ]
        END; # TOSTRING #
   # test the MAXDELTA operator                                              #
   PROC test max delta = ( []REAL list )VOID:
        BEGIN
           REAL max diff = MAXDELTA list;
           print( ( "Max difference [" ) );
           FOR i FROM LWB list TO UPB list DO
               print( ( " ", TOSTRING list[ i ] ) )
           OD;
           print( ( " ] is ", TOSTRING max diff, ":",  newline ) );
           FOR i FROM LWB list TO UPB list - 1 DO
               IF REAL diff = ABS ( list[ i ] - list[ i + 1 ] );
                  diff = max diff
               THEN
                   print( ( "    ", TOSTRING list[ i ], ", ", TOSTRING list[ i + 1 ]
                          , " ==> ", TOSTRING diff
                          , newline
                          )
                        )
               FI
           OD
        END; # test max delta #
   # task test case                                                          #
   test max delta( ( 1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8, 6, 2, 9, 11, 10, 3 ) );
   # additional test cases                                                   #
   test max delta( ( -1.1, -1.2 ) ); test max delta( ( pi, pi, pi ) );
   test max delta( ( )            ); test max delta( ( 776.591 )    )

END</lang>

Output:
Max difference [ 1 8 2 -3 0 1 1 -2.3 0 5.5 8 6 2 9 11 10 3 ] is 7:
    1, 8 ==> 7
    2, 9 ==> 7
    10, 3 ==> 7
Max difference [ -1.1 -1.2 ] is 0.1:
    -1.1, -1.2 ==> 0.1
Max difference [ 3.1415926536 3.1415926536 3.1415926536 ] is 0:
    3.1415926536, 3.1415926536 ==> 0
    3.1415926536, 3.1415926536 ==> 0
Max difference [ ] is 0:
Max difference [ 776.591 ] is 0:

ALGOL W

<lang algolw>begin % find the maximum difference between consecutive list elements  %

   % returns the maximum difference between pairs of elements of list        %
   %         the lower and upper bounds of list must be specified in lb & ub %
   real procedure maxDelta ( real    array list ( * )
                           ; integer value lb, ub
                           ) ;
   begin
       real maxDiff;
       maxDiff := 0;
       for i := lb until ub - 1 do begin
           real delta;
           delta := abs ( list( i ) - list( i + 1 ) );
           if delta > maxDiff then maxDiff := delta % found a higher difference %
       end for_i;
       maxDiff
   end maxDelta;
   % test the maxDelta procedure                                             %
   procedure testMaxDelta ( real    array list ( * )
                          ; integer value lb, ub, rw, dp
                          ) ;
   begin
       real maxDiff;
       maxDiff := maxDelta( list, lb, ub );
       write( s_w := 0, "Max difference [" );
       for i := lb until ub do begin
           writeon( r_format := "A", r_w := rw, r_d := dp, s_w := 0, " ", list( i ) );
       end for_i;
       writeon( r_format := "A", r_w := rw, r_d := dp, s_w := 0, " ] is ", maxDiff, ":" );
       for i := lb until ub - 1 do begin
           real diff;
           diff := abs ( list( i ) - list( i + 1 ) );
           if diff = maxDiff then begin
               write( r_format := "A", r_w := rw, r_d := dp, s_w := 0, "    ", list( i ), ", ", list( i + 1 ), " ==> ", diff );
           end if_diff_eq_maxDiff
        end for_i
   end testMaxDelta;
   begin % task test case                                                    %
       integer tPos;
       real array tc( 1 :: 17 );
       tPos := 0;
       for i := 1, 8, 2, -3, 0, 1, 1, -23, 0, 55, 8, 6, 2, 9, 11, 10, 3 do begin
           tPos       := tPos + 1;
           tc( tPos ) := i
       end for_i;
       tc( 8 ) := -2.3; tc( 10 ) := 5.5;
       testMaxDelta( tc, 1, 17, 4, 1 )
   end;
   begin % additional test cases                                             %
       integer tPos;
       real array tc( 1 :: 3 );
       tc( 1 ) := -1.1; tc( 2 ) := -1.2; testMaxDelta( tc, 1, 2, 4, 1  );
       for i := 1 until 3 do tc( i ) := pi;
       testMaxDelta( tc, 1, 3, 6, 4 );
       testMaxDelta( tc, 1, 0, 3, 1 );
       tc( 1 ) := 776.591;
       testMaxDelta( tc, 1, 1, 6, 3 )
   end

end.</lang>

Output:
Max difference [  1.0  8.0  2.0 -2.9  0.0  1.0  1.0 -2.2  0.0  5.5  8.0  6.0  2.0  9.0 11.0 10.0  3.0 ] is  7.0:
     1.0,  8.0 ==>  7.0
     2.0,  9.0 ==>  7.0
    10.0,  3.0 ==>  7.0
Max difference [ -1.0 -1.1 ] is  0.1:
    -1.0, -1.1 ==>  0.1
Max difference [ 3.1416 3.1416 3.1416 ] is 0.0000:
    3.1416, 3.1416 ==> 0.0000
    3.1416, 3.1416 ==> 0.0000
Max difference [ ] is 0.0:
Max difference [ 776.591 ] is  0.000:

Go

<lang go>package main

import (

   "fmt"
   "math"

)

func main() {

   list := []float64{1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8, 6, 2, 9, 11, 10, 3}
   maxDiff := -1.0
   var maxPairs [][2]float64
   for i := 1; i < len(list); i++ {
       diff := math.Abs(list[i-1] - list[i])
       if diff > maxDiff {
           maxDiff = diff
           maxPairs = [][2]float64{{list[i-1], list[i]}}
       } else if diff == maxDiff {
           maxPairs = append(maxPairs, [2]float64{list[i-1], list[i]})
       }
   }
   fmt.Println("The maximum difference between adjacent pairs of the list is:", maxDiff)
   fmt.Println("The pairs with this difference are:", maxPairs)

}</lang>

Output:
The maximum difference between adjacent pairs of the list is: 7
The pairs with this difference are: [[1 8] [2 9] [10 3]]

Haskell

<lang haskell>import Data.List (maximumBy) import Data.Ord (comparing)

maxDeltas :: [Float] -> [(Float, (Float, Float))] maxDeltas xs = filter ((delta ==) . fst) pairs

 where
   pairs =
     zipWith
       (\a b -> (abs (a - b), (a, b)))
       xs
       (tail xs)
   delta = fst $ maximumBy (comparing fst) pairs

main :: IO () main =

 mapM_ print $
   maxDeltas [1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8, 6, 2, 9, 11, 10, 3]</lang>
Output:
(7.0,(1.0,8.0))
(7.0,(2.0,9.0))
(7.0,(10.0,3.0))

Nim

<lang Nim>import strformat, strutils

type Pair = (float, float)

func `$`(p: Pair): string = &"({p[0]:g}, {p[1]:g})"

func maxdiff(list: openArray[float]): tuple[diff: float; list: seq[Pair]] =

 assert list.len >= 2
 result = (diff: -1.0, list: @[])
 var prev = list[0]
 for n in list[1..^1]:
   let d = abs(n - prev)
   if d > result.diff:
     result.diff = d
     result.list = @[(prev, n)]
   elif d == result.diff:
     result.list.add (prev, n)
   prev = n

let list = [float 1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8, 6, 2, 9, 11, 10, 3] let (diff, pairs) = list.maxdiff()

echo &"The maximum difference between adjacent pairs of the list is: {diff:g}" echo "The pairs with this difference are: ", pairs.join(" ")</lang>

Output:
The maximum difference between adjacent pairs of the list is: 7
The pairs with this difference are: (1, 8) (2, 9) (10, 3)

Phix

procedure maxdiff(sequence s)
    sequence d = sq_abs(sq_sub(s[1..-2],s[2..-1]))
    atom m = max(d)
    sequence p = find_all(m,d)
    for i=1 to length(p) do
        integer pi = p[i], pj = pi+1
        p[i] = sprintf("s[%d..%d]=%V",{pi,pj,s[pi..pj]})
    end for
    printf(1,"max difference is %g, occurring at %s\n",{m,join(p,", ")})
end procedure
maxdiff({1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3})
Output:
max difference is 7, occurring at s[1..2]={1,8}, s[13..14]={2,9}, s[16..17]={10,3}

Python

<lang python>Maximum difference between adjacent numbers in list


  1. maxDeltas [Float] -> [(Float, (Float, Float))]

def maxDeltas(ns):

   Each of the maximally differing successive pairs
      in ns, each preceded by the value of the difference.
   
   pairs = [
       (abs(a - b), (a, b)) for a, b
       in zip(ns, ns[1:])
   ]
   delta = max(pairs, key=lambda ab: ab[0])[0]
   return [
       ab for ab in pairs
       if delta == ab[0]
   ]


  1. ------------------------- TEST -------------------------
  2. main :: IO ()

def main():

   Each of the maximally differing pairs in a list
   maxPairs = maxDeltas([
       1, 8, 2, -3, 0, 1, 1, -2.3, 0,
       5.5, 8, 6, 2, 9, 11, 10, 3
   ])
   for ab in maxPairs:
       print(ab)


  1. MAIN ---

if __name__ == '__main__':

   main()</lang>
Output:
(7, (1, 8))
(7, (2, 9))
(7, (10, 3))

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...

Wren

<lang ecmascript>var list = [1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8,6, 2, 9, 11, 10, 3] var maxDiff = -1 var maxPairs = [] for (i in 1...list.count) {

   var diff = (list[i-1] - list[i]).abs
   if (diff > maxDiff) {
       maxDiff = diff
       maxPairs = [[list[i-1], list[i]]]
   } else if (diff == maxDiff) {
       maxPairs.add([list[i-1], list[i]])
   }

} System.print("The maximum difference between adjacent pairs of the list is: %(maxDiff)") System.print("The pairs with this difference are: %(maxPairs)")</lang>

Output:
The maximum difference between adjacent pairs of the list is: 7
The pairs with this difference are: [[1, 8], [2, 9], [10, 3]]

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