Author Topic: -={ Challenge }=- Subst at n  (Read 7278 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
-={ Challenge }=- Subst at n
« on: March 28, 2010, 06:38:57 PM »
The Challenge: to substitute an item in a list at a specified position.

Example:

Code: [Select]
(subst_at_n 2 '(0 1 2 3 4 5) 4)

=>  (0 1 2 3 2 5)


I figure there are quite a few ways to do this, so the challenge might be interesting.

I apologise in advance if this has been addressed in the past - I did do a search, but was perhaps not thorough enough.

My entry:

Code: [Select]
 (defun Subst_at_n (item lst n)
    (if lst
      (cons (if (zerop n) item (car lst))
            (Subst_at_n item (cdr lst) (1- n)))
      (car lst)))
« Last Edit: March 29, 2010, 06:18:30 AM by Lee Mac »

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Subst at n
« Reply #1 on: March 28, 2010, 06:49:22 PM »
I wrote this back in June...

Code: [Select]
;;; Replace nth item in list
;;; #Nth - nth number in list to replace
;;; #New - replacement item
;;; #List - list to process
;;; Alan J. Thompson, 06.16.09
(defun AT:NthReplace (#Nth #New #List / #Count)
  (setq #Count -1)
  (mapcar '(lambda (x)
             (if (eq #Nth (setq #Count (1+ #Count)))
               #New
               x
             ) ;_ if
           ) ;_ lambda
          #List
  ) ;_ mapcar
) ;_ defun

This would be better...
Code: [Select]
(defun AT:NthReplace (#Nth #New #List / #Count)
  (if (zerop #Nth)
    (cons #New (cdr #List))
    (progn (setq #Count -1)
           (mapcar (function (lambda (x)
                               (if (eq #Nth (setq #Count (1+ #Count)))
                                 #New
                                 x
                               ) ;_ if
                             ) ;_ lambda
                   ) ;_ function
                   #List
           ) ;_ mapcar
    ) ;_ progn
  ) ;_ if
) ;_ defun
« Last Edit: March 29, 2010, 11:58:35 AM by alanjt »
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Subst at n
« Reply #2 on: March 28, 2010, 07:05:06 PM »
BTW Lee...

Code: [Select]
([color=red]Mac-[/color]Subst_at_n item (cdr lst) (1- n)))
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #3 on: March 28, 2010, 07:12:09 PM »
Wowza... I made that change in the post box, something I'm never going to do again... Thanks alan

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Subst at n
« Reply #4 on: March 28, 2010, 07:15:40 PM »
Wowza... I made that change in the post box, something I'm never going to do again... Thanks alan

:) I've done the exact same thing.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: -={ Challenge }=- Subst at n
« Reply #5 on: March 28, 2010, 07:32:31 PM »
Code: [Select]
;; variation by CAB
(defun replace_CAB (lst i itm)
  (setq i (1+ i))
  (mapcar '(lambda (x) (if (zerop (setq i (1- i))) itm x)) lst)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Subst at n
« Reply #7 on: March 28, 2010, 07:36:13 PM »
Quick & dirty & just for fun ...

Code: [Select]
(defun _ReplaceNth ( n item lst / f )

    (defun f ()
        (if (zerop n)
            (progn (defun f () x) item)
            (progn (setq n (1- n)) x)
        )    
    )            
        
    (mapcar (function (lambda (x) (f))) lst)
        
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Subst at n
« Reply #8 on: March 28, 2010, 07:39:25 PM »
and for even more fun ...

Code: [Select]
(defun _ReplaceNth ( n item lst )
    (   (lambda (f) (mapcar (function (lambda (x) (f))) lst))
        (lambda ( )
            (if (zerop n)
                (progn (defun f () x) item)
                (progn (setq n (1- n)) x)
            )    
        )            
    )
)

 :lol:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: -={ Challenge }=- Subst at n
« Reply #9 on: March 28, 2010, 07:50:39 PM »
Code: [Select]
#include "stdafx.h"
#include <array>
#include <iostream>

using namespace std;
using namespace std::tr1;

int _tmain(int argc, _TCHAR* argv[])
{
  array<int,9> ints = {1,2,3,4,5,6,7,8,9};
  ints[2] = 9;
  for_each(ints.begin(), ints.end(), [](int n) { cout << n << " "; });
  cout << endl;
  system("pause");
  return 0;
}
« Last Edit: March 28, 2010, 07:56:26 PM by Daniel »

LE3

  • Guest
Re: -={ Challenge }=- Subst at n
« Reply #10 on: March 28, 2010, 07:55:03 PM »
Be careful don't end like me and be tagged for ever and be part of a real facts...  :lol:  :evil:  :lmao:

Here is something from my very old lisp coding school.-
Code: [Select]
(defun substAtPosition  (newItem position oldList / cont lth lst)
  (setq cont 0
lth  (length oldList))
  (while (/= cont position)
    (setq lst (append lst (list (nth cont oldList)))
 cont (1+ cont)))
  (setq lst (append lst (list newItem))
cont (1+ cont))
  (while (< cont lth)
    (setq lst (append lst (list (nth cont oldList)))
 cont (1+ cont)))
  lst)
 
Quote
(substAtPosition 2 4 '(0 1 2 3 4 5))
(0 1 2 3 2 5)

Wowza... I made that change in the post box, something I'm never going to do again... Thanks alan


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: -={ Challenge }=- Subst at n
« Reply #11 on: March 28, 2010, 10:36:52 PM »

Quote
int _tmain(int argc, _TCHAR* argv[])

sick puppy  :-P
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.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: -={ Challenge }=- Subst at n
« Reply #12 on: March 28, 2010, 11:41:46 PM »
I thought you might like the lambda expression, I can format it like lisp  :laugh:

Code: [Select]
for_each(
 ints.begin(),
    ints.end(),
        [](int n) { cout << n << " "; });

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #13 on: March 29, 2010, 06:20:27 AM »
Nice one Michael - I love that 'double-defun' trick  :lol:

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #14 on: March 29, 2010, 06:22:28 AM »
http://www.theswamp.org/index.php?topic=14170.0


I knew there would be another one, there was no way this operation was untouched  :evil:

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #15 on: March 29, 2010, 06:45:52 AM »
Some more fun  :-)

Code: [Select]
(defun Subst_at_n (item lst n / Sub)

  (defun Sub (a b c)
    (if b
      (cons
        (if (zerop c)
          (progn
            (defun Sub (a b c)
              (if b
                (cons (car b)
                      (Sub a (cdr b) c))))
            a)
          (car b))
        (Sub a (cdr b) (1- c)))
      (car b)))

  (Sub item lst n))

wizman

  • Bull Frog
  • Posts: 290
Re: -={ Challenge }=- Subst at n
« Reply #16 on: March 29, 2010, 09:33:40 AM »
Some more fun  :-)

Code: [Select]
(defun Subst_at_n (item lst n / Sub)

  (defun Sub (a b c)
    (if b
      (cons
        (if (zerop c)
          (progn
            (defun Sub (a b c)
              (if b
                (cons (car b)
                      (Sub a (cdr b) c))))
            a)
          (car b))
        (Sub a (cdr b) (1- c)))
      (car b)))

  (Sub item lst n))

Nice one Lee.  Just throwing this one for the record:
(acet-list-put-nth a lst n)

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: -={ Challenge }=- Subst at n
« Reply #17 on: March 29, 2010, 09:37:19 AM »
I dont understand this one at all.
http://www.theswamp.org/index.php?topic=32800.msg382733#msg382733

It's like its written in a different language.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #18 on: March 29, 2010, 09:38:18 AM »

Nice one Lee.  Just throwing this one for the record:
(acet-list-put-nth a lst n)

Thanks Wiz, does documentation exist for the acet-* functions, I know there is a few listed over at AfraLISP, but is there a full list anywhere?

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #19 on: March 29, 2010, 09:39:40 AM »
I dont understand this one at all.
http://www.theswamp.org/index.php?topic=32800.msg382733#msg382733

It's like its written in a different language.

Its the one above it written as anonymous functions... or 'obfuscated' as Michael would say  :evil:

wizman

  • Bull Frog
  • Posts: 290
Re: -={ Challenge }=- Subst at n
« Reply #20 on: March 29, 2010, 09:45:26 AM »
I haven't seen one Lee, i just do a search at vlide's appropos, then figure out the arguments. 

LE3

  • Guest
Re: -={ Challenge }=- Subst at n
« Reply #21 on: March 29, 2010, 10:49:06 AM »
In any of these challenges, do we've known who or which of the proposals has won? or do we just end up uploading all the possible options, for future reference? and decide which one is the best to fit the current needs, can we or who will judge them? or can we simple open a topic to place/write a function to do some specific task? and this can end being like a library reference - it is challenge the proper wording?

And yes I like these type of topics.

Sorry folks, got to ask, hope you won't mind, thanks.

 

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #22 on: March 29, 2010, 11:03:09 AM »
I like to see the ingenious/interesting approaches to the problem, and really enjoy these kind of topics also  :-)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Subst at n
« Reply #23 on: March 29, 2010, 11:12:45 AM »
In any of these challenges, do we've known who or which of the proposals has won? or do we just end up uploading all the possible options, for future reference? and decide which one is the best to fit the current needs, can we or who will judge them? or can we simple open a topic to place/write a function to do some specific task? and this can end being like a library reference - it is challenge the proper wording?

And yes I like these type of topics.

Sorry folks, got to ask, hope you won't mind, thanks.

The winner is the body of knowledge that the swamp represents. I'm not interested in anything else, in particular nonsense like "Person A's submission is better than Person B's". That's the fastest way to have people stop posting their ideas or deleting posts when "better" solutions are posted. Sometimes we bench performance, and that's cool, because performance itself is only one of many things that constitutes a good algorithm. </opinion>
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #24 on: March 29, 2010, 11:15:00 AM »
In any of these challenges, do we've known who or which of the proposals has won? or do we just end up uploading all the possible options, for future reference? and decide which one is the best to fit the current needs, can we or who will judge them? or can we simple open a topic to place/write a function to do some specific task? and this can end being like a library reference - it is challenge the proper wording?

And yes I like these type of topics.

Sorry folks, got to ask, hope you won't mind, thanks.

The winner is the body of knowledge that the swamp represents. I'm not interested in anything else, in particular nonsense like "Person A's submission is better than Person B's". That's the fastest way to have people stop posting their ideas or deleting posts when "better" solutions are posted. Sometimes we bench performance, and that's cool, because performance itself is only one of many things that constitutes a good algorithm. </opinion>

I couldn't have put it better, completely agree.

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: -={ Challenge }=- Subst at n
« Reply #25 on: March 29, 2010, 11:27:51 AM »
Challenges are all about learning in my opinion -i.e. "We are supposed to teach each other not beat each other". (c) Se7en inc.

I think LE3 is on to something though (each user should pick and choose the solution best to fit their need(s)). ...we need to add more comments and be maybe be more academic about our solutions.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: -={ Challenge }=- Subst at n
« Reply #26 on: March 29, 2010, 11:30:21 AM »
Code: [Select]
#include "stdafx.h"
#include <array>
#include <iostream>

using namespace std;
using namespace std::tr1;

int _tmain(int argc, _TCHAR* argv[])
{
  array<int,9> ints = {1,2,3,4,5,6,7,8,9};
  ints[2] = 9;
  for_each(ints.begin(), ints.end(), [](int n) { cout << n << " "; });
  cout << endl;
  system("pause");
  return 0;
}

I cant compile this: where/what is "stdafx.h"? I dont got that one.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Subst at n
« Reply #27 on: March 29, 2010, 11:36:05 AM »
I cant compile this: where/what is "stdafx.h"? I dont got that one.

Compile it with visual c++ instead of gnu/gcc.

Subtitle: stdafx.h is a visual c++ centric pre-compiled header.

/guess
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: -={ Challenge }=- Subst at n
« Reply #28 on: March 29, 2010, 11:38:56 AM »
awe visual studio?! ...nope, never mind, not worth it.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Subst at n
« Reply #29 on: March 29, 2010, 11:45:01 AM »
It was a parody / lampoon post anyway, direct element access is fundamental to arrays in C, C++, VB, Python ...
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: -={ Challenge }=- Subst at n
« Reply #30 on: March 29, 2010, 11:47:34 AM »
just to complete the picture :)
Code: [Select]
(defun SubstNth (n item lst / arr)
  (vlax-safearray-put-element
    (vlax-safearray-fill
      (setq arr (vlax-make-safearray vlax-vbInteger (cons 0 (1- (length lst)))))
      lst
    )
    n
    item
  )
  (vlax-safearray->list arr)
)
don't benchmark it :(

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: -={ Challenge }=- Subst at n
« Reply #31 on: March 29, 2010, 11:51:13 AM »
* Se7en giving Visual Studio a try.

Oh my goodness?! Ok, so i cant just compile a file by opening it up in the VS IDE and hitting "compile" so i started to "build a project". ...You've got to be kidding me?! This thing is crazy!  And you guys think that Vim/Mingw is complicated?!

;p

It was a parody / lampoon post anyway, direct element access is fundamental to arrays in C, C++, VB, Python ...
Yeah i know but it too was greek to me and i wanted to check it out. :)
array[nu] = blah.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Subst at n
« Reply #32 on: March 29, 2010, 11:51:41 AM »
Awesome VovKa. I had thought of that one to be honest but the fact it would be challenging to deal with heterogeneous data stopped me. Glad you posted one regardless.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

LE3

  • Guest
Re: -={ Challenge }=- Subst at n
« Reply #33 on: March 29, 2010, 11:55:39 AM »
In any of these challenges, do we've known who or which of the proposals has won? or do we just end up uploading all the possible options, for future reference? and decide which one is the best to fit the current needs, can we or who will judge them? or can we simple open a topic to place/write a function to do some specific task? and this can end being like a library reference - it is challenge the proper wording?

And yes I like these type of topics.

Sorry folks, got to ask, hope you won't mind, thanks.

The winner is the body of knowledge that the swamp represents. I'm not interested in anything else, in particular nonsense like "Person A's submission is better than Person B's". That's the fastest way to have people stop posting their ideas or deleting posts when "better" solutions are posted. Sometimes we bench performance, and that's cool, because performance itself is only one of many things that constitutes a good algorithm. </opinion>

so Mike, to clear up my English class 102, challenge is not a competition? or I am way off/away of the tree? if that it is well, something was lost on the translation :)

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: -={ Challenge }=- Subst at n
« Reply #34 on: March 29, 2010, 12:03:46 PM »
LE3, When i posted the first "challenge" i choose the word because of of its many meanings. -i.e.(that is to say) "Challenge" as more of a "challenge the brain" meaning and/or a fun (not for real) task. 
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Subst at n
« Reply #35 on: March 29, 2010, 12:05:54 PM »
so Michael, to clear up my English class 102, challenge is not a competition?

You asked who the winner was, I answered you.

The challenge is whether one can pen a solution the problem, not whether Bob is better than Bill.

</opinion>

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

nivuahc

  • Guest
Re: -={ Challenge }=- Subst at n
« Reply #36 on: March 29, 2010, 12:06:35 PM »
In any of these challenges, do we've known who or which of the proposals has won? or do we just end up uploading all the possible options, for future reference? and decide which one is the best to fit the current needs, can we or who will judge them? or can we simple open a topic to place/write a function to do some specific task? and this can end being like a library reference - it is challenge the proper wording?

And yes I like these type of topics.

Sorry folks, got to ask, hope you won't mind, thanks.

The winner is the body of knowledge that the swamp represents. I'm not interested in anything else, in particular nonsense like "Person A's submission is better than Person B's". That's the fastest way to have people stop posting their ideas or deleting posts when "better" solutions are posted. Sometimes we bench performance, and that's cool, because performance itself is only one of many things that constitutes a good algorithm. </opinion>

so Mike, to clear up my English class 102, challenge is not a competition? or I am way off/away of the tree? if that it is well, something was lost on the translation :)

The "winner" is anyone who walks away from the challenge having learned something. Like me. I don't participate in the challenges themselves, but I read every one of them with great interest.

LE3

  • Guest
Re: -={ Challenge }=- Subst at n
« Reply #37 on: March 29, 2010, 12:10:20 PM »
got it - thanks.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: -={ Challenge }=- Subst at n
« Reply #38 on: March 29, 2010, 01:10:36 PM »
The "winner" is anyone who walks away from the challenge having learned something. Like me. I don't participate in the challenges themselves, but I read every one of them with great interest.

Hooray, I also winner!   :-D

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: -={ Challenge }=- Subst at n
« Reply #39 on: March 29, 2010, 01:37:53 PM »
Hooray, I also winner!  :-D

says one of the swamp's best teachers
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Subst at n
« Reply #40 on: March 29, 2010, 02:04:02 PM »
In that sense I have been a winner many times because of your solutions Evgeniy  :-)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: -={ Challenge }=- Subst at n
« Reply #41 on: March 29, 2010, 04:24:53 PM »
In that sense I have been a winner many times because of your solutions Evgeniy  :-)
x2
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: -={ Challenge }=- Subst at n
« Reply #42 on: March 29, 2010, 05:15:17 PM »
Ok, now i'm just waiting for Greg and the "Group hug" post.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org