Author Topic: [challenge] A26 : Add the N'th element into a list at position  (Read 1718 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
[challenge] A26 : Add the N'th element into a list at position
« on: February 18, 2022, 09:03:33 AM »
Add the N'th element into a list at a position.

(add-nth <list> <item> <position>)

Example:
(add-nth '(a c d) 'B 1)
> (A B C D)
(add-nth '(a c d) 'B 10)
> (A C D B)


EDIT: Changed from 1 based indexing to 0 based indexing and added condition for larger position.
« Last Edit: February 22, 2022, 10:42:56 AM by JohnK »
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #1 on: February 18, 2022, 09:26:29 AM »
I have this old one (similar):
Code: [Select]
; Function: ALE_AppendBeforeNth
;
; Version 1.00 - 15/11/2005
;
; Description:
;   returns a copy of the list with a new item added before
;   the item in the nth position (NthPos)
;
; Arguments:
;   NthPos = Integer - nth like
;   NewItm = An atom or list
;   In_Lst = A list
;   InRLst = Original list reversed
;
; Return Values:
;   A list with a new item added before the item in the nth position
;
; Examples:
;   (setq alist '((0 . A) (1 . B) (2 . C) (3 . D)))
;
;   (ALE_AppendBeforeNth 0 "NEW" alist (reverse alist))
;   Returns: ("NEW" (0 . A) (1 . B) (2 . C) (3 . D))
;
;   (ALE_AppendBeforeNth 1 "NEW" alist (reverse alist))
;   Returns: ((0 . A) "NEW" (1 . B) (2 . C) (3 . D))
;
;   (ALE_AppendBeforeNth 3 "NEW" alist (reverse alist))
;   Returns: ((0 . A) (1 . B) (2 . C) (3 . D) "NEW")
;
;   (ALE_AppendBeforeNth 4 "NEW" alist (reverse alist))
;   Returns: ((0 . A) (1 . B) (2 . C) (3 . D))
;
;   (ALE_AppendBeforeNth 1 '(9 . Z) alist (reverse alist))
;   Returns: ((0 . A) (9 . Z) (1 . B) (2 . C) (3 . D))
;
;   (ALE_AppendBeforeNth 1 nil alist (reverse alist))
;   Returns: ((0 . A) nil (1 . B) (2 . C) (3 . D))
;
(defun ALE_AppendBeforeNth (NthPos NewItm In_Lst InRLst / LstLng NthItm)
  (cond
    ( (null In_Lst) nil )
    ( (zerop NthPos) (cons NewItm In_Lst) )
    ( (null (setq NthItm (nth NthPos In_Lst))) In_Lst )
    ( (zerop (setq LstLng (- (length In_Lst) (1+ NthPos))))
      (append In_Lst (list NewItm))
    )
    ( T
      (while
        (/=
          NthPos
          (length (setq InRLst (cdr (member NthItm InRLst))))
        )
      )
      (while
        (/=
          LstLng
          (length (setq In_Lst (cdr (member NthItm In_Lst))))
        )
      )
      (append (reverse InRLst) (list NewItm) (cons NthItm In_Lst))
    )
  )
)

ribarm

  • Gator
  • Posts: 2747
  • Marko Ribar, architect
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #2 on: February 18, 2022, 09:32:48 AM »
Recursive :

Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth ( l e p )
  2.   (if (and l (/= p 1))
  3.     (cons (car l) (add-nth (cdr l) e (1- p)))
  4.     (if l
  5.       (cons e (cons (car l) (add-nth (cdr l) e (1- p))))
  6.     )
  7.   )
  8. )
  9.  

Iterative - straight forward :

Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth ( l e p / r )
  2.   (repeat (1- p)
  3.     (setq r (cons (car l) r) l (cdr l))
  4.   )
  5.   (setq r (cons e r))
  6.   (repeat (length l)
  7.     (setq r (cons (car l) r) l (cdr l))
  8.   )
  9.   (reverse r)
  10. )
  11.  

Iteration with applying anonymous (lambda) function by using (mapcar) function on main list and applying concatenation (append) function to resulting list with lists as elements :

Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth ( l e p )
  2.   (apply (function append) (mapcar (function (lambda ( x ) (if (zerop (setq p (1- p))) (list e x) (list x)))) l))
  3. )
  4.  

: (add-nth '(a c d) 'B 2)
(A B C D)
« Last Edit: February 18, 2022, 10:44:53 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

bruno_vdh

  • Newt
  • Posts: 82
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #3 on: February 18, 2022, 10:07:07 AM »
(add-nth '(a c d) 'B 2)
> (A B C D)

Hi, what should the following data return, knowing that nth starts at zero
Code: [Select]
(add-nth '(a c d) 'b 1)
(add-nth '(a c d) 'b 0)
(add-nth '(a c d) 'b 10)

Waiting for a first quick proposal
Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth (l x n)
  2.   (if (> n 1)
  3.     (cons (car l) (add-nth (cdr l) x (1- n)))
  4.     (cons x l)
  5.   )
  6. )

well20152016

  • Newt
  • Posts: 130
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #4 on: February 18, 2022, 10:20:16 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth (l b n / lst)
  2.   (setq i 0)
  3.   (foreach a l
  4.     (if (equal (setq i (+ 1 i)) n ) (setq lst (cons b lst) lst (cons a lst)) (setq lst (cons a lst)))
  5.     )
  6. (reverse lst))

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #5 on: February 20, 2022, 07:53:32 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun addnth ( x n l )
  2.     (cond
  3.         (   (not l) (list x))
  4.         (   (< 1 n) (cons (car l) (addnth x (1- n) (cdr l))))
  5.         (   (cons x l))
  6.     )
  7. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (addnth 'b 2 '(a c d))
  2. (A B C D)
  3. _$ (addnth 'b 10 '(a c d))
  4. (A C D B)

Odd that you would opt for the index to be one-based...

dexus

  • Newt
  • Posts: 75
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #6 on: February 21, 2022, 02:52:38 AM »
My attempt was very similar to ribarm's iterative version.
Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth-dexus (l i n / r)
  2.   (repeat (min (1- n) (length l))
  3.     (setq r (cons (car l) r)
  4.           l (cdr l))
  5.   )
  6.   (setq r (cons i r))
  7.   (while l
  8.     (setq r (cons (car l) r)
  9.           l (cdr l))
  10.   )
  11.   (reverse r)
  12. )
« Last Edit: February 23, 2022, 11:25:28 AM by dexus »

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #7 on: February 21, 2022, 10:36:05 AM »
I'm sorry I didn't notice that I made this 1 based indexing; I am beyond the challenges I had an opportunity to workout before hand so I probably typed the declaration up without thinking twice about the index.

We can make this one 0 based index and I think "(add-nth '(a c d) 'b 10)" should attach to the end so it should return (a c d b).
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

bruno_vdh

  • Newt
  • Posts: 82
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #8 on: February 21, 2022, 03:19:20 PM »
I'm sorry I didn't notice that I made this 1 based indexing; I am beyond the challenges I had an opportunity to workout before hand so I probably typed the declaration up without thinking twice about the index.

We can make this one 0 based index and I think "(add-nth '(a c d) 'b 10)" should attach to the end so it should return (a c d b).
No worries :-)

For brevity:
Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth (l x n)
  2.   (if (and l (> n 0))
  3.     (cons (car l) (add-nth (cdr l) x (1- n )))
  4.     (cons x l)))

For fantasy:
Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth (l x n)
  2.   (if (/= 0 n nil l)
  3.     (cons (car l) (add-nth (cdr l) x (1- n )))
  4.     (cons x l)))

For possible optimization:
Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth (l x n / f)
  2.   (defun f (l n)
  3.     (if (> n 0)
  4.       (cons (car l) (f (cdr l) (1- n)))
  5.       (cons x l)))
  6.   (f l (min n (length l)))
  7. )
  8.  

The results:
Code: [Select]
_$ (add-nth nil 'b 5)
(B)
_$ (add-nth '(a c d) 'b 0)
(B A C D)
_$ (add-nth '(a c d) 'b 1)
(A B C D)
_$ (add-nth '(a c d) 'b 10)
(A C D B)

« Last Edit: February 21, 2022, 03:32:59 PM by bruno_vdh »

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #9 on: February 22, 2022, 10:44:28 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth-se7en (aList item pos / add-nth)
  2.   (defun add-nth ( aList item index cntr )
  3.     (if aList
  4.       (if (= cntr index)
  5.         (cons item (add-nth aList item index (1+ cntr)))
  6.         (cons (car aList) (add-nth (cdr aList) item index (1+ cntr))))) )
  7.   (if (>= pos (length aList))
  8.     (reverse (cons item (reverse aList)))
  9.     (add-nth aList item pos 0)
  10.     )
  11.   )

bruno_vdh, I really need to spend some time and study your code; you have some very nice routines!
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #10 on: February 22, 2022, 04:44:42 PM »
Another, disqualified -
Code - Auto/Visual Lisp: [Select]
  1. (defun addnth ( x n l / b m )
  2.     (setq m (vl-member-if-not '(lambda ( a ) (if (< 0 (setq n (1- n))) (setq b (cons a b)))) l))
  3.     (append (reverse b) (cons x m))
  4. )

bruno_vdh

  • Newt
  • Posts: 82
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #11 on: February 23, 2022, 09:36:07 AM »
Another, disqualified -
Code - Auto/Visual Lisp: [Select]
  1. (defun addnth ( x n l / b m )
  2.     (setq m (vl-member-if-not '(lambda ( a ) (if (< 0 (setq n (1- n))) (setq b (cons a b)))) l))
  3.     (append (reverse b) (cons x m))
  4. )

Hi, maybe…
Code: [Select]
(if (> 1 (setq n (1- n)))
We can make this one 0 based index

bruno_vdh

  • Newt
  • Posts: 82
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #12 on: February 23, 2022, 09:38:38 AM »
bruno_vdh, I really need to spend some time and study your code; you have some very nice routines!
Thank you, always for the game.
Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth (l x n / m)  
  2.   (repeat (min n (length l)) (setq m (cons (car l) m) l (cdr l)))
  3.   (append (reverse m) (cons x l))
  4. )
Code - Auto/Visual Lisp: [Select]
  1. (defun add-nth (l x n)
  2.   (mapcar '(lambda (a b)
  3.              (cond ((= -1 (setq n (1- n))) x)
  4.                    ((minusp n) a)
  5.                    (b)
  6.              )
  7.            )
  8.           (cons x l)
  9.           (reverse (cons x (reverse l)))
  10.   )
  11. )

Lee Mac

  • Seagull
  • Posts: 12696
  • London, England
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #13 on: February 23, 2022, 05:26:19 PM »
Another, disqualified -
Code - Auto/Visual Lisp: [Select]
  1. (defun addnth ( x n l / b m )
  2.     (setq m (vl-member-if-not '(lambda ( a ) (if (< 0 (setq n (1- n))) (setq b (cons a b)))) l))
  3.     (append (reverse b) (cons x m))
  4. )

Hi, maybe…
Code: [Select]
(if (> 1 (setq n (1- n)))
We can make this one 0 based index

Oh I didn't realise it had changed...

JohnK

  • Administrator
  • Seagull
  • Posts: 10140
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #14 on: February 23, 2022, 06:00:33 PM »
Yes, it was a mistake on my part. Sorry.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #15 on: February 24, 2022, 01:17:12 PM »
Code: [Select]
(defun AddNth_ALE1 (x n L / g i r)
  (cond
    ( (>= n (setq g (length L))) (append L (list x)) )
    ( (/= 0 n nil L)
      (setq r (reverse L)    i (nth n L)     g (- g n 1))
      (while (/= n (length (setq r (cdr (member i r))))))
      (while (/= g (length (setq L (cdr (member i L))))))
      (append (reverse r) (cons x (cons i L)))   ; ex (append (reverse r) (list x) (cons i L))
    )
    ( (cons x L) )
  )
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #16 on: February 25, 2022, 04:24:53 AM »
Code: [Select]
(progn (setq aList (atoms-family 1)) (print " length ") (princ (length aList)) (princ "\n") (princ)) => " length " 4122
(setq NthP 4100)
--- Benchmark utility: In memory of Michael Puckett ---
    (ADDNTH_ALE1 "New" NTHP ALIST).......1375 / 3.2 <fastest>
    (ADD-NTH_RIB3 ALIST "New" NTHP)......3203 / 1.38
    (ADD-NTH_RIB2 ALIST "New" NTHP)......3391 / 1.3
    (ADD-NTH-DEXUS ALIST "New" NTHP).....3407 / 1.29
    (ADDNTH_LEE2 "New" NTHP ALIST).......3484 / 1.26
    (ADD-NTH_BRU4 ALIST "New" NTHP)......3485 / 1.26
    (ADD-NTH_BRU3 ALIST "New" NTHP)......3609 / 1.22
    (ADD-NTH_BRU2 ALIST "New" NTHP)......3640 / 1.21
    (ADD-NTH_RIB1 ALIST "New" NTHP)......3828 / 1.15
    (ADD-NTH_BRU1 ALIST "New" NTHP)......3906 / 1.13
    (ADD-NTH_WEL1 ALIST "New" NTHP)......3937 / 1.12
    (ADD-NTH-SE7EN ALIST "New" NTHP).....3969 / 1.11
    (ADDNTH_LEE1 "New" NTHP ALIST).......4406 / 1 <slowest>
Code: [Select]
(setq NthP 5 aList '(a b c d e f g h i l m n o p q r s t u v z))
--- Benchmark utility: In memory of Michael Puckett ---
    (ADD-NTH_BRU1 ALIST "New" NTHP)......1797 / 2.99 <fastest>
    (ADDNTH_LEE1 "New" NTHP ALIST).......1828 / 2.94
    (ADD-NTH_BRU2 ALIST "New" NTHP)......1828 / 2.94
    (ADD-NTH_BRU3 ALIST "New" NTHP)......1828 / 2.94
    (ADDNTH_ALE1 "New" NTHP ALIST).......1844 / 2.91
    (ADD-NTH-DEXUS ALIST "New" NTHP).....1890 / 2.84
    (ADD-NTH_RIB2 ALIST "New" NTHP)......1907 / 2.82
    (ADD-NTH_WEL1 ALIST "New" NTHP)......1922 / 2.8
    (ADD-NTH_RIB1 ALIST "New" NTHP)......1969 / 2.73
    (ADD-NTH-SE7EN ALIST "New" NTHP).....1984 / 2.71
    (ADD-NTH_BRU4 ALIST "New" NTHP)......2047 / 2.63
    (ADD-NTH_RIB3 ALIST "New" NTHP)......2078 / 2.59
    (ADDNTH_LEE2 "New" NTHP ALIST).......5375 / 1 <slowest>

domenicomaria

  • Bull Frog
  • Posts: 389
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #17 on: March 05, 2022, 02:12:42 AM »
Marc'Antonio
are
ADDNTH_ALE1 and AddNth_A
the same thing ?


Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #19 on: March 06, 2022, 08:53:33 AM »
Code: [Select]
(defun AddNth_ALE2 (x n L / Fx g)
  (defun Fx (n L)
    (repeat (/       n              1000) (setq L (Cd1000r L)))
    (repeat (/ (setq n (rem n 1000)) 100) (setq L (Cd100r  L)))
    (repeat (/ (setq n (rem n  100))  12) (setq L (Cd12r   L)))
    (repeat (rem n 12)                    (setq L (cdr     L)))
  )
  (cond
    ( (>= n (setq g (length L))) (append L (list x)) )
    ( (/= 0 n nil L)
      (append (reverse (Fx (- g n) (reverse L))) (cons x (Fx n L)))
    )
    ( (cons x L) )
  )
)
(defun Cd12r (L)
  (cddddr(cddddr(cddddr L)))
)
(defun Cd100r (L)
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr L)))))))))))))))))))))))))
)
(defun Cd1000r (L)
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr(cddddr(cddddr
  (cddddr(cddddr(cddddr(cddddr(cddddr l))))))))))))
  )))))))))))))))))))))))))))))))))))))))))))))))))
  )))))))))))))))))))))))))))))))))))))))))))))))))
  )))))))))))))))))))))))))))))))))))))))))))))))))
  )))))))))))))))))))))))))))))))))))))))))))))))))
  ))))))))))))))))))))))))))))))))))))))))))
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1314
  • Marco
Re: [challenge] A26 : Add the N'th element into a list at position
« Reply #20 on: March 06, 2022, 08:54:11 AM »
Code: [Select]
aList length = 4119   NthP = 4100
--- Benchmark utility: In memory of Michael Puckett ---
Elapsed milliseconds / relative speed for 4096 iteration(s):
    (ADDNTH_ALE2 "New" NTHP ALIST).......1656 / 4.46 <fastest>
    (ADDNTH_ALE1 "New" NTHP ALIST).......2969 / 2.49
    (ADD-NTH_RIB3 ALIST "New" NTHP)......6781 / 1.09
    (ADD-NTH_RIB2 ALIST "New" NTHP)......7078 / 1.04
    (ADD-NTH-DEXUS ALIST "New" NTHP).....7125 / 1.04
    (ADDNTH_LEE2 "New" NTHP ALIST).......7141 / 1.03
    (ADD-NTH_BRU4 ALIST "New" NTHP)......7390 / 1 <slowest>