Author Topic: [challenge] A08 : "Pack" consecutive duplicates of list elements into sublists  (Read 1560 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Using only AutoLisp, "Pack" a list that contains repeated elements. The elements should be placed in separate sublists.

Example:
(pack '(a a a a b c c a a d e e e e))
> ((A A A A) (B) (C C) (A A) (D) (E E E E))
(pack '())
> nil
« Last Edit: December 28, 2021, 08:06:32 AM by John Kaul (Se7en) »
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Challenge posted.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

well20152016

  • Newt
  • Posts: 130
The simplest way

Code - Auto/Visual Lisp: [Select]
  1. (defun pack (l / l1 l2)
  2.   (while l
  3.     (setq n (length l)
  4.           a (car l)
  5.           l (vl-remove-if '(lambda (b) (equal a b)) l)
  6.           n (repeat (- n (length l)) (setq l1 (cons a l1)))
  7.         )
  8.   (setq l2 (cons l1 l2) l1 nil))
  9.  (reverse l2) )
  10.  


Code - Auto/Visual Lisp: [Select]
  1. (defun pack (l / l1 l2 l3)
  2.   (while l
  3.     (setq l1 nil l2 nil l1 (list (car l))
  4.           n (foreach a (cdr l) (if (= (car l) a ) (setq l1 (cons a l1)) (setq l2 (cons a l2))))
  5.           l3 (cons l1 l3)
  6.           l (reverse l2)
  7.           ))
  8.  (reverse l3) )
  9.  
  10.  
« Last Edit: December 28, 2021, 09:14:04 AM by well20152016 »

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Code - Auto/Visual Lisp: [Select]
  1. (defun pack1 ( l / f )
  2.     (defun f ( l a )
  3.         (if l
  4.             (if (= (car l) (car a))
  5.                 (f (cdr l) (cons (car l) a))
  6.                 (cons a (pack1 l))
  7.             )
  8.             (list a)
  9.         )
  10.     )
  11.     (if l (f (cdr l) (list (car l))))
  12. )
« Last Edit: December 28, 2021, 08:49:54 AM by Lee Mac »

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
The simplest way
...

Sorry, well20152016 you will have to build your own vl-remove-if.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
You are quick. It took me a few hours to get mine working.

Code - Auto/Visual Lisp: [Select]
  1. (defun pack (aList / segment)
  2.   (defun group-subarray (element seg aList)
  3.     (cond
  4.       ((null aList)
  5.        (list (cons element seg)))
  6.       ((eq element (car aList))
  7.        (group-subarray element (cons element seg) (cdr aList)))
  8.       (t
  9.         (cons
  10.           (cons element seg)
  11.           (group-subarray (car aList) '() (cdr aList))))
  12.       )
  13.     )
  14.   (if (null aList)
  15.     '()
  16.     (group-subarray (car aList) '() (cdr aList)))
  17.   )
  18.  
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Code - Auto/Visual Lisp: [Select]
  1. (defun pack2 ( l / r s x y )
  2.     (while l
  3.         (setq x (car  l)
  4.               s (list x)
  5.               l (cdr  l)
  6.         )
  7.         (while (and l (= x (setq y (car l))))
  8.             (setq s (cons y s)
  9.                   l (cdr  l)
  10.             )
  11.         )
  12.         (setq r (cons s r))
  13.     )
  14.     (reverse r)
  15. )
« Last Edit: December 28, 2021, 02:04:51 PM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Code - Auto/Visual Lisp: [Select]
  1. (defun pack3 ( l / s x y )
  2.     (if l
  3.         (progn
  4.             (setq x (car  l)
  5.                   s (list x)
  6.                   l (cdr  l)
  7.             )  
  8.             (while (and l (= x (setq y (car l))))
  9.                 (setq s (cons y s)
  10.                       l (cdr  l)
  11.                 )
  12.             )
  13.             (cons s (pack3 l))
  14.         )
  15.     )
  16. )
« Last Edit: December 28, 2021, 02:05:36 PM by Lee Mac »

VovKa

  • Swamp Rat
  • Posts: 1475
  • Ukraine
Code: [Select]
(defun pack4 (l / nl)
  (foreach e l
    (setq nl (if (= e (caar nl))
       (cons (cons e (car nl)) (cdr nl))
       (cons (list e) nl)
     )
    )
  )
  (reverse nl)
)

VovKa

  • Swamp Rat
  • Posts: 1475
  • Ukraine
Lee, your 2 and 3 do not like nils

gile

  • Water Moccasin
  • Posts: 2392
  • Marseille, France
Code - Auto/Visual Lisp: [Select]
  1. (defun pack (l / loop)
  2.   (defun loop (l a)
  3.     (cond
  4.       ((null l) (reverse a))
  5.       ((/= (car l) (caar a)) (loop (cdr l) (cons (list (car l)) a)))
  6.       (T (loop (cdr l) (cons (cons (car l) (car a)) (cdr a))))
  7.     )
  8.   )
  9.   (loop l nil)
  10. )
Speaking English as a French Frog

gile

  • Water Moccasin
  • Posts: 2392
  • Marseille, France
In other words:
Code - Auto/Visual Lisp: [Select]
  1. (defun pack (l / loop)
  2.   (defun loop (l a)
  3.     (if l
  4.       (loop
  5.         (cdr l)
  6.         (if (= (car l) (caar a))
  7.           (cons (cons (car l) (car a)) (cdr a))
  8.           (cons (list (car l)) a)
  9.         )
  10.       )
  11.       (reverse a)
  12.     )
  13.   )
  14.   (loop l nil)
  15. )
Speaking English as a French Frog

Stefan

  • Bull Frog
  • Posts: 303
Code - Auto/Visual Lisp: [Select]
  1. (defun pack_stef (l / p r)
  2.   (setq p (list (car l)) l (cdr l))
  3.   (while
  4.     (cond
  5.       ( (not l) nil)
  6.       ( (eq (car l) (car p))
  7.         (setq p (cons (car l) p)
  8.               l (cdr l)
  9.         )
  10.       )
  11.       (T
  12.         (setq r (cons p r)
  13.               p (list (car l))
  14.               l (cdr l)
  15.         )
  16.       )
  17.     )
  18.   )
  19.   (reverse (cons p r))
  20. )

ronjonp

  • Needs a day job
  • Posts: 7437
Code - Auto/Visual Lisp: [Select]
  1. (defun pack-rjp (l / a r)
  2.   (while l
  3.     (setq a (list (car l)))
  4.     (setq l (cdr l))
  5.     (while (and l (equal (car a) (car l))) (setq a (cons (car l) a)) (setq l (cdr l)))
  6.     (setq r (cons a r))
  7.   )
  8.   (reverse r)
  9. )

Windows 11 x64 - AutoCAD /C3D 2022

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Lee, your 2 and 3 do not like nils

Thanks - now revised :-)