How to resolve the algorithm Range expansion step by step in the Common Lisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Range expansion step by step in the Common Lisp 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).

Expand the range description: Note that the second element above, is the range from minus 3 to minus 1.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Range expansion step by step in the Common Lisp programming language

Source code in the common programming language

(defun expand-ranges (string)
  (loop
     with prevnum = nil
     for idx = 0 then (1+ nextidx)
     for (number nextidx) = (multiple-value-list
                             (parse-integer string
                                            :start idx :junk-allowed t))
     append (cond
              (prevnum
               (prog1
                   (loop for i from prevnum to number
                      collect i)
                 (setf prevnum nil)))
              ((and (< nextidx (length string))
                    (char= (aref string nextidx) #\-))
               (setf prevnum number)
               nil)
              (t
               (list number)))
     while (< nextidx (length string))))

CL-USER> (expand-ranges "-6,-3--1,3-5,7-11,14,15,17-20")
(-6 -3 -2 -1 3 4 5 7 8 9 10 11 14 15 17 18 19 20)


  

You may also check:How to resolve the algorithm Set consolidation step by step in the F# programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Forward difference step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the Ol programming language
You may also check:How to resolve the algorithm Arithmetic derivative step by step in the Julia programming language