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

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
(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: 10605
(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: 10605
(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.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
(challenge) nth remover
« Reply #15 on: January 21, 2004, 12:28:22 PM »
>Heh, Daron's first in the trap
heyheyhey.............
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
(challenge) nth remover
« Reply #16 on: January 21, 2004, 12:30:41 PM »
Quote from: Daron
Well, I was getting a headache trying to do one non-vl, but here it is in the simplest form.
Come on Daron, Keep trying. This challege has to do with Lists --That what lisp was designed for.  ...break it down.  

ok, hows this? Picture this being a Real Life situation with Real Life objects. -e.g. you have a set of drawings and you want to remove the 5th one. How would/could you do that in RL? Translate that process to code.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) nth remover
« Reply #17 on: January 21, 2004, 12:34:50 PM »
Sorry, didn't mean to laugh. Sorry Daron. I just expected to see it pop up among the solutions to this challenge.

I made a pretty grave error in the recursion before. Feel free to laugh very loudly and long at that one!

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
(challenge) nth remover
« Reply #18 on: January 21, 2004, 12:37:40 PM »
>I made a pretty grave error in the recursion before. Feel free to laugh very loudly and long at that one!

...yeah me too. :D
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

daron

  • Guest
(challenge) nth remover
« Reply #19 on: January 21, 2004, 12:40:14 PM »
Okay, I took to disecting this:
Code: [Select]

(defun cool (lst i / a)
  (setq a -1)
  (vl-remove-if (function (lambda (n)(= (setq a (1+ a)) i))) lst)
)

How does the lambda function work here? I see that function can be removed if stated like '(lambda (n)(= (setq a (1+ a))i))) lst) but that doesn't help.

SMadsen

  • Guest
(challenge) nth remover
« Reply #20 on: January 21, 2004, 12:59:46 PM »
VL-REMOVE-IF removes the current element of lst if the predicate-function returns anything else than nil.
The normal way of thinking about it is to evaluate each item within the LAMBDA. For example something like:

(vl-remove-if (function (lambda (n)(> n 2))) '(1 2 3 4 1 2 3 4))
-> (1 2 1 2)

But, because LAMBDA will run through every item of the list by merely supplying it as an argument, there is no need to evaluate it further.
For example, if the predicate function always returned T, it would remove each item as it is supplied to LAMBDA's argument n:

(vl-remove-if (function (lambda (n) T)) '(1 2 3 4 1 2 3 4))
-> nil

.. and if it always returned nil then no items would be removed:

(vl-remove-if (function (lambda (n) nil)) '(1 2 3 4 1 2 3 4))
-> (1 2 3 4 1 2 3 4)

To confirm that LAMBDA do evaluate each item, you could do something like:

(vl-remove-if (function (lambda (n)(princ n))) '(1 2 3 4 1 2 3 4))
-> 12341234nil

Of course, it returns nil because it removes each item (PRINC returns a null character, not nil).

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) nth remover
« Reply #21 on: January 21, 2004, 01:01:22 PM »
Ok here is another, but it will only work with a list of unique values.
Code: [Select]

(defun nthremove ( ndx lst )
  (append
    (reverse (cdr (member (nth ndx lst) (reverse lst))))
    (cdr (member (nth ndx lst) lst))
  )  
)
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

daron

  • Guest
(challenge) nth remover
« Reply #22 on: January 21, 2004, 01:03:48 PM »
You should write the help files. You do a better job of explaining it than the help files can. Thanks.

SMadsen

  • Guest
(challenge) nth remover
« Reply #23 on: January 21, 2004, 01:13:38 PM »
Thanks Daron, could you suggest that to Autod€sk? Just kidding.

Actually the last sentence wasn't correct. PRINC without argument returns a null character - otherwise is returns the evaluated argument. But in either case it's not nil. Unless it prints a nil argument .. umm, better stop now ..

daron

  • Guest
(challenge) nth remover
« Reply #24 on: January 21, 2004, 01:15:43 PM »
It seems through AUGI that you might have some pretty deep connections with Autodesk. They just don't know it yet.

SMadsen

  • Guest
(challenge) nth remover
« Reply #25 on: January 21, 2004, 01:34:36 PM »
Daron, I wouldn't know as long I can't make a buck on it  :)

Keith, that APPEND thing gave me an idea to cut out the item. So here's another:

Code: [Select]
(defun removeNth (lst i / a b)
  (cond ((> i (length lst)) lst)
        ((setq a lst b (reverse lst))
         (append (reverse (repeat (- (length b) i) (setq b (cdr b))))
                 (repeat (1+ i) (setq a (cdr a))))
        )
  )
)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) nth remover
« Reply #26 on: January 21, 2004, 04:00:41 PM »
Did you notice that it didn't require the setting of any additional variables.
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

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
(challenge) nth remover
« Reply #27 on: January 21, 2004, 05:04:34 PM »
I had an idea too. Now i didnt have much time on this, but here it is in rough form so excuse the mess.

Stig, Remeber that problem we were having with localising var's in a recursive procedure? Heres the answer (And it was soooo simple i cant believe we didnt think of it. :? :lol:)

Ill comment this code later,

Code: [Select]
(defun nthRemover (lst item / nlst cntr)
  (defun Nested-nthRemover (lst item)
    (if (/= (if (not cntr) (setq cntr 0) cntr) item)
      (setq nlst (cons (car lst) nlst)))
    (setq cntr (1+ cntr))
    (if (> (length (cdr lst)) 0)
      (Nested-nthRemover (cdr lst) item)
      (setq cntr nil))
    (reverse nlst))
  (Nested-nthRemover lst item))
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) nth remover
« Reply #28 on: January 21, 2004, 05:17:50 PM »
John, do you remember that global variables weren't a problem in recursive functions? Well, you just made them 'global' to the nested function :)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
(challenge) nth remover
« Reply #29 on: January 21, 2004, 05:31:44 PM »
Oh yeah. Darn! And i thought i was smart there for a second. :lol:
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) nth remover
« Reply #30 on: January 21, 2004, 06:02:23 PM »
Didn't say it wasn't a smart way to do it. It is smart!

SMadsen

  • Guest
(challenge) nth remover
« Reply #31 on: January 21, 2004, 06:08:59 PM »
Quote from: Keith
Did you notice that it didn't require the setting of any additional variables.
Yep. Slick and short. Limited to first occurence of an item but slick and short nonetheless.

If only there was a NTH_MEMBER function. That's why the idea of (repeat i (setq lst (cdr lst))) - only it didn't turn out as clean as yours.

daron

  • Guest
(challenge) nth remover
« Reply #32 on: January 21, 2004, 06:27:11 PM »
I was trying to use member at first. I wanted the member of the nth + 1 part of the list. That was easy, but the first half is where I was having trouble; gathering the list 1- to the nth of the list.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
(challenge) nth remover
« Reply #33 on: January 21, 2004, 09:22:11 PM »
Quote from: SMadsen
If only there was a NTH_MEMBER function. That's why the idea of (repeat i (setq lst (cdr lst))) - only it didn't turn out as clean as yours.


Like this?
Code: [Select]
(defun nthMember (lst item / cntr pos)
  (mapcar
    '(lambda (x)
      (if (not cntr) (setq cntr 0) (setq cntr (1+ cntr)))
       (if (eq x item)
         (setq pos cntr)
         )
       )
    lst
    )
  pos
 )

Command: (nthmember '(1 2 3 4) 2)
> 1
Command: (nthmember '(1 2 3 4) 1)
> 0
Command: (nthmember '(1 2 3 4) 6)
> nil
Command: (nthmember '(1 2 3 4) 3)
> 2
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) nth remover
« Reply #34 on: January 22, 2004, 04:26:42 AM »
Nah, I meant more like: get the rest of the list from nth index

Code: [Select]
(defun nthmem (lst i)
  (cond ((zerop i) lst)
        ((repeat i (setq lst (cdr lst))))
  )
)


(nthmem '(0 1 2 3 4 5) 2)
(2 3 4 5)
(nthmem '(0 1 2 3 4 5) 0)
(0 1 2 3 4 5)
(nthmem '(0 1 2 3 4 5) 4)
(4 5)
(reverse (nthmem (reverse '(0 1 2 3 4 5)) 4))
(0 1)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
(challenge) nth remover
« Reply #35 on: January 22, 2004, 09:21:08 AM »
Oh, Got cha. ...hey thats cool.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
(challenge) nth remover
« Reply #36 on: January 22, 2004, 09:31:52 AM »
NEXT..................

since i didn't get to play in this one, i'm ready for another. :D
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
(challenge) nth remover
« Reply #37 on: January 22, 2004, 09:39:44 AM »
Another?!  ...Ok let me try to think up another idea.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org