Maximum difference between adjacent elements of list

From Rosetta Code
Revision as of 13:05, 18 July 2021 by CalmoSoft (talk | contribs) (Created page with "{{Draft task}} ;Task:Find maximum difference between adjacent elements of list. <br> List = [-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,8,1] <br><br> =={{header|Ring}}== <lang ring> se...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.


List = [-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,8,1]



Ring

<lang ring> see "working..." + nl strList = "[-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,8,1]" see "Maximum difference between adjacent elements of list is:" + nl + nl see "Input list: " + strList + nl + nl see "Output:" + nl sList = [-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,8,1] oldDiff = fabs(sList[1]-sList[2]) first = sList[1] second = sList[2] sortList = []

for n = 2 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)

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
      see "" + first1 + "," + second1 + " ==> " + oldDiff1 + nl   
      see "" + first2 + "," + second2 + " ==> " + oldDiff2 + nl
   else
      exit
   ok

next

see "done..." + nl </lang>

Output:
working...
Maximum difference between adjacent elements of list is:

Input list: [-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,8,1]

Output:
2,9 ==> 7
8,1 ==> 7
done...