Maximum difference between adjacent elements of list: Difference between revisions

no edit summary
No edit summary
 
(10 intermediate revisions by 8 users not shown)
Line 138:
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
</pre>
 
Line 294 ⟶ 343:
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}}==
Line 423 ⟶ 501:
{{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#}}==
Line 582 ⟶ 809:
 
=={{header|J}}==
<syntaxhighlight lang="j">maxdiff =: ([: (([:>,.|@-/]"1) 2(,.[/\ (#~] (=[:>./])[@:|@) -/"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
7 1 8 7
7 2 9 7
7 10 3</pre> 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}}==
Line 1,099 ⟶ 1,330:
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)}}==
Line 1,128 ⟶ 1,406:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">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 = []
258

edits