TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Lee Mac on March 28, 2010, 06:38:57 PM
-
The Challenge: to substitute an item in a list at a specified position.
Example:
(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:
(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)))
-
I wrote this back in June...
;;; 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...
(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
-
BTW Lee...
([color=red]Mac-[/color]Subst_at_n item (cdr lst) (1- n)))
-
Wowza... I made that change in the post box, something I'm never going to do again... Thanks alan
-
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.
-
;; 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)
)
-
http://www.theswamp.org/index.php?topic=14170.0
(http://www.theswamp.org/screens/index.php?dir=cab/&file=av-BigGrin.gif)
-
Quick & dirty & just for fun ...
(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)
)
-
and for even more fun ...
(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:
-
#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;
}
-
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.-
(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)
(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
-
int _tmain(int argc, _TCHAR* argv[])
sick puppy :-P
-
I thought you might like the lambda expression, I can format it like lisp :laugh:
for_each(
ints.begin(),
ints.end(),
[](int n) { cout << n << " "; });
-
Nice one Michael - I love that 'double-defun' trick :lol:
-
http://www.theswamp.org/index.php?topic=14170.0
(http://www.theswamp.org/screens/index.php?dir=cab/&file=av-BigGrin.gif)
I knew there would be another one, there was no way this operation was untouched :evil:
-
Some more fun :-)
(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))
-
Some more fun :-)
(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)
-
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.
-
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?
-
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:
-
I haven't seen one Lee, i just do a search at vlide's appropos, then figure out the arguments.
-
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.
-
I like to see the ingenious/interesting approaches to the problem, and really enjoy these kind of topics also :-)
-
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>
-
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.
-
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.
-
#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.
-
I cant compile this: where/what is "stdafx.h"? I dont got that one.
Compile it with visual c++ instead of gnu/gcc. (http://www.theswamp.org/screens/mp/poke.gif)
Subtitle: stdafx.h is a visual c++ centric pre-compiled header.
/guess
-
awe visual studio?! ...nope, never mind, not worth it.
-
It was a parody / lampoon post anyway, direct element access is fundamental to arrays in C, C++, VB, Python ...
-
just to complete the picture :)
(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 :(
-
/me 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.
-
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.
-
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 :)
-
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.
-
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>
:)
-
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.
-
got it - thanks.
-
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
-
Hooray, I also winner! :-D
says one of the swamp's best teachers
-
In that sense I have been a winner many times because of your solutions Evgeniy :-)
-
In that sense I have been a winner many times because of your solutions Evgeniy :-)
x2
-
Ok, now i'm just waiting for Greg and the "Group hug" post.