Maximum difference between adjacent elements of list: Difference between revisions
(Added Wren) |
(Added Go) |
||
Line 15: | Line 15: | ||
10,3 ==> 7 |
10,3 ==> 7 |
||
<br><br> |
<br><br> |
||
=={{header|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> |
|||
{{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|Phix}}== |
=={{header|Phix}}== |
Revision as of 15:59, 18 July 2021
- 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
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]]
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}
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