Author Topic: [challenge] A13 : Drop every N'th element from a list  (Read 361 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Needs a day job
  • Posts: 9947
[challenge] A13 : Drop every N'th element from a list
« on: January 14, 2022, 09:10:22 AM »
Using only AutoLisp, drop every N'th element from a list.
NOTE: The first element in the list is number 0.

Example:
(drop '(a b c d e f g h i k) 2)
> (A B D E G H K)
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Tharwat

  • Swamp Rat
  • Posts: 645
  • Hypersensitive
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #1 on: January 14, 2022, 09:31:58 AM »
My attempt.
Code - Auto/Visual Lisp: [Select]
  1. (defun Drop-Tharwat (lst pos / num rtn)
  2.   (setq num 0)
  3.   (foreach itm lst
  4.     (or (and (= num pos)
  5.              (setq num 0)
  6.         )
  7.         (setq rtn (cons itm rtn)
  8.               num (1+ num)
  9.         )
  10.     )
  11.   )
  12.   (reverse rtn)
  13. )

JohnK

  • Administrator
  • Needs a day job
  • Posts: 9947
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #2 on: January 14, 2022, 09:56:17 AM »
mine:
Code - Auto/Visual Lisp: [Select]
  1. (defun drop-se7en (aList item / drop-nth)
  2.    (defun drop-nth ( aList item cntr )
  3.      (if (null aList)
  4.        nil
  5.        (if (= cntr item)
  6.          (drop-nth (cdr aList) item 0)
  7.          (cons (car aList) (drop-nth (cdr aList) item (1+ cntr))))))
  8.    (drop-nth aList item 0))
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

gile

  • Water Moccasin
  • Posts: 2340
  • Marseille, France
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #3 on: January 14, 2022, 10:05:18 AM »
(defun drop-gile (l n)
  (cond
    ((null l) nil)
    ((= n 0) (cdr l))
    (T (cons (car l) (drop-gile (cdr l) (1- n))))
  )
)


EDIT: I missread "Drop every N'th element from a list"

Code - Auto/Visual Lisp: [Select]
  1. (defun drop-gile (l n / f)
  2.   (defun f (l i)
  3.     (cond
  4.       ((or (null l) (< i 0)) l)
  5.       ((zerop i) (f (cdr l) n))
  6.       (T (cons (car l) (f (cdr l) (1- i))))
  7.     )
  8.   )
  9.   (f l n)
  10. )
« Last Edit: January 14, 2022, 12:03:20 PM by gile »
Speaking English as a French Frog

well20152016

  • Newt
  • Posts: 125
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #4 on: January 14, 2022, 10:36:50 AM »
Code - Auto/Visual Lisp: [Select]
  1. ;;;(drop  '(a b c d e f g h i k) 2)
  2. (defun drop (l n / i l1)
  3.   (setq i 0)
  4.   (repeat (length l)
  5.   (if (> (rem  (1+ i) (1+ n)) 0)  (setq l1 (cons (nth i l) l1)))
  6.  (setq  i (+ 1 i))  
  7. )
  8. (reverse  l1 )
  9. )
  10.  
  11. (defun drop (l n / i l1)
  12. (setq i 0)
  13. (mapcar'(lambda (x) (if (> (rem  (1+ i) (1+ n)) 0)  (setq l1 (cons x l1) )) (setq  i (+ 1 i))) l)
  14. (reverse  l1 )
  15. )
  16.  
« Last Edit: January 14, 2022, 10:42:22 AM by well20152016 »

Stefan

  • Bull Frog
  • Posts: 288
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #5 on: January 14, 2022, 11:25:27 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun drop-stef (l n / f)
  2.   (defun f (l x)
  3.     (if l
  4.       (cons (car l)
  5.         (if (= x 1) (f (cddr l) n)
  6.           (f (cdr l) (1- x))
  7.         )
  8.       )
  9.     )
  10.   )
  11.   (if (> n 0) (f l n))
  12. )
« Last Edit: January 14, 2022, 11:31:10 AM by Stefan »

kirby

  • Newt
  • Posts: 57
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #6 on: January 14, 2022, 11:37:51 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun Drop-kirby (MyList Index / OutList CNT MyItem1)
  2. ; Challenge A13 : Drop every N'th element from a list
  3. ; e.g. (drop '(a b c d e f g h i k) 2)
  4. ; output (A B D E G H K)
  5.  
  6. (setq OutList nil)
  7.  
  8. (if (and        (and (eq (type Index) 'INT) (>= Index 0))       ; Index is non-nil and >= 0
  9.                 (and MyList (eq (type MyList) 'LIST))           ; List exists
  10.     ) ; close and
  11.   (progn
  12.         (setq CNT 0)
  13.         (repeat (length MyList)
  14.                 (if (/= (rem (1+ CNT) (1+ Index)) 0)    ; check that remainder is not 0
  15.                         (setq OutList (cons (nth CNT MyList) OutList))
  16.                 ) ; close if
  17.        
  18.                 (setq CNT (1+ CNT))
  19.         ) ; close repeat
  20.  
  21.         (setq OutList (reverse OutList))
  22.   )
  23. ) ; close if
  24. OutList
  25. )
  26.  

bruno_vdh

  • Newt
  • Posts: 31
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #7 on: January 14, 2022, 11:44:53 AM »
Hi,

recursive version
(defun drop-vdh1 (l n)
  (if l
    (if   (zerop n)
      (cdr l)
      (cons (car l) (drop-vdh1 (cdr l) (1- n)))
    )
  )
)

Code: [Select]
(defun drop-vdh1 (l n / f)
  (defun f (l i)
    (if l
      (if (zerop i)
(f (cdr l) n)
(cons (car l) (f (cdr l) (1- i)))
      )
    )
  )
  (f l n)
)

iterative version
(defun drop-vdh2 (l n / m)
  (if (> (length l) n)
    (repeat (1+ n)
      (setq m (cons (car l) m)
       l (cdr l)
      )
    )
    l
  )
  (append (reverse (cdr m)) l)
)


version with mapcar
Code: [Select]
(defun drop-vdh2 (l n / i)
  (setq i 0)
  (apply 'append
(mapcar '(lambda (x)
    (cond ((< i n) (setq i (1+ i)) (list x))
  ((setq i 0) nil)
    )
  )
l
)
  )
)

message modified following Stefan's remark
« Last Edit: January 14, 2022, 12:47:31 PM by bruno_vdh »

Stefan

  • Bull Frog
  • Posts: 288
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #8 on: January 14, 2022, 12:01:08 PM »
@gile, @bruno
Remove EVERY nth

Code - Auto/Visual Lisp: [Select]
  1. (DROP-THARWAT L N) (A B D E G H K)
  2. (DROP-SE7EN   L N) (A B D E G H K)
  3. (DROP-GILE    L N) (A B D E F G H I K)
  4. (DROP-WELL1   L N) (A B D E G H K)
  5. (DROP-WELL2   L N) (A B D E G H K)
  6. (DROP-STEF    L N) (A B D E G H K)
  7. (DROP-KIRBY   L N) (A B D E G H K)
  8. (DROP-VDH1    L N) (A B D E F G H I K)
  9. (DROP-VDH2    L N) (A B D E F G H I K)
« Last Edit: January 14, 2022, 12:10:13 PM by Stefan »

bruno_vdh

  • Newt
  • Posts: 31
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #9 on: January 14, 2022, 12:45:48 PM »
Thank you Stefan, I read too fast with google translate, I corrected my previous message.

bruno_vdh

  • Newt
  • Posts: 31
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #10 on: January 14, 2022, 01:02:13 PM »
A more optimized version with mapcar
Code: [Select]
(defun drop-vdh3 (l n / i m)
  (setq i 0)
  (mapcar '(lambda (x)
     (if (< i n)
       (setq i (1+ i)
     m (cons x m)
       )
       (setq i 0)
     )
   )
  l
  )
  (reverse m)
)


The same in iterative version with while
Code: [Select]
(defun drop-vdh4 (l n / i m)
  (setq i 0)
  (while l
    (if (< i n)
      (setq i (1+ i)
    m (cons (car l) m)
      )
      (setq i 0)
    )
    (setq l (cdr l))
  )
  (reverse m)
)
« Last Edit: January 14, 2022, 01:17:05 PM by bruno_vdh »

Lee Mac

  • Seagull
  • Posts: 12605
  • London, England
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #11 on: January 14, 2022, 03:54:02 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun drop-lm1 ( l n / r )
  2.     (while l
  3.         (repeat (min n (length l))
  4.             (setq r (cons (car l) r) l (cdr l))
  5.         )
  6.         (setq l (cdr l))
  7.     )
  8.     (reverse r)
  9. )

ronjonp

  • Needs a day job
  • Posts: 7360
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #12 on: January 14, 2022, 05:22:11 PM »
My disqualified version:
Code - Auto/Visual Lisp: [Select]
  1. (defun drop-rjp (l n / a)
  2.   (setq a 0)
  3.   (vl-remove-if '(lambda (x) (= 0 (rem (setq a (1+ a)) (1+ n)))) l)
  4. )

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1566
  • Moscow (Russia)
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #13 on: January 15, 2022, 10:15:59 AM »
Recursion version:
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l n)
  2.   (or (listp n) (setq n (cons n n)))
  3.   (cond ((not l) nil)
  4.         ((zerop (car n)) (f (cdr l) (cons (cdr n) (cdr n))))
  5.         ((cons (car l) (f (cdr l) (cons (1- (car n)) (cdr n)))))
  6.   )
  7. )
use:
Code - Auto/Visual Lisp: [Select]
  1.  
  2.  (f '(a b c d e f g h i k) 2) ;;        >> (A B D E G H K)
  3.  (f '(a b c d e f g h i k) '(0 . 2));; >> (B C E F H I)
  4.  (f '(a b c d e f g h i k) '(1 . 2));; >> (A C D F G I K)
  5.  (f '(a b c d e f g h i k) '(2 . 2));; >> (A B D E G H K)
  6.  

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1254
  • Marco
Re: [challenge] A13 : Drop every N'th element from a list
« Reply #14 on: January 16, 2022, 09:23:36 AM »
Code: [Select]
(defun ALE_Drop (L n / c o)
  (if (and (setq c n) (zerop n))
    nil
    (reverse
      (foreach f L
        (if (zerop c)
          (setq c n)
          (setq c (1- c)  o (cons f o))
        )
        o
      )
    )
  )
)