Author Topic: -={ Challenge }=- Nested Summing  (Read 9034 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: -={ Challenge }=- Nested Summing
« Reply #15 on: July 17, 2010, 09:14:41 AM »
I tried a C# solution, but it's not very elegant  :|


A work of art... I see you caught the point2d -3d too  8-)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: -={ Challenge }=- Nested Summing
« Reply #16 on: July 17, 2010, 09:19:38 AM »
Quote
A work of art...
Thanks, but I think it's very exaggerated...
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: -={ Challenge }=- Nested Summing
« Reply #17 on: July 17, 2010, 10:49:14 AM »
Another way:

Superb Gile - thinking outside the box  :lol:

LE3

  • Guest
Re: -={ Challenge }=- Nested Summing
« Reply #18 on: July 17, 2010, 12:03:30 PM »
I will try with this old style school and mickey mouse approach and solution (and without looking into any of already code posted), compared to you masters of lisp  :-P
... and don't laugh..... ok you can  :lol:

Quote
(setq lst '( 1 2 ( 3 4 ( 1 ))))
Command: tst
Total: 11

(setq lst '( 1 2 ( 3 4 ( 1 )) ( 3 4 ( 1 ))))
Command: tst
Total: 19

Code: [Select]
(defun sum (lst / lth cont)
(setq lth (length lst) cont 0)
(while (< cont lth)
  (setq item (nth cont lst))
  (cond
  ((eq (type item) 'list) (sum item))
  ((eq (type item) 'int) (setq total (+ item total))))
  (setq cont (1+ cont))))

(defun summer (/ lth cont total)
(setq lth (length lst) cont 0 total 0)
(while (< cont lth)
  (setq item (nth cont lst))
  (cond
  ((eq (type item) 'list) (sum item))
  ((eq (type item) 'int) (setq total (+ item total))))
  (setq cont (1+ cont))
  total))

(defun c:tst () (princ "\nTotal: ") (princ (summer)) (princ))

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: -={ Challenge }=- Nested Summing
« Reply #19 on: July 17, 2010, 12:11:05 PM »
Thanks for joining in the fun Luis  :evil:

Try with a dotted pair  :-)

LE3

  • Guest
Re: -={ Challenge }=- Nested Summing
« Reply #20 on: July 17, 2010, 12:40:59 PM »
let's see...
Quote
(setq lst '( 1 2 ( 3 4 ( 1 ) (3 . 4))))
Command: tst
Total: 18
Code: [Select]
(defun isDottedPair (item) (and (car item) (cdr item) (atom (car item)) (atom (cdr item))))
   
(defun sum (lst / lth cont)
(setq lth (length lst) cont 0)
(while (< cont lth)
  (setq item (nth cont lst))
  (cond
  ((and (eq (type item) 'list) (isDottedPair item)) (setq total (+ (car item) (cdr item) total)))
  ((eq (type item) 'list) (sum item))
  ((eq (type item) 'int) (setq total (+ item total))))
  (setq cont (1+ cont))))

(defun summer (/ lth cont total)
(setq lth (length lst) cont 0 total 0)
(while (< cont lth)
  (setq item (nth cont lst))
  (cond
  ((and (eq (type item) 'list) (isDottedPair item)) (setq total (+ (car item) (cdr item) total)))
  ((eq (type item) 'list) (sum item))
  ((eq (type item) 'int) (setq total (+ item total))))
  (setq cont (1+ cont))
  total))

Thanks for joining in the fun Luis  :evil:

Try with a dotted pair  :-)

pkohut

  • Guest
Re: -={ Challenge }=- Nested Summing
« Reply #21 on: July 17, 2010, 12:49:15 PM »
Lee,

I always liked this one from the Compuserve days:
-David

Ah, yes. 100 virtual points for anyone that can remember the moderator's name?

LE3

  • Guest
Re: -={ Challenge }=- Nested Summing
« Reply #22 on: July 17, 2010, 12:53:00 PM »
Hi Gile,
Just tried this, and does not return the right sum... ?

(1 2 3)
_$ (SUMLIST lst)
6 ;; ok

(1 2 (3 4 (1)))
_$ (SUMLIST lst)
6 ;; ?

Another way:

Code: [Select]
(defun sumlist (l)
  (eval
    (read
      (strcat "(+ "
      (vl-string-translate "(.)" "   " (vl-princ-to-string l1))
      ")"
      )
    )
  )
)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: -={ Challenge }=- Nested Summing
« Reply #23 on: July 17, 2010, 01:31:29 PM »
Oops !!!

I forgot to change l1 for l after debugging...
Try with corrected code.
Speaking English as a French Frog

LE3

  • Guest
Re: -={ Challenge }=- Nested Summing
« Reply #24 on: July 17, 2010, 02:22:25 PM »
I'll check... Je sais que c'était quelque chose de simple

Oops !!!

I forgot to change l1 for l after debugging...
Try with corrected code.

pkohut

  • Guest
Re: -={ Challenge }=- Nested Summing
« Reply #25 on: July 17, 2010, 02:47:23 PM »
Slightly reworked version of D's. Call to acedGetArgs eliminated. pRbTmp != NULL eliminated. RTPOINT and RT3DPOINT reworked. Got rid of unneeded curly brackets in each of the case statements (not needed unless a local variable needs to be assigned defined at case scope).

Code: [Select]
static int ads_nsum(resbuf * pArgs)
{
    double ans = 0;
    for(resbuf *pRbTmp = pArgs; pRbTmp; pRbTmp=pRbTmp->rbnext)
    {
        switch(pRbTmp->restype)
        {
        case RTSHORT: ans+=pRbTmp->resval.rint; break;
        case RTLONG: ans+=pRbTmp->resval.rlong; break;
        case RTREAL: ans+=pRbTmp->resval.rreal; break;
        case RTPOINT: ans += pRbTmp->resval.rpoint[0] +
                          pRbTmp->resval.rpoint[1]; break;
        case RT3DPOINT: ans+=pRbTmp->resval.rpoint[0] +
                            pRbTmp->resval.rpoint[1] +
                            pRbTmp->resval.rpoint[2]; break;
        }
    }
    acedRetReal(ans);
    return (RSRSLT) ;
}
« Last Edit: July 17, 2010, 02:51:31 PM by pkohut »

David Bethel

  • Swamp Rat
  • Posts: 656
Re: -={ Challenge }=- Nested Summing
« Reply #26 on: July 17, 2010, 03:30:18 PM »
Lee,

I always liked this one from the Compuserve days:
-David

Ah, yes. 100 virtual points for anyone that can remember the moderator's name?

Anne Brown


She was into quilting or something.  Not a CAd person at all.

Some of the threadmatsers that I remember were

Jon Fleming
Bill Townsend
Tony Tanzillio ( for a while )
Steve Johhnson
Ditmar Rudolph



R12 Dos - A2K

pkohut

  • Guest
Re: -={ Challenge }=- Nested Summing
« Reply #27 on: July 17, 2010, 03:34:51 PM »

Anne Brown

She was into quilting or something.  Not a CAd person at all.

Yep. You can collect your points at the CompuServe front desk.  :-)

Some of the threadmatsers that I remember were

Jon Fleming
Bill Townsend
Tony Tanzillio ( for a while )
Steve Johhnson
Ditmar Rudolph

Wow, scored bonus on those. I'd forgotten.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8691
  • AKA Daniel
Re: -={ Challenge }=- Nested Summing
« Reply #28 on: July 17, 2010, 07:55:27 PM »
Slightly reworked version of D's. Call to acedGetArgs eliminated. pRbTmp != NULL eliminated. RTPOINT and RT3DPOINT reworked. Got rid of unneeded curly brackets in each of the case statements (not needed unless a local variable needs to be assigned defined at case scope).

Code: [Select]
static int ads_nsum(resbuf * pArgs)
{
    double ans = 0;
    for(resbuf *pRbTmp = pArgs; pRbTmp; pRbTmp=pRbTmp->rbnext)
    {
        switch(pRbTmp->restype)
        {
        case RTSHORT: ans+=pRbTmp->resval.rint; break;
        case RTLONG: ans+=pRbTmp->resval.rlong; break;
        case RTREAL: ans+=pRbTmp->resval.rreal; break;
        case RTPOINT: ans += pRbTmp->resval.rpoint[0] +
                          pRbTmp->resval.rpoint[1]; break;
        case RT3DPOINT: ans+=pRbTmp->resval.rpoint[0] +
                            pRbTmp->resval.rpoint[1] +
                            pRbTmp->resval.rpoint[2]; break;
        }
    }
    acedRetReal(ans);
    return (RSRSLT) ;
}

so your adding another function call for acedGetArgs() somewhere?

pkohut

  • Guest
Re: -={ Challenge }=- Nested Summing
« Reply #29 on: July 17, 2010, 08:06:15 PM »
so your adding another function call for acedGetArgs() somewhere?

No, check the signature of ads_nsum.