How to resolve the algorithm Range extraction step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Range extraction step by step in the 11l programming language

Table of Contents

Problem Statement

A format for expressing an ordered list of integers is to use a comma separated list of either Example The list of integers: Is accurately expressed by the range expression: (And vice-versa).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Range extraction step by step in the 11l programming language

Source code in the 11l programming language

F range_extract(lst)
   [[Int]] r
   V lenlst = lst.len
   V i = 0
   L i < lenlst
      V low = lst[i]
      L i < lenlst - 1 & lst[i] + 1 == lst[i + 1]
         i++
      V hi = lst[i]
      I hi - low >= 2
         r [+]= [low, hi]
      E I hi - low == 1
         r [+]= [low]
         r [+]= [hi]
      E
         r [+]= [low]
      i++
   R r

F printr(ranges)
   print(ranges.map(r -> (I r.len == 2 {r[0]‘-’r[1]} E String(r[0]))).join(‘,’))

L(lst) [[-8, -7, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7,
         8, 9, 10, 11, 14, 15, 17, 18, 19, 20],
        [0, 1, 2, 4, 6, 7, 8, 11, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22,
         23, 24, 25, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39]]
   printr(range_extract(lst))

  

You may also check:How to resolve the algorithm Sokoban step by step in the Raku programming language
You may also check:How to resolve the algorithm Latin Squares in reduced form step by step in the MiniZinc programming language
You may also check:How to resolve the algorithm Search a list step by step in the Go programming language
You may also check:How to resolve the algorithm Date format step by step in the Rust programming language
You may also check:How to resolve the algorithm Show the epoch step by step in the R programming language