Maximum difference between adjacent elements of list: Difference between revisions

no edit summary
(→‎{{header|REXX}}: added the computer programming language REXX.)
No edit summary
 
(49 intermediate revisions by 33 users not shown)
Line 4:
Find maximum difference between adjacent elements of list.
 
The list may have a negative valuevalues, a zero valuevalues, a real numbernumbers.
 
 
List &nbsp; = &nbsp; <big> [1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3] </big>
 
 
Output would be &nbsp; (which could have more verbiage):
1,8 ==> 7
2,9 ==> 7
10,3 ==> 7
<br><br>
 
=={{header|11l}}==
2,9 ==> 7
{{trans|Python}}
<syntaxhighlight lang="11l">F maxDeltas(ns)
V pairs = zip(ns, ns[1..]).map((a, b) -> (abs(a - b), (a, b)))
V delta = max(pairs, key' ab -> ab[0])[0]
R pairs.filter(ab -> @delta == ab[0])
 
V maxPairs = maxDeltas([Float(1), 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8, 6, 2, 9, 11, 10, 3])
 
L(ab) maxPairs
print(ab)</syntaxhighlight>
 
{{out}}
<pre>
(7, (1, 8))
(7, (2, 9))
(7, (10, 3))
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<syntaxhighlight lang="action!">INCLUDE "H6:REALMATH.ACT"
 
DEFINE PTR="CARD"
 
PTR FUNC GetPtr(BYTE ARRAY a INT index)
RETURN (a+index*6)
 
PROC MaxDifference(BYTE ARRAY a INT len REAL POINTER maxDiff)
INT i
REAL diff
REAL POINTER r1,r2
 
IntToReal(0,maxDiff)
FOR i=0 TO len-2
DO
r1=GetPtr(a,i)
r2=GetPtr(a,i+1)
RealAbsDiff(r1,r2,diff)
IF RealGreaterOrEqual(diff,maxDiff) THEN
RealAssign(diff,maxDiff)
FI
OD
RETURN
 
PROC PrintPairs(BYTE ARRAY a INT len,maxDiff)
INT i
REAL diff
REAL POINTER r1,r2
 
FOR i=0 TO len-2
DO
r1=GetPtr(a,i)
r2=GetPtr(a,i+1)
RealAbsDiff(r1,r2,diff)
IF RealEqual(diff,maxDiff) THEN
Print(" AbsDiff(") PrintR(r1)
Print(", ") PrintR(r2)
Print(") = ") PrintRE(maxDiff)
FI
OD
RETURN
 
PROC Decode(CHAR ARRAY s BYTE ARRAY a INT POINTER len)
BYTE i,start,length
CHAR ARRAY item,tmp(10)
REAL POINTER r
 
len^=0 i=1
WHILE i<=s(0)
DO
start=i
WHILE i<=s(0) AND s(i)#32
DO
i==+1
OD
length=i-start
SCopyS(tmp,s,start,i-1)
r=GetPtr(a,len^)
ValR(tmp,r)
len^==+1
i==+1
OD
RETURN
 
PROC Test(CHAR ARRAY s)
BYTE ARRAY a(200)
INT len
REAL maxDiff
 
PrintF("Max difference in [%S] is ",s)
Decode(s,a,@len)
MaxDifference(a,len,maxDiff)
PrintRE(maxDiff)
PrintPairs(a,len,maxDiff) PutE()
RETURN
 
PROC Main()
Put(125) PutE() ;clear the screen
 
Test("1 8 2 -3 0 1 1 -2.3 0 5.5 8 6 2 9 11 10 3")
Test("0.12 -0.04 0.34 0.72 0.34 0.27 -0.1")
Test("10E30 20E30 15E30 5E30 -5E30")
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Maximum_difference_between_adjacent_elements_of_list.png Screenshot from Atari 8-bit computer]
<pre>
Max difference in [1 8 2 -3 0 1 1 -2.3 0 5.5 8 6 2 9 11 10 3] is 7
AbsDiff(1, 8) = 7
AbsDiff(2, 9) = 7
AbsDiff(10, 3) = 7
 
Max difference in [0.12 -0.04 0.34 0.72 0.34 0.27 -0.1] is .38
AbsDiff(-0.04, .34) = .38
AbsDiff(.34, .72) = .38
AbsDiff(.72, .34) = .38
 
Max difference in [10E30 20E30 15E30 5E30 -5E30] is 1.0E+31
AbsDiff(1.0E+31, 2.0E+31) = 1.0E+31
AbsDiff(1.5E+31, 5E+30) = 1.0E+31
AbsDiff(5E+30, -5E+30) = 1.0E+31
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="algol68">
-- Find the maximum absolute difference between adjacent values in a list, and output all pairs with that difference
-- J. Carter 2023 Apr
-- Uses the PragmAda Reusable Components (https://github.com/jrcarter/PragmARC)
 
with Ada.Text_IO;
with PragmARC.Images;
 
procedure List_Max_Diff is
function Float_Image is new PragmARC.Images.Float_Image (Number => Float);
function Image (Number : in Float) return String;
-- Returns the image of Number without a decimal point or fractional digits if the fractional part is zero
function Image (Number : in Float) return String is
Result : constant String := Float_Image (Number, Aft => 0, Exp => 0);
begin -- Image
if Result'Length > 1 and then Result (Result'Last - 1 .. Result'Last) = ".0" then
return Result (Result'First .. Result'Last - 2);
end if;
return Result;
end Image;
type Float_List is array (Positive range <>) of Float with Dynamic_Predicate => Float_List'First = 1;
List : constant Float_List := (1.0, 8.0, 2.0, -3.0, 0.0, 1.0, 1.0, -2.3, 0.0, 5.5, 8.0, 6.0, 2.0, 9.0, 11.0, 10.0, 3.0);
Max : Float := -1.0;
begin -- List_Max_Diff
Find : for I in 1 .. List'Last - 1 loop
Max := Float'Max (abs (List (I) - List (I + 1) ), Max);
end loop Find;
Print : for I in 1 .. List'Last - 1 loop
if abs (List (I) - List (I + 1) ) = Max then
Ada.Text_IO.Put_Line (Item => Image (List (I) ) & ',' & Image (List (I + 1) ) & " ==> " & Image (Max) );
end if;
end loop Print;
end List_Max_Diff;
</syntaxhighlight>
 
{{out}}
<pre>
1,8 ==> 7
2,9 ==> 7
10,3 ==> 7
<br><br/pre>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight 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:
Line 73 ⟶ 245:
test max delta( ( -1.1, -1.2 ) ); test max delta( ( pi, pi, pi ) );
test max delta( ( ) ); test max delta( ( 776.591 ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 90 ⟶ 262:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight 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 %
Line 147 ⟶ 319:
testMaxDelta( tc, 1, 1, 6, 3 )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 161 ⟶ 333:
Max difference [ ] is 0.0:
Max difference [ 776.591 ] is 0.000:
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">maxdiff ← (⊢((⌈/⊢),⊣(⌿⍨)⊢=(⌈/⊢))(|-)/)∘(↑2,/⊢)</syntaxhighlight>
{{out}}
<pre> maxdiff 1 8 2 ¯3 0 1 1 ¯2.3 0 5.5 8 6 2 9 11 10 3
7 1 8
7 2 9
7 10 3</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">maximumAdjacentDifference: function [L][
result: [[[] 0]]
 
loop 0..(size L)-2 'a [
diff: abs (y: <= L\[a+1]) - (x: <= L\[a])
 
if diff >= last maximum result => last ->
result: (select result 'x -> diff = last x) ++ @[@[@[x,y], diff]]
]
maxLen: result\0\1
result: @[unique map result 'x -> first x, maxLen]
return result
]
 
lst: @[1,8,2,neg 3,0,1,1,neg 2.3,0,5.5,8,6,2,9,11,10,3]
 
diffs: maximumAdjacentDifference lst
 
loop diffs\0 'd ->
print [d "->" diffs\1]</syntaxhighlight>
 
{{out}}
 
<pre>[1 8] -> 7
[2 9] -> 7
[10 3] -> 7</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">List := [1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3]
mxDiff := []
 
loop % List.Count() -1
mxDiff[d := Abs(list[A_Index+1] - list[A_Index])] .= list[A_Index] ", " list[A_Index+1] " ==> " d "`n"
 
MsgBox % result := Trim(mxDiff[mxDiff.MaxIndex()], "`n")</syntaxhighlight>
{{out}}
<pre>1, 8 ==> 7
2, 9 ==> 7
10, 3 ==> 7</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f MAXIMUM_DIFFERENCE_BETWEEN_ADJACENT_ELEMENTS_OF_LIST.AWK
BEGIN {
list = "1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3"
n = split(list,arr,",")
for (i=1; i<n; i++) {
a = arr[i]
b = arr[i+1]
if (abs(a-b) == highest) {
sets = sprintf("%s%s,%s ",sets,a,b)
}
else if (abs(a-b) > highest) {
highest = abs(a-b)
sets = sprintf("%s,%s ",a,b)
}
}
printf("%s: %s\n",highest,sets)
exit(0)
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
</syntaxhighlight>
{{out}}
<pre>
7: 1,8 2,9 10,3
</pre>
 
=={{header|BASIC}}==
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">maxdist = 0
DIM lista(0 TO 16)
FOR i = 0 TO UBOUND(lista)
READ lista(i)
NEXT i
 
PRINT "Maximum difference between adjacent elements of lista is:"
FOR i = 0 TO UBOUND(lista) - 1
dist = ABS(lista(i) - lista(i + 1))
IF dist > maxdist THEN maxdist = dist
NEXT i
 
FOR i = 0 TO UBOUND(lista) - 1
dist = ABS(lista(i) - lista(i + 1))
IF dist = maxdist THEN PRINT lista(i); ", "; lista(i + 1); " ==> "; maxdist
NEXT i
 
DATA 1., 8., 2., -3., 0., 1., 1., -2.3, 0., 5.5, 8., 6., 2., 9., 11., 10., 3.</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">arraybase 1
maxdist = 0
dim lista = {1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8, 6, 2, 9, 11, 10, 3}
 
print "Maximum difference between adjacent elements of lista is:"
for i = 1 to lista[?]-1
dist = abs(lista[i] - lista[i+1])
if dist > maxdist then maxdist = dist
next i
 
for i = 1 to lista[?]-1
dist = abs(lista[i] - lista[i+1])
if dist = maxdist then print lista[i] & ", " & lista[i+1] & " ==> " & maxdist
next i</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">LET maxdist = 0
DIM lista(0 TO 16)
FOR i = 0 TO UBOUND(lista)
READ lista(i)
NEXT i
PRINT "Maximum difference between adjacent elements of lista is:"
FOR i = 0 TO UBOUND(lista)-1
LET dist = ABS(lista(i)-lista(i+1))
IF dist > maxdist THEN LET maxdist = dist
NEXT i
FOR i = 0 TO UBOUND(lista)-1
LET dist = ABS(lista(i)-lista(i+1))
IF dist = maxdist THEN PRINT lista(i); ", "; lista(i+1); " ==> "; maxdist
NEXT i
DATA 1., 8., 2., -3., 0., 1., 1., -2.3, 0., 5.5, 8., 6., 2., 9., 11., 10., 3.
END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">maxdist = 0
dim lista(16)
lista(0) = 1 : lista(1) = 8
lista(2) = 2 : lista(3) = -3
lista(4) = 0 : lista(5) = 1
lista(6) = 1 : lista(7) = -2.3
lista(8) = 0 : lista(9) = 5.5
lista(10) = 8 : lista(11) = 6
lista(12) = 2 : lista(13) = 9
lista(14) = 11 : lista(15) = 10
lista(16) = 3
 
print "Maximum difference between adjacent elements of lista is:"
for i = 0 to arraysize(lista(),1)-1
dist = abs(lista(i) - lista(i+1))
if dist > maxdist maxdist = dist
next i
 
for i = 0 to arraysize(lista(),1)-1
dist = abs(lista(i) - lista(i+1))
if dist = maxdist print lista(i), ", ", lista(i+1), " ==> ", maxdist
next i</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|C}}==
{{trans|Wren}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
typedef struct {
double x;
double y;
} pair;
 
int main() {
double list[17] = {1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8,6, 2, 9, 11, 10, 3};
double diff, maxDiff = -1;
pair maxPairs[5];
int i, count = 0;
for (i = 1; i < 17; ++i) {
diff = fabs(list[i-1] - list[i]);
if (diff > maxDiff) {
maxDiff = diff;
maxPairs[count++] = (pair){list[i-1], list[i]};
} else if (diff == maxDiff) {
maxPairs[count++] = (pair){list[i-1], list[i]};
}
}
printf("The maximum difference between adjacent pairs of the list is: %g\n", maxDiff);
printf("The pairs with this difference are: ");
for (i = 0; i < count; ++i) printf("[%g, %g] ", maxPairs[i].x, maxPairs[i].y);
printf("\n");
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
The maximum difference between adjacent pairs of the list is: 7
The pairs with this difference are: [1, 8] [2, 9] [10, 3]
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">#include <iostream>
#include <iterator>
#include <vector>
#include <utility>
#include <cstdlib>
#include <algorithm>
 
//supposing example numbers are entered!
int main( ) {
std::cout << "Enter some doubles, separated by blanks, e to end!\n" ;
std::vector<double> numbers { std::istream_iterator<double>{ std::cin } ,
std::istream_iterator<double>{} } ;
std::vector<std::pair<int , int>> neighbours ;
for ( int i = 0 ; i < numbers.size( ) - 1 ; i++ )
neighbours.push_back( std::make_pair( numbers[i] , numbers[ i + 1 ] )) ;
std::sort( neighbours.begin( ) , neighbours.end( ) , []( auto p1 , auto p2 ) {
return std::abs( p1.first - p1.second ) > std::abs( p2.first - p2.second )
; } ) ;
double maximum = std::abs( neighbours[0].first - neighbours[0].second ) ;
int pos = 0 ;
while ( std::abs(neighbours[pos].first - neighbours[pos].second ) == maximum ) {
std::cout << neighbours[pos].first << ',' << neighbours[pos].second <<
" ==> " << maximum << '\n' ;
pos++ ;
}
return 0 ;
}</syntaxhighlight>
{{out}}
<pre>
1,8 ==> 7
2,9 ==> 7
10,3 ==> 7
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
{Data type to hold difference info}
 
type TDiffInfo = record
Diff,N1,N2: double;
end;
 
{Array of different info}
 
type TDiffArray = array of TDiffInfo;
 
procedure FindMaximumDifference(List: array of double; var DiffArray: TDiffArray);
{Analyze data and return list of differnces}
var I: integer;
var Diff,MaxDiff: double;
begin
MaxDiff:=0;
for I:=0 to High(List)-1 do
begin
Diff:=Abs(List[I+1]-List[I]);
if Diff>=MaxDiff then
begin
MaxDiff:=Diff;
SetLength(DiffArray,Length(DiffArray)+1);
DiffArray[High(DiffArray)].Diff:=MaxDiff;
DiffArray[High(DiffArray)].N1:=List[I];
DiffArray[High(DiffArray)].N2:=List[I+1];
end
end;
end;
 
procedure MaximumAdjacentDifference(Memo: TMemo);
var DA: TDiffArray;
var I: integer;
begin
FindMaximumDifference([1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3],DA);
for I:=0 to High(DA) do
begin
Memo.Lines.Add(Format('abs(%4.1f - %4.1f) = %4.1f',[DA[I].N1,DA[I].N2,DA[I].Diff]));
end;
end;
 
</syntaxhighlight>
{{out}}
<pre>
abs( 1.0 - 8.0) = 7.0
abs( 2.0 - 9.0) = 7.0
abs(10.0 - 3.0) = 7.0
 
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
list[] = [ 1 8 2 -3 0 1 1 -2.3 0 5.5 8 6 2 9 11 10 3 ]
for i = 2 to len list[]
dist = abs (list[i - 1] - list[i])
max = higher dist max
.
for i = 2 to len list[]
if abs (list[i - 1] - list[i]) = max
print list[i - 1] & "," & list[i] & " ==> " & dist
.
.
</syntaxhighlight>
{{out}}
<pre>
1,8 ==> 7
2,9 ==> 7
10,3 ==> 7
</pre>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Max diff 'twix adj elems. Nigel Galloway: July 18th., 2021
let n,g=[1;8;2;-3;0;1;1;-2;3;0;5;5;8;6;2;9;11;10;3]|>List.pairwise|>List.groupBy(fun(n,g)->abs(n-g))|>List.maxBy fst in printfn "Pairs %A have the max diff of %d" g n
</syntaxhighlight>
{{out}}
<pre>
Pairs [(1, 8); (2, 9); (10, 3)] have the max diff of 7
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: assocs grouping math prettyprint sequences ;
 
: max-diff ( seq -- assoc )
2 clump [ first2 - abs ] collect-by >alist supremum ;
 
{ 1 8 2 -3 0 1 1 -2.3 0 5.5 8 6 2 9 11 10 3 } max-diff .</langsyntaxhighlight>
{{out}}
<pre>
{ 7 V{ { 1 8 } { 2 9 } { 10 3 } } }
</pre>
 
=={{header|Free Pascal}}==
{{trans|Pascal}}
<syntaxhighlight lang="pascal">
program maximumDifferenceBetweenAdjacentElementsOfList(output);
type
tmyReal = extended;
tmyList = array of tmyReal;
const
input: tmyList = (1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8, 6, 2, 9, 11, 10, 3);
procedure OutSameDistanceInList(Dist:tmyReal;const list: tmyList);
var
currentDistance : tmyReal;
i : integer;
begin
for i := 0 to High(List) - 1 do
begin
currentDistance := abs(list[i] - list[succ(i)]);
if currentDistance = dist then
writeln('idx : ',i:4,' values ',list[i],' ',list[i+1]);
end;
writeln;
end;
 
function internalDistance(const list: tmyList): tmyReal;
var
i: integer;
maximumDistance, currentDistance: tmyReal;
begin
maximumDistance := 0.0;
for i := 0 to High(List) - 1 do
begin
currentDistance := abs(list[i] - list[succ(i)]);
if currentDistance > maximumDistance then
begin
maximumDistance := currentDistance;
end;
end;
internalDistance := maximumDistance;
end;
 
var
list: tmyList;
foundDistance : tmyReal;
begin
list := input;
if length(list) > 0 then
Begin
foundDistance := internalDistance(list);
OutSameDistanceInList(foundDistance,list);
end;
{$IFDEF WINDOWS}
readln;
{$ENDIF}
end.</syntaxhighlight>
{{out}}
<pre>
idx : 0 values 1.0000000000000000E+000 8.0000000000000000E+000
idx : 12 values 2.0000000000000000E+000 9.0000000000000000E+000
idx : 15 values 1.0000000000000000E+001 3.0000000000000000E+000 </pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Dim As Single Dist, MaxDist = 0
Dim As Single List(16) => {1., 8., 2., -3., 0., 1., 1., -2.3, 0., 5.5, 8., 6., 2., 9., 11., 10., 3.}
 
Print !"Maximum difference between adjacent elements of list is:"
For i As Integer = 0 To Ubound(List)
Dist = Abs(List(i) - List(i+1))
If Dist > MaxDist Then MaxDist = Dist
Next i
 
For i As Integer = 0 To Ubound(List)
Dist = Abs(List(i) - List(i+1))
If Dist = MaxDist Then Print List(i) & ", " & List(i+1) & " ==> " & MaxDist
Next i</syntaxhighlight>
{{out}}
<pre>Maximum difference between adjacent elements of lista is:
1, 8 ==> 7
2, 9 ==> 7
10, 3 ==> 7</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 199 ⟶ 777:
fmt.Println("The maximum difference between adjacent pairs of the list is:", maxDiff)
fmt.Println("The pairs with this difference are:", maxPairs)
}</langsyntaxhighlight>
 
{{out}}
Line 208 ⟶ 786:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (maximumBy)
import Data.Ord (comparing)
 
maxDeltas :: (Num a, Ord a) => [Floata] -> [(Floata, (Floata, Floata))]
maxDeltas xs = filter ((delta ==) . fst) pairs
where
Line 224 ⟶ 802:
main =
mapM_ print $
maxDeltas [1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8, 6, 2, 9, 11, 10, 3]</langsyntaxhighlight>
{{Out}}
<pre>(7.0,(1.0,8.0))
(7.0,(2.0,9.0))
(7.0,(10.0,3.0))</pre>
 
=={{header|J}}==
<syntaxhighlight lang="j">maxdiff=: [: (,.|@-/"1) 2(,/\ (#~ (=>./)@:|) -/\) ]</syntaxhighlight>
{{out}}
<pre> maxdiff 1 8 2 _3 0 1 1 _2.3 0 5.5 8 6 2 9 11 10 3
1 8 7
2 9 7
10 3 7
(}:,&":' ==> ',&":{:)"1 maxdiff 1 8 2 _3 0 1 1 _2.3 0 5.5 8 6 2 9 11 10 3
1 8 ==> 7
2 9 ==> 7
10 3 ==> 7</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">def maximally_different_adjacent_pairs:
. as $in
| reduce range(1; length) as $i ({ maxDiff: -1};
(($in[$i-1] - $in[$i])|length) as $diff # length being abs
| if $diff > .maxDiff
then .maxDiff = $diff
| .maxPairs = [[$in[$i-1], $in[$i]]]
elif $diff == .maxDiff
then .maxPairs += [[$in[$i-1], $in[$i]]]
else .
end
);
 
# Example:
[1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8,6, 2, 9, 11, 10, 3]
| maximally_different_adjacent_pairs
</syntaxhighlight>
{{out}}
<pre>
{"maxDiff":7,"maxPairs":[[1,8],[2,9],[10,3]]}
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
function maxadjacentdiffs(list)
pairs, maxδ = Pair{eltype(list)}[], abs(zero(eltype(list)))
for i in eachindex(list[begin:end-1])
x, y = list[i], list[i + 1]
if (δ = abs(x - y)) == maxδ
push!(pairs, x => y)
elseif δ > maxδ
maxδ, pairs = δ, [x => y]
end
end
return maxδ, pairs
end
 
diff, pairs = maxadjacentdiffs(Real[1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3])
 
foreach(p -> println(p[1], ", ", p[2], " ==> ", diff), pairs)
</syntaxhighlight>{{out}}
<pre>
1, 8 ==> 7
2, 9 ==> 7
10, 3 ==> 7
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">l = {1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8, 6, 2, 9, 11, 10, 3};
diff = Abs@Differences[l];
m = Max[diff];
pos = Flatten[Position[diff, m]];
Column[Part[l, # ;; # + 1] -> m & /@ pos]</syntaxhighlight>
{{out}}
<pre>{1,8}->7
{2,9}->7
{10,3}->7</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat, strutils
 
type Pair = (float, float)
Line 254 ⟶ 905:
 
echo &"The maximum difference between adjacent pairs of the list is: {diff:g}"
echo "The pairs with this difference are: ", pairs.join(" ")</langsyntaxhighlight>
 
{{out}}
<pre>The maximum difference between adjacent pairs of the list is: 7
The pairs with this difference are: (1, 8) (2, 9) (10, 3)</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec fold_pairwise f m = function
| a :: (b :: _ as t) -> fold_pairwise f (f m a b) t
| _ -> m
 
let pair_to_str (a, b) =
Printf.sprintf "(%g, %g)" a b
 
let max_diffs =
let next m a b =
match abs_float (b -. a), m with
| d', (d, _) when d' > d -> d', [a, b]
| d', (d, l) when d' = d -> d', (a, b) :: l
| _ -> m
in fold_pairwise next (0., [])
 
let () =
let d, l = max_diffs [1.;8.;2.;-3.;0.;1.;1.;-2.3;0.;5.5;8.;6.;2.;9.;11.;10.;3.] in
List.rev_map pair_to_str l |> String.concat " " |> Printf.printf "%g: %s\n" d</syntaxhighlight>
{{out}}
<pre>7: (1, 8) (2, 9) (10, 3)</pre>
 
=={{header|Pascal}}==
The following <tt>program</tt> needs an ISO 7185 Standard “unextended” Pascal, level&nbsp;1, compliant compiler.
<syntaxhighlight lang="pascal">program maximumDifferenceBetweenAdjacentElementsOfList(output);
 
function internalDistance(
list: array[indexMinimum..indexMaximum: integer] of real
): real;
var
i: integer;
maximumDistance, currentDistance: real;
begin
maximumDistance := 0.0;
for i := indexMinimum to pred(indexMaximum) do
begin
currentDistance := abs(list[i] - list[succ(i)]);
if currentDistance > maximumDistance then
begin
maximumDistance := currentDistance
end
end;
internalDistance := maximumDistance
end;
 
var
list: array[1..2] of real;
begin
list[1] := 1.0; list[2] := 8.0;
writeLn(internalDistance(list));
end.</syntaxhighlight>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
use warnings;
use List::Util qw( reduce max );
 
my @list = (1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3);
 
my %diffs;
reduce { $diffs{ abs $a - $b } .= " $a,$b"; $b } @list;
my $max = max keys %diffs;
print "$_ ==> $max\n" for split ' ', $diffs{ $max };</syntaxhighlight>
{{out}}
<pre>
1,8 ==> 7
2,9 ==> 7
10,3 ==> 7
</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">procedure</span> <span style="color: #000000;">maxdiffdifferences</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=,</span> <span style="color: #7060A8000000;">sq_absmx</span> <span style="color: #0000FF;">(=</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">({</span><span style="color: #000000;">s0</span><span style="color: #0000FF;">[,</span><span style="color: #000000008000;">1</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]},</span> <span style="color: #000000;">smn</span> <span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]))mx</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">mav</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)0</span>
<span style="color: #004080008080;">sequencefunction</span> <span style="color: #000000;">pfall</span> <span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #7060A8000000;">find_allfn</span><span style="color: #0000FF;">(,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">md</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ds</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000004080;">iatom</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1m</span> <span style="color: #0080800000FF;">to=</span> <span style="color: #7060A8000000;">lengthfn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pd</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integersequence</span> <span style="color: #000000;">pip</span> <span style="color: #0000FF;">=</span> <span style="color: #0000007060A8;">pfind_all</span><span style="color: #0000FF;">[(</span><span style="color: #000000;">im</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">pjd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pi</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1)</span>
<span style="color: #000000008080;">pfor</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"s[%d..%d]=%V"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">pi1</span><span style="color: #0000FF;">,</span><span style="color: #000000008080;">pjto</span><span style="color: #0000FF;">,</span><span style="color: #0000007060A8;">slength</span><span style="color: #0000FF;">[(</span><span style="color: #000000;">pip</span><span style="color: #0000FF;">..)</span><span style="color: #000000;">pj</span><span style="color: #0000FF008080;">]})do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">pi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">pj</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pi</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8000000;">printfp</span><span style="color: #0000FF;">([</span><span style="color: #000000;">1i</span><span style="color: #0000FF;">,]</span> <span style="color: #0080000000FF;">"max=</span> difference<span isstyle="color: %g,#7060A8;">sprintf</span><span occurringstyle="color: at#0000FF;">(</span><span %style="color: #008000;">"s\n[%d..%d]=%V"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mpi</span><span style="color: #0000FF;">,</span><span style="color: #7060A8000000;">joinpj</span><span style="color: #0000FF;">(,</span><span style="color: #000000;">ps</span><span style="color: #0000FF;">,[</span><span style="color: #008000000000;">pi</span><span style=",color: #0000FF;">..</span><span style="color: #000000;">pj</span><span style="color: #0000FF;">)]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #008000;">", occurring at "</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)>=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]))</span>
<span style="color: #000000;">mx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fall</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">maxsq</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">mn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fall</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">minsq</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">av</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)/</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"sequence: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"max difference is %g%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mx</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"min difference is %g%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mn</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"avg difference is %.7g\n\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">av</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">maxdiffdifferences</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2.3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5.5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">differences</span><span style="color: #0000FF;">({-</span><span style="color: #000000;">1.1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1.2</span><span style="color: #0000FF;">})</span>
<!--</lang>-->
<span style="color: #000000;">differences</span><span style="color: #0000FF;">({})</span>
<span style="color: #000000;">differences</span><span style="color: #0000FF;">({</span><span style="color: #000000;">776.591</span><span style="color: #0000FF;">})</span>
<!--</syntaxhighlight>-->
{{output}}
<pre>
sequence: {1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3}
max difference is 7, occurring at s[1..2]={1,8}, s[13..14]={2,9}, s[16..17]={10,3}
min difference is 0, occurring at s[6..7]={1,1}
avg difference is 3.6625
 
sequence: {-1.1,-1.2}
max difference is 0.1, occurring at s[1..2]={-1.1,-1.2}
min difference is 0.1, occurring at s[1..2]={-1.1,-1.2}
avg difference is 0.1
 
sequence: {}
max difference is 0
min difference is 0
avg difference is 0
 
sequence: {776.591}
max difference is 0
min difference is 0
avg difference is 0
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">import util.
 
main =>
L = [1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3],
Diffs = findall([Diff,A,B], (nextto(A,B,L), Diff = abs(A-B))),
MaxDiff = max([Diff : [Diff,_,_] in Diffs]),
println(MaxDiff=[[A,B] : [Diff,A,B] in Diffs, Diff == MaxDiff]),
nl.</syntaxhighlight>
 
{{out}}
<pre>7 = [[1,8],[2,9],[10,3]]</pre>
 
Here is an alternative way to get the pairwise differences, using <code>zip/2</code>:
<syntaxhighlight lang="picat"> % ...
Diffs = [[abs(A-B),A,B] : {A,B} in zip(L.butlast,L.tail)],
% ...
 
% Returns all element of L but the last
butlast(L) = L[1..L.len-1].</syntaxhighlight>
 
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">'''Maximum difference between adjacent numbers in list'''
 
 
Line 316 ⟶ 1,098:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>(7, (1, 8))
(7, (2, 9))
(7, (10, 3))</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" line>sub max-diff (*@list) {
return 0 if +@list < 2;
my $max = @list.rotor(2 => -1).max({ (.[0] - .[1]).abs }).map( (* - *).abs )[0];
"$max @ elements { @list.rotor(2 => -1).grep( { (.[0] - .[1]).abs == $max } ).gist }"
}
 
 
sub min-diff (*@list) {
return 0 if +@list < 2;
my $min = @list.rotor(2 => -1).min({ (.[0] - .[1]).abs }).map( (* - *).abs )[0];
"$min @ elements { @list.rotor(2 => -1).grep( { (.[0] - .[1]).abs == $min } ).gist }"
}
 
 
sub avg-diff (*@list) { (+@list > 1) ?? (@list.sum / +@list) !! 0 }
 
 
# TESTING
 
for (
[ 1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3 ]
,[(rand × 1e6) xx 6]
,[ e, i, τ, π, ∞ ]
,[ 1.9+3.7i, 2.07-13.2i, 0.2+-2.2i, 4.6+0i ]
,[ 6 ]
,[]
,[<One Two Three>]
)
-> @list {
say 'List: ', ~ @list.raku;
for (' Maximum', &max-diff ,' Minimum', &min-diff, ' Average', &avg-diff)
-> $which, &sub {
say "$which distance between list elements: " ~ &sub(@list);
}
say '';
}</syntaxhighlight>
{{out}}
<pre>List: [1, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8, 6, 2, 9, 11, 10, 3]
Maximum distance between list elements: 7 @ elements ((1 8) (2 9) (10 3))
Minimum distance between list elements: 0 @ elements ((1 1))
Average distance between list elements: 3.658824
 
List: [480470.3324944039e0, 719954.3173377671e0, 648221.5674277907e0, 340053.78585537826e0, 540629.6741075241e0, 4336.700602958543e0]
Maximum distance between list elements: 536292.9735045655 @ elements ((540629.6741075241 4336.700602958543))
Minimum distance between list elements: 71732.74990997638 @ elements ((719954.3173377671 648221.5674277907))
Average distance between list elements: 455611.06297097047
 
List: [2.718281828459045e0, <0+1i>, 6.283185307179586e0, 3.141592653589793e0, Inf]
Maximum distance between list elements: Inf @ elements ((3.141592653589793 Inf))
Minimum distance between list elements: 2.896386731590008 @ elements ((2.718281828459045 0+1i))
Average distance between list elements: Inf+0.2i
 
List: [<1.9+3.7i>, <2.07-13.2i>, <0.2-2.2i>, <4.6+0i>]
Maximum distance between list elements: 16.900855007957436 @ elements ((1.9+3.7i 2.07-13.2i))
Minimum distance between list elements: 4.919349550499537 @ elements ((0.2-2.2i 4.6+0i))
Average distance between list elements: 2.1925-2.925i
 
List: [6]
Maximum distance between list elements: 0
Minimum distance between list elements: 0
Average distance between list elements: 0
 
List: []
Maximum distance between list elements: 0
Minimum distance between list elements: 0
Average distance between list elements: 0
 
List: ["One", "Two", "Three"]
Cannot convert string to number: base-10 number must begin with valid digits or '.' in '⏏Two' (indicated by ⏏)
in sub max-diff at listdiff.p6 line 3
in block at listdiff.p6 line 33
in block <unit> at listdiff.p6 line 20
</pre>
 
=={{header|REXX}}==
Some basic error checking was made &nbsp; (non─numeric element, &nbsp; not enough elements).
No attempt was made to validate that the list elements are numeric.
 
<lang rexx>/*REXX program finds/displays the maximum difference between adjacent elements of a list*/
This REXX version was optimized to handle very large lists.
<syntaxhighlight lang="rexx">/*REXX program finds/displays the maximum difference between adjacent elements of a list*/
parse arg $ /*obtain optional arguments from the CL*/
if $='' | S=="," then $= , /*Not specified? Then use the default.*/
'1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3' /*commas are optional,; blanks mayare beOK. */
w= 0 /*the maximum width of any element num.*/
$= translate($, , ',') /*elide optional commas between numbers*/
#= words($) /*number of elements in the $ list. */
do i=1 for words($); x= word($, i) /*examine each element, ensure a number*/
@.i= x; w= max(w, length(x) ) /*assign element #; find maximum width.*/
if datatype(x, 'N') then iterate /*Is it numeric? Yes, then continue. */
say '***error*** element #'i " isn't numeric: " x
exit 13
end /*i*/
n= 0 /*N: # sets of (difference) elements.*/
if #<2 then do; say '***error*** not enough elements were specified, minimum is two.'
exit 13
Line 335 ⟶ 1,202:
say ' list of elements: ' space($) /*show the list of numbers in list.*/
say ' number of elements: ' # /*show " number " elements " " */
aad= -1; bb= /*thed: AAmaximum difference andfound (so BB list(sfar); # pairs. */
d= -1 do j=1 for #-1; jp= j + 1; a= @.j /*obtain the "A" element. /*maximum difference found (so far). */
do j=1 for #-1; a= word($, j ) b= @.jp /*obtain the "A " element. "B" " */
b= word($, j + 1) /* " " "B" " */
newD= abs(a - b) /*obtain difference between 2 elements.*/
if newD<d then iterate /*Is this not a new max difference ? */
d= newD /*assign new maximum difference, and */
aan= aan a;+ 1 bb= bb b /* the elements that had /*bump the max# diffof (difference) sets. */
aa.n= a; bb.n= b /* the elements that had the max diff.*/
end /*j*/
say
say /*stick a fork in it, we're all done. */
say 'The maximum difference of the elements in the list is: ' d
do k=1 for words(aa)n
say 'and is between the elements: ' wordright(aa.k, kw) " and " wordright(bb.k, kw)
end /*k*/
exit 0 /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<orepre>
list of elements: 1 8 2 -3 0 1 1 -2.3 0 5.5 8 6 2 9 11 10 3
number of elements: 17
 
The maximum difference of the elements in the list is: 7
and is between the elements: 1 and 8
and is between the elements: 2 and 9
and is between the elements: 10 and 3
</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight 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]"
Line 407 ⟶ 1,274:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 420 ⟶ 1,287:
10,3 ==> 7
done...
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
≪ DUP SIZE 0 → list len max
≪ 2 len '''FOR''' j
list j 1 - GET list j GET - ABS
'''IF''' max OVER < '''THEN''' 'max' STO
'''ELSE''' DROP '''END'''
'''NEXT'''
{} max +
2 len '''FOR''' j
list j 1 - GET list j GET
'''IF''' DUP2 - ABS max == '''THEN''' R→C +
'''ELSE''' DROP2 '''END'''
'''NEXT'''
≫ ≫ 'ADJMAX' STO
|
( {list} -- { max ( adjacent couples) }
Scan input list...
... calculate adjacent gap
... and store max value
Initialize output list
Scan input list...
Get 2 adjacent elements
If gap = max, add the 2 elements as a complex to the output list
|}
The following line of code delivers what is required:
[1 8 2 -3 0 1 1 -2.3 0 5.5 8 6 2 9 11 10 3] ADJMAX
{{out}}
<pre>
1: { 7 (1,8) (2,9) (10,3) }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">list = [1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3]
 
max_dif, pairs = list.each_cons(2).group_by{|a,b| (a-b).abs}.max
 
puts "Maximum difference is #{max_dif}:"
pairs.each{|pair| p pair}
</syntaxhighlight>
{{out}}
<pre>Maximum difference is 7:
[1, 8]
[2, 9]
[10, 3]
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::io ;
use itertools::Itertools ;
 
fn main() {
println!("Enter some real numbers, separated by blanks!");
let mut inline : String = String::new( ) ;
io::stdin( ).read_line( &mut inline ).unwrap( ) ;
let entered_line : &str = &*inline ;
let numbers : Vec<f32> = entered_line.split_whitespace( ).map( | s |
s.trim( ). parse::<f32>( ).unwrap( ) ).collect( ) ;
let mut pairs : Vec<(&f32, &f32)> = numbers.iter( ).tuple_windows( ).collect( ) ;
let num_slice = &mut pairs[..] ;
let len = num_slice.len( ) ;
num_slice.sort_by( | a, b| (a.0 - a.1).abs( ).partial_cmp(&( &*b.0 -&*b.1).abs( ))
.unwrap( )) ;
let maximumpair = &num_slice[len - 1 .. len] ;
let maxi = maximumpair.to_vec( ) ;
let maximum : f32 = (maxi[0].0- maxi[0].1).abs( ) ;
let maxima : Vec<&(&f32, &f32)> = num_slice.iter( ).filter( |&p |
(p.0 - p.1).abs( ) == maximum ).collect( ) ;
maxima.iter( ).for_each( | p | println!("{},{} ==> {}" ,
p.0 , p.1 , maximum)) ;
}</syntaxhighlight>
{{out}}
<pre>
1,8 ==> 7
2,9 ==> 7
10,3 ==> 7
</pre>
 
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">import math
 
fn main() {
list := [1.0, 8, 2, -3, 0, 1, 1, -2.3, 0, 5.5, 8, 6, 2, 9, 11, 10, 3]
mut max_diff := -1.0
mut max_pairs := [][2]f64{}
for i in 1..list.len {
diff := math.abs(list[i-1] - list[i])
if diff > max_diff {
max_diff = diff
max_pairs = [[list[i-1], list[i]]!]
} else if diff == max_diff {
max_pairs << [list[i-1], list[i]]!
}
}
println("The maximum difference between adjacent pairs of the list is: $max_diff")
println("The pairs with this difference are: $max_pairs")
}</syntaxhighlight>
 
{{out}}
<pre>
The maximum difference between adjacent pairs of the list is: 7
The pairs with this difference are: [[1, 8] [2, 9] [10, 3]]
</pre>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">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 = []
Line 436 ⟶ 1,419:
}
System.print("The maximum difference between adjacent pairs of the list is: %(maxDiff)")
System.print("The pairs with this difference are: %(maxPairs)")</langsyntaxhighlight>
 
{{out}}
Line 445 ⟶ 1,428:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="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.];
Line 462 ⟶ 1,445:
];
];
]</langsyntaxhighlight>
 
{{out}}
262

edits