Maximum difference between adjacent elements of list: Difference between revisions
Content added Content deleted
m (→{{header|Julia}}: List may contain all real number types.) |
(→OCaml: add) |
||
Line 18: | Line 18: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang="11l">F maxDeltas(ns) |
<syntaxhighlight lang="11l">F maxDeltas(ns) |
||
V pairs = zip(ns, ns[1..]).map((a, b) -> (abs(a - b), (a, b))) |
V pairs = zip(ns, ns[1..]).map((a, b) -> (abs(a - b), (a, b))) |
||
Line 507: | Line 506: | ||
idx : 12 values 2.0000000000000000E+000 9.0000000000000000E+000 |
idx : 12 values 2.0000000000000000E+000 9.0000000000000000E+000 |
||
idx : 15 values 1.0000000000000000E+001 3.0000000000000000E+000 </pre> |
idx : 15 values 1.0000000000000000E+001 3.0000000000000000E+000 </pre> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Line 590: | Line 588: | ||
7 2 9 |
7 2 9 |
||
7 10 3</pre> |
7 10 3</pre> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 682: | Line 679: | ||
<pre>The maximum difference between adjacent pairs of the list is: 7 |
<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> |
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}}== |
=={{header|Pascal}}== |
||
Line 855: | Line 874: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<syntaxhighlight lang="raku" line>sub max-diff (*@list) { |
<syntaxhighlight lang="raku" line>sub max-diff (*@list) { |
||
return 0 if +@list < 2; |
return 0 if +@list < 2; |