Author Topic: (challenge) nth remover  (Read 16520 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
(challenge) nth remover
« on: January 21, 2004, 10:23:39 AM »
Your challenge is to create a nth remover. This procedure will take two arguments; a list and the nth number of the item in the list to remove. Demonstration:

(setq lst '(1 2 3 4))
(nthRemover lst 2)
 -> (1 2 4)

Get it? Your procedure should remove the nth item from a given list and return the list back to the user. This challenge should be a bit more challenging and make you think a bit more about the lisp language basics. This is gonna be a good one to see all the diff methods everyone uses. (I cant wait to see some of the little tricks you guys have to offer. I think this is gonna be a good one.) No "time" on this one, just come up with a cool procedure.

Let the coding begin. w00t!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) nth remover
« Reply #1 on: January 21, 2004, 11:05:28 AM »
Here is one ...
Code: [Select]

(defun NthRemover ( ndx lst / count newlist )
  (setq count 0)
  (repeat (length lst)
    (if (/= count ndx)
      (setq newlist (append newlist (list (car lst))))
    )
    (setq lst (cdr lst)
 count (1+ count)
    )
  )
  newlist
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
(challenge) nth remover
« Reply #2 on: January 21, 2004, 11:07:17 AM »
Here's another ..

Code: [Select]

(vl-load-com)


(defun nthremover (lst i / tmp)
  (if (and i (vl-consp lst) (not (minusp i)))
    (progn (repeat (min i (length lst))
             (setq tmp (cons (car lst) tmp)
                   lst (cdr lst)
             )
           )
           (append (reverse tmp) (cdr lst))
    )
    lst
  )
)


(setq lst '(1 2 3 4))

(nthRemover lst 2)   ; (1 2 4)
(nthRemover lst 6)   ; (1 2 3 4)
(nthRemover lst nil) ; (1 2 3 4)
(nthRemover nil 0)   ; nil
(nthRemover lst 0)   ; (2 3 4)
(nthRemover '(1 2 3 (4) 5) 0) ; (2 3 (4) 5)
(nthRemover '(1 2 3 (4) 5) 3) ; (1 2 3 5)



kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

SMadsen

  • Guest
(challenge) nth remover
« Reply #3 on: January 21, 2004, 11:26:46 AM »
Using the built-in VL-REMOVE-IF:

Code: [Select]
(defun cool (lst i / a)
  (setq a -1)
  (vl-remove-if (function (lambda (n)(= (setq a (1+ a)) i))) lst)
)

SMadsen

  • Guest
(challenge) nth remover
« Reply #4 on: January 21, 2004, 11:38:49 AM »
And a recursive one:

Code: [Select]
(defun nthRemove (lst i)
  (and (atom i)(setq i (cons (length lst) i)))
  (cond ((null lst) nil)
        ((/= (car i) (cdr i))
         (cons (car lst) (nthRemove (cdr lst) (cons (1- (car i)) (cdr i)))))
        ((cons (car lst) (nthRemove (cddr lst) (cons (1- (car i)) (cdr i)))))
  )
)

SMadsen

  • Guest
(challenge) nth remover
« Reply #5 on: January 21, 2004, 11:45:44 AM »
Waiting for someone to fall into the trap (= (nth i lst) item) ...  :)

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
(challenge) nth remover
« Reply #6 on: January 21, 2004, 11:50:34 AM »
Ok maybe I need to think of another fun challenge. (This one seems to easy after i think about it a bit and see how fast you guys jumped on this one.) :P
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) nth remover
« Reply #7 on: January 21, 2004, 11:55:51 AM »
That was/is a good challenge.
Tried to find a solution without a counter but got a headache. Any challenge that gives a headache is a good one.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
(challenge) nth remover
« Reply #8 on: January 21, 2004, 11:56:42 AM »
Stig. You may want to have another look at the recursive routine.
I think it is out by one index.

regards
kwb
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
(challenge) nth remover
« Reply #9 on: January 21, 2004, 12:03:04 PM »
Ok good. I was starting to think it was too easy. I saw a couple of resopnces real fast so I took a sec to think up a few lines of code and it was looking prety easy.

Oh, here's a goodie that came off the top of my head
(if (/= (if (not cntr) (setq cntr 0) cntr) item)
    (setq nlst (cons (car lst) nlst)))

Heres one aimed at giving you a "smart" recursion process.
(if (> (length (cdr lst)) 0)
    (nthRemover (cdr lst) item))

:P :P :P

Ok, enough playing arround :D

Later,
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) nth remover
« Reply #10 on: January 21, 2004, 12:13:00 PM »
Kerry, thanks for spotting the flaw! Here's corrected version, I hope:

Code: [Select]
(defun nthRemove (lst i)
  (and (atom i)(setq i (cons 0 i)))
  (cond ((null lst) nil)
        ((= (car i) (cdr i))(nthRemove (cdr lst) (cons (1+ (car i)) (cdr i))))
        ((cons (car lst) (nthRemove (cdr lst) (cons (1+ (car i)) (cdr i)))))
  )
)

daron

  • Guest
(challenge) nth remover
« Reply #11 on: January 21, 2004, 12:17:18 PM »
Well, I was getting a headache trying to do one non-vl, but here it is in the simplest form.
Code: [Select]

(defun nthremove (lst n)
     (vl-load-com)
     (vl-remove (nth n lst) lst)
)

SMadsen

  • Guest
(challenge) nth remover
« Reply #12 on: January 21, 2004, 12:19:54 PM »
Heh, Daron's first in the "(= (nth i lst) item)"-trap  :)

(nthremove '(1 2 3 1 2 3) 2)
(1 2 1 2)

daron

  • Guest
(challenge) nth remover
« Reply #13 on: January 21, 2004, 12:22:51 PM »
This is what I got:
Code: [Select]
Command: (nthremove '(a g b d s c 1 2 3 4 6 5 4 3) 8)
(A G B D S C 1 2 4 6 5 4)

I don't see the trap you're talking about?

daron

  • Guest
(challenge) nth remover
« Reply #14 on: January 21, 2004, 12:23:54 PM »
Okay, I see it now.