Author Topic: [challenge] A07 : Eliminate consecutive duplicates from a list  (Read 1535 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
[challenge] A07 : Eliminate consecutive duplicates from a list
« on: December 22, 2021, 09:25:07 AM »
Using only AutoLisp, and given a list that contains repeated elements, return a list where the consecutive duplicates are replaced by a single copy of the item. The order of the elements, in the list, should not be changed. -i.e. transform a list into a compressed version.

Example:
(compress '(a a a a b c c a a d e e e e))
> (A B C A D E)
(compress '(A B C A D E))
> (A B C A D E)
(compress '(A A))
> (A)
(compress '())
nil
« Last Edit: December 22, 2021, 05:16:39 PM by John Kaul (Se7en) »
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Tharwat

  • Swamp Rat
  • Posts: 667
  • Hypersensitive
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #1 on: December 22, 2021, 09:35:59 AM »
My attempt.
Append function may slow down the process a bit as known to many but anyway I have chosen it to avoid using reverse at the end.
Code - Auto/Visual Lisp: [Select]
  1. (defun Eliminate:Duplicates (lst / itm rtn)
  2.   (while lst (or (member (setq itm (car lst)) rtn)
  3.                  (setq rtn (append rtn (list itm)))
  4.                  )
  5.     (setq lst (cdr lst))
  6.     )
  7.   rtn
  8.   )
  9.  

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #2 on: December 22, 2021, 09:57:55 AM »
Mine:
Code - Auto/Visual Lisp: [Select]
  1. (defun compres-se7en (aList / finddupli complst)
  2.    (defun finddupli (lst)
  3.      (if (not (member lst complst))
  4.        (setq complst (cons lst complst))))
  5.   (mapcar 'finddupli aList)
  6.   (reverse complst)
  7.  )
(compres-se7en '(a a a a b c c a a d e e e e))
(compres-se7en '(A B C A D E))
(compres-se7en '(A A))
(compres-se7en '())

I didn't consider trying not to use reverse. I'll see if I can break away from work to pen another version in a bit.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

gile

  • Water Moccasin
  • Posts: 2392
  • Marseille, France
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #3 on: December 22, 2021, 10:54:17 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun compress (lst)
  2.   (cond
  3.     ((null lst) nil)
  4.     ((= (car lst) (cadr lst)) (compress (cdr lst)))
  5.     (T (cons (car lst) (compress (cdr lst))))
  6.   )
  7. )
« Last Edit: December 22, 2021, 11:22:22 AM by gile »
Speaking English as a French Frog

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #4 on: December 22, 2021, 12:03:12 PM »
Code: [Select]
(defun ALE_RemConsDup (l / o p)
  (reverse
    (foreach f l
      (if (equal f p)
        (progn (setq p f) o)
        (setq  p f  o (cons f o))
      )
    )
  )
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #5 on: December 22, 2021, 12:15:45 PM »
Mine:
Code - Auto/Visual Lisp: [Select]
  1. (defun compres-se7en (aList / finddupli complst)
  2.    (defun finddupli (lst)
  3.      (if (not (member lst complst))
  4.        (setq complst (cons lst complst))))
  5.   (mapcar 'finddupli aList)
  6.   (reverse complst)
  7.  )
(compres-se7en '(a a a a b c c a a d e e e e))
(compres-se7en '(A B C A D E))
(compres-se7en '(A A))
(compres-se7en '())

I didn't consider trying not to use reverse. I'll see if I can break away from work to pen another version in a bit.
Code: [Select]
(compres-se7en '(a a a a b c c a a d e e e e)) => (A B C D E) not > (A B C A D E)
(compres-se7en '(A B C A D E))                 => (A B C D E) not > (A B C A D E)

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #6 on: December 22, 2021, 01:03:37 PM »
clarification:
It should be only grouping consecutive duplicates
Therefore:

(compres-se7en '(a a a a b c c a a d e e e e)) => (A B C D E) not > (A B C A D E)
is incorrect.
This is how it should work.
(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)) => (A B C A D E)

That being said, my processing of (A B C A D E) is incorrect. I should return the same list because there are not any CONSECUTIVE duplicates in that list. (A B C A D E)

I will have to redo my entry.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #7 on: December 22, 2021, 01:08:36 PM »
Tharwat and I have incorrect entries.

gile and Marc'Antonio Alessi have correct entries.

(from my limited testing)
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

gile

  • Water Moccasin
  • Posts: 2392
  • Marseille, France
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #8 on: December 22, 2021, 01:40:02 PM »
Imperative way.
Code - Auto/Visual Lisp: [Select]
  1. (defun compress (lst / ret)
  2.   (while lst
  3.     (if (/= (car lst) (cadr lst))
  4.       (setq ret (cons (car lst) ret))
  5.     )
  6.     (setq lst (cdr lst))
  7.   )
  8.   (reverse ret)
  9. )
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #9 on: December 22, 2021, 02:11:49 PM »
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun compress ( l / r x )
  2.     (while (setq x (car l))
  3.         (setq l (cdr l)
  4.               r (cons x r)
  5.         )
  6.         (while (= x (car l)) (setq l (cdr l)))
  7.     )
  8.     (reverse r)
  9. )

Stefan

  • Bull Frog
  • Posts: 303
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #10 on: December 22, 2021, 02:30:32 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun compress (l / r)
  2.   (while
  3.     (progn
  4.       (or
  5.         (= (car l) (car r))
  6.         (setq r (cons (car l) r))
  7.       )
  8.       (setq l (cdr l))
  9.     )
  10.   )
  11.   (reverse r)
  12. )

Stefan

  • Bull Frog
  • Posts: 303
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #11 on: December 22, 2021, 02:49:22 PM »
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun compress ( l / r x )
  2.     (while (setq x (car l))
  3.         (setq l (cdr l)
  4.               r (cons x r)
  5.         )
  6.         (while (= x (car l)) (setq l (cdr l)))
  7.     )
  8.     (reverse r)
  9. )

Code: [Select]
(compress '(nil 1 1 2 2))

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #12 on: December 22, 2021, 03:54:01 PM »
corrected entry
Code - Auto/Visual Lisp: [Select]
  1. (defun compress (aList / dupli-subarray)
  2.   (defun dupli-subarray (element seg lst)
  3.     (cond
  4.       ((null lst)
  5.        (cons element seg))
  6.       ((eq element (car lst))
  7.         (dupli-subarray element '() (cdr lst)))
  8.       (t
  9.         (append
  10.           (cons element seg)
  11.           (dupli-subarray (car lst) '() (cdr lst)))))
  12.     )
  13.   (if (null aList)
  14.     '()
  15.     (dupli-subarray (car aList) '() (cdr aList)))
  16.   )
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #13 on: December 22, 2021, 06:21:50 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun compress (l / r)
  2.   (while
  3.     (progn
  4.       (or
  5.         (= (car l) (car r))
  6.         (setq r (cons (car l) r))
  7.       )
  8.       (setq l (cdr l))
  9.     )
  10.   )
  11.   (reverse r)
  12. )

Code: [Select]
(compress '(nil 1 1 2 2))
 :-)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #14 on: December 23, 2021, 03:35:42 AM »
New version to avoid:  (compress '(nil 1 1 2 2)) > (1 2)
Code: [Select]
(defun ALE_RemConsDup (L / o p)   ; r1.02
  (setq p (cons 0 (car L)))       ; #02 ex (setq p L)
  (reverse
    (foreach f L
      (if (equal f p)
        o                         ; #01 ex (progn (setq p f) o)
        (setq p f  o (cons f o))
      )
    )
  )
)
Edit: see#nn