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

0 Members and 1 Guest are viewing this topic.

Stefan

  • Bull Frog
  • Posts: 303
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #15 on: December 23, 2021, 04:11:46 AM »
Code: [Select]
(compress '(nil 1 1 2 2))
 :-)
:-D

Don't tell anyone. They will test it on a, b, c

apricot125

  • Mosquito
  • Posts: 13
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #16 on: December 24, 2021, 01:23:13 AM »
a simple, but ugly one
Code: [Select]
;;;====================================================================================
  ;  P08 (**) Eliminate consecutive duplicates of list elements.
  ;      If a list contains repeated elements they should be
  ;      replaced with a single copy of the element. The order
  ;      of the elements should not be changed.
  ;  Example:      (compress '(a a a a b c c a a d e e e e))
  ;            --> (A B C A D E)
;;;====================================================================================
(defun compress (mylst / 1st ans len i)
  (setq 1st (car mylst)
        ans (list 1st)
        len (length mylst)
        i  1
  )
  (while (< i len)
    (if (equal 1st (nth i mylst))
      t
      (setq 1st (nth i mylst)
            ans (append ans (list 1st))
      )
    )  ; _ end of if
    (setq i (1+ i))
  )  ; _ end of while
  ans
)  ; _ end of defun

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #17 on: January 20, 2022, 02:35:15 AM »
Express Tools:
Code - Auto/Visual Lisp: [Select]
  1. (ACET-LIST-REMOVE-ADJACENT-DUPS '(a a a a b c c a a d e e e e))
  2. > (A B C A D E)
  3. (ACET-LIST-REMOVE-ADJACENT-DUPS '(A B C A D E))
  4. > (A B C A D E)
  5. (ACET-LIST-REMOVE-ADJACENT-DUPS '(A A))
  6. > (A)
  7. (ACET-LIST-REMOVE-ADJACENT-DUPS '())
  8. nil

domenicomaria

  • Bull Frog
  • Posts: 390
Re: [challenge] A07 : Eliminate consecutive duplicates from a list
« Reply #18 on: March 16, 2022, 05:15:13 AM »
(setq   l (list nil nil 1 1 1 2 2 3 nil nil 3 3 4 4 4 nil nil) )
(ACET-LIST-REMOVE-ADJACENT-DUPS l)

returns :
(     1 2 3 nil 3 4 nil)

... while it has to return

(nil 1 2 3 nil 3 4 nil)