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

0 Members and 1 Guest are viewing this topic.

#### JohnK

• 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.

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)

#### 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

• 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

• 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]
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...