Author Topic: (Challenge) Summation  (Read 14307 times)

0 Members and 1 Guest are viewing this topic.

LE

  • Guest
Re: (Challenge) Summation
« Reply #15 on: January 17, 2007, 10:47:27 AM »
Here is mine:

Code: [Select]
;;;GETVALS
;;;_$ (getvals lst)
;;;(("two" . 15) ("three" . 18) ("one" . 12))
(defun getvals (lst / total tmp l i)
  (setq total 0
tmp lst
i 0)
  (setq items (acad_strlsort (mapcar 'car lst)))
  (while (< i (length items))
    (if (setq add (cadr (assoc (nth i items) tmp)))
      (progn
(if (not (setq value (cdr (assoc (nth i items) l))))
  (setq value 0))
(setq dot (cons (nth i items) (+ add value))
      tmp (vl-remove (list (nth i items) add) tmp))))
    (if (assoc (nth i items) l)
      (setq l (subst dot (assoc (nth i items) l) l))
      (setq l (cons dot l)))
    (setq i (1+ i)))
  l)

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: (Challenge) Summation
« Reply #16 on: January 17, 2007, 11:12:30 AM »
There is some amazing code in this thread. Im trying to study it all but right at this moment i find myself stuck on Evgeniy's summation_2 :)

I think it maybe time to start up that vlide. :(   :)

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

Donate to TheSwamp.org

LE

  • Guest
Re: (Challenge) Summation
« Reply #17 on: January 17, 2007, 12:15:32 PM »
Here is again my function, now using the mapcar style, and the same algorithm:

Code: [Select]
(defun getvals (lst / value add total tmp l items)
  (setq total 0
tmp lst)
  (mapcar (function
    (lambda (i)
      (if (setq add (cadr (assoc i tmp)))
(progn
  (setq value (if (not (cdr (assoc i l)))
0
(cdr (assoc i l))))
  (setq dot (cons i (+ add value))
tmp (vl-remove (list i add) tmp))))
      (setq l (if (assoc i l)
(subst dot (assoc i l) l)
(cons dot l)))))
  (setq items (acad_strlsort (mapcar 'car lst))))
  l)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Summation
« Reply #18 on: January 17, 2007, 12:26:10 PM »
There is some amazing code in this thread. Im trying to study it all but right at this moment i find myself stuck on Evgeniy's summation_2 :)

I think it maybe time to start up that vlide. :(   :)



The best code in this thread has written gile!

LE

  • Guest
Re: (Challenge) Summation
« Reply #19 on: January 17, 2007, 12:28:26 PM »
The best code in this thread has written gile!

How do you came up with that conclusion?  :-o

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: (Challenge) Summation
« Reply #20 on: January 17, 2007, 12:30:45 PM »
The best code in this thread has written gile!

I haven't gotten to look at all of them yet but so far I agree. Very nice code! (clean!)


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

Donate to TheSwamp.org

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Summation
« Reply #21 on: January 17, 2007, 12:35:13 PM »
How do you came up with that conclusion?  :-o

Excuse!
It only my opinion.
Its code is minimal on the size and at small lists it the fastest.

LE

  • Guest
Re: (Challenge) Summation
« Reply #22 on: January 17, 2007, 12:37:29 PM »
How do you came up with that conclusion?  :-o

Excuse!
It only my opinion.
Its code is minimal on the size and at small lists it the fastest.

Thanks.

I am not fan of recursion at all, I normally avoid that in all my code, I think I had use it once in C++ and maybe once in Autolisp.
« Last Edit: January 17, 2007, 12:43:11 PM by LE »

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: (Challenge) Summation
« Reply #23 on: January 17, 2007, 12:38:49 PM »
How do you came up with that conclusion?  :-o

I dont think theres any thing bad about anyones code LE, I agree that gile's code was very good because it was very simple and easy. I know I was shocked at how easy the code was.  I learned tons from that proced. ...all of them so far.  No judging, just learning.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

LE

  • Guest
Re: (Challenge) Summation
« Reply #24 on: January 17, 2007, 12:44:41 PM »
Please;

My comment is not to say what it is best or worst... I simple asked how it went to that conclusion.... :)

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
Re: (Challenge) Summation
« Reply #25 on: January 17, 2007, 12:58:22 PM »
Oh okay. Gotcha. :)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

LE

  • Guest
Re: (Challenge) Summation
« Reply #26 on: January 17, 2007, 01:12:16 PM »
Here are some comments about Recursion vs Iteration.

http://www.cs.wisc.edu/~vernon/cs367/notes/6.RECURSION.html

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (Challenge) Summation
« Reply #27 on: January 17, 2007, 01:26:54 PM »
Nice link there Luis.
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.

gile

  • Gator
  • Posts: 2506
  • Marseille, France
Re: (Challenge) Summation
« Reply #28 on: January 17, 2007, 03:22:35 PM »
Quote
The best code in this thread has written gile!

First, many thanks to Evgeniy. Comming from you, the comment realy touch me. I admire the way you write LISP (your style), I consider you as a recursion master and learn a lot reading your codes.

About recursion form, even it's often slower and needs more memory than an iterative form, I like it it very much for its elegancy and its way of thinking (and when I posted my code, recursive form was the rule).

In my opinion, all posted codes are interesting because they're all different.

I enjoy these kind of challenge.
« Last Edit: January 17, 2007, 03:27:44 PM by gile »
Speaking English as a French Frog

LE

  • Guest
Re: (Challenge) Summation
« Reply #29 on: January 17, 2007, 04:10:05 PM »
In my opinion, all posted codes are interesting because they're all different.

Ditto^