Making range of elements of a list

Making range of elements of a list

rajeshpatnaik2001
Advocate Advocate
1,419 Views
3 Replies
Message 1 of 4

Making range of elements of a list

rajeshpatnaik2001
Advocate
Advocate

Hi,

If any one has a function to create range of numbers inside a list, please share with me.
Example:
(setq no_list (list 2 3 4 9 10 11 12 14 16 17 18 19 20 22))

(make_range no_list)

The output should be
("2-4" "9-12" "14" "16-20" "22")

 

Thanks.

0 Likes
Accepted solutions (1)
1,420 Views
3 Replies
Replies (3)
Message 2 of 4

rajeshpatnaik2001
Advocate
Advocate
Accepted solution

I got it. Thanks!
(defun make_range (no_list* / start* new_list len cn no)
(setq start* nil)
(setq new_list (list))
(setq len (length no_list*))
(setq cn 0)
(while (< cn len)
(setq no (nth cn no_list*))
(if (= start* nil) (setq start* no))
(if (nth (+ 1 cn) no_list*)
(if (/= (+ 1 no) (nth (+ 1 cn) no_list*))
(progn
(if (/= start* no)
(setq new_list (cons (strcat (itoa start*) "-" (itoa no)) new_list))
(setq new_list (cons (itoa no) new_list))
)
(setq start* (nth (+ 1 cn) no_list*))
)
)
(if (/= no (nth (- cn 1) no_list*))
(if (/= start* no)
(setq new_list (cons (strcat (itoa start*) "-" (itoa no)) new_list))
(setq new_list (cons (itoa no) new_list))
)
)
)
(setq cn (1+ cn))
)
(reverse new_list)
)

0 Likes
Message 3 of 4

_gile
Consultant
Consultant

Just for fun.

 

(defun make_range (lst / toStr loop)
  (defun toStr (lst)
    (if (cdr lst)
      (strcat (itoa (last lst)) "-" (itoa (car lst)))
      (itoa (car lst))
    )
  )
  (defun loop (lst tmp acc)
    (cond
      ((null lst) (reverse (cons (toStr tmp) acc)))
      ((= (car lst) (1+ (car tmp))) (loop (cdr lst) (cons (car lst) tmp) acc))
      (T (loop (cdr lst) (list (car lst)) (cons (toStr tmp) acc)))
    )
  )
  (loop (cdr lst) (list (car lst)) nil)
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 4

Moshe-A
Mentor
Mentor

@rajeshpatnaik2001 hi,

 

here is my 'fun' version

it also will run on unsorted or reversed list even with duplicate items.

 

enjoy

moshe

 

 

(defun make_range (no_list / const_subl build_range ; local functions
	                     no_list no data_range subL0 subL1)

 ; construct sublist 
 (defun const_subL (no subL0)
  (if (= (- no (car subL0)) 1)
   (reverse (cons no subL0))
  )
 ); const_subL
  
 ; construct result 
 (defun build_range ()
  (mapcar
   '(lambda (subL)
     (cond
      ((= (length subL) 1)
       (itoa (car subL))
      ); case
      ( t
       (strcat (itoa (car subL)) "-" (itoa (last subL)))
      ); case
     ); cond
    )
   data_range
  )
 ); build_range

  
 ; here start make_range
 (setq no (apply 'min no_list))
 (setq data_range (list (list no)))
  
 (while (setq no_list (vl-remove no no_list))
  (setq no (apply 'min no_list))
  (setq subL0 (car data_range))
   
  (if (setq subL1 (const_subL no (reverse subL0)))
   (progn
    (setq data_range (vl-remove subL0 data_range))
    (setq data_range (cons subL1 data_range))
   ); progn
   (setq data_range (cons (list no) data_range))
  ); if
 ); while
 
 (reverse (build_range))
);  make_range
0 Likes