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

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
-={ Challenge }=- Nested Summing
« on: July 17, 2010, 06:09:26 AM »
Just for fun  :lol:

The Challenge:

Sum a list of numerical elements, nested to any level  8-)

Example:

Code: [Select]
(SumList '( 1 2 3 ))
==> 6

Code: [Select]
(SumList '( 1 2 ( 3 4 ( 1 ))))
==> 11

Have fun!

Lee
« Last Edit: July 17, 2010, 06:55:29 AM by Lee Mac »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: -={ Challenge }=- Nested Summing
« Reply #1 on: July 17, 2010, 07:02:12 AM »
Code: [Select]
static int ads_nsum(void)
  {
    struct resbuf *pArgs=acedGetArgs();
    double ans = 0;
    for(resbuf *pRbTmp=pArgs;pRbTmp!=NULL;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];
                        ans+=pRbTmp->resval.rpoint[1];} break;
      case RT3DPOINT: { ans+=pRbTmp->resval.rpoint[0];
                        ans+=pRbTmp->resval.rpoint[1];
                        ans+=pRbTmp->resval.rpoint[2];} break;
      }
    }
    acedRetReal(ans);
    return (RSRSLT) ;
  }

« Last Edit: July 17, 2010, 09:12:59 AM by eAmbiguousOutput »

David Bethel

  • Swamp Rat
  • Posts: 656
Re: -={ Challenge }=- Nested Summing
« Reply #2 on: July 17, 2010, 07:02:42 AM »
Lee,

I always liked this one from the Compuserve days:

Code: [Select]
;;;++++++++++++ Explode Any List Steve Johnson +++++++++++++++++++++
(defun exlist (LST / expl new)
  (defun expl (SUBLST)
    (foreach one SUBLST
      (if (atom one)
          (setq new (cons one new))
          (if (listp (cdr one))
              (expl one)
              (setq new (cons (cdr one) (cons (car one) new)))))))
  (expl (list LST))
  (reverse new))
Code: [Select]
 (defun SumList (l)
    (apply '+ (exlist l)))

(SumList '(1 2 ( 3 4 ( 1 ))))

-David
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Nested Summing
« Reply #3 on: July 17, 2010, 07:08:08 AM »
Dan - I'm blown out of the water by that  :lol: I can't even begin to dissect it to try to understand it...

David - that reminds me of Michaels 'Squish' function - nice approach.

Here is my submission:

Code: [Select]
(defun LM:SumList ( l )
  (if l
    (+ (if (eq 'list (type (car l)))
         (LM:SumList (car l))
         (car l)
       )
       (LM:SumList (cdr l))
    )
    0
  )
)


David Bethel

  • Swamp Rat
  • Posts: 656
Re: -={ Challenge }=- Nested Summing
« Reply #4 on: July 17, 2010, 07:22:27 AM »
Lee,

Try a list like this:

Code: [Select]
(setq l1 '( 1 2 (3 . 4)(5 6 7) 8))
That could have came from same stuff.  There were quite a few of us on the Compuserve forum that are still hanging around.  It was Autodesk's 'official' forum.  ALT- & COMP- stuff was around, just loaded with flamers and junk...

-David
« Last Edit: July 17, 2010, 07:27:30 AM by David Bethel »
R12 Dos - A2K

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: -={ Challenge }=- Nested Summing
« Reply #5 on: July 17, 2010, 07:23:57 AM »
Hi,

the same as Lee:
Code: [Select]
(defun sumlist (l / e)
  (if (setq e (car l))
    (+ (if (atom e)
         e
         (sumlist e)
       )
       (sumlist (cdr l))
    )
    0
  )
)

or using cond
Code: [Select]
(defun sumlist (l / e)
  (cond
    ((null (setq e (car l))) 0)
    ((atom (car l)) (+ e (sumlist (cdr l))))
    (T (+ (sumlist e) (sumlist (cdr l))))
  )
)
Speaking English as a French Frog

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: -={ Challenge }=- Nested Summing
« Reply #6 on: July 17, 2010, 07:36:20 AM »
This one should work with the list posted by David

Code: [Select]
(defun sumlist (l / e)
  (cond
    ((and l (atom l)) l)
    ((null (setq e (car l))) 0)
    ((atom (car l)) (+ e (sumlist (cdr l))))
    (T (+ (sumlist e) (sumlist (cdr l))))
  )
)
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Nested Summing
« Reply #7 on: July 17, 2010, 07:37:22 AM »
Lee,

Try a list like this:

Code: [Select]
(setq l1 '( 1 2 (3 . 4)(5 6 7) 8))
That could have came from same stuff.  There were quite a few of us on the Compuserve forum that are still hanging around.  It was Autodesk's 'official' forum.  ALT- & COMP- stuff was around, just loaded with flamers and junk...

-David

Ah yes - I hadn't accounted for any dotted pairs...  :|




gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: -={ Challenge }=- Nested Summing
« Reply #8 on: July 17, 2010, 07:42:45 AM »
Or, using if statement:

Code: [Select]
(defun sumlist (l / e)
  (if (setq e (car l))
    (+ (if (atom e)
         e
         (sumlist e)
       )
       (if (listp (cdr l))
         (sumlist (cdr l))
         (cdr l)
       )
    )
    0
  )
)
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Nested Summing
« Reply #9 on: July 17, 2010, 07:43:33 AM »
Along the same lines as Gile I'm afraid  :|

Code: [Select]
(defun LM:SumList ( l )
  (if l
    (if (atom l) l
      (if (atom (car l))
        (+ (car l) (LM:SumList (cdr l)))
        (+ (LM:SumList (car l)) (LM:SumList (cdr l)))
      )
    )
    0
  )
)

Code: [Select]
(defun LM:SumList2 ( l / x )
  (if l
    (if (atom l) l
      (+
        (if (atom (setq x (car l))) x (LM:SumList2 x))
        (LM:SumList2 (cdr l))
      )
    )
    0
  )
)
« Last Edit: July 17, 2010, 08:16:38 AM by Lee Mac »

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: -={ Challenge }=- Nested Summing
« Reply #10 on: July 17, 2010, 07:47:40 AM »
good here   :laugh:

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Nested Summing
« Reply #11 on: July 17, 2010, 07:56:40 AM »
good here   :laugh:

Yours will probably wipe the floor with us speedwise too  8-)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: -={ Challenge }=- Nested Summing
« Reply #12 on: July 17, 2010, 08:22:57 AM »
I tried a C# solution, but it's not very elegant  :|

Code: [Select]
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace ChallengeSumList
{
    public class SumListClass
    {
        [LispFunction("gc-SumList")]
        public double SumList(ResultBuffer resbuf)
        {
            TypedValue[] args = resbuf.AsArray();
            double result = 0;
            foreach (TypedValue tv in args)
            {
                switch (tv.TypeCode)
                {
                    case (short)LispDataType.Int16:
                        result += (short)tv.Value;
                        break;
                    case (short)LispDataType.Int32:
                        result += (int)tv.Value;
                        break;
                    case (short)LispDataType.Double:
                        result += (double)tv.Value;
                        break;
                    case (short)LispDataType.Point2d:
                        result += ((Point2d)tv.Value).X + ((Point2d)tv.Value).Y;
                        break;
                    case (short)LispDataType.Point3d:
                        result += ((Point3d)tv.Value).X + ((Point3d)tv.Value).Y + ((Point3d)tv.Value).Z;
                        break;
                    default:
                        break;
                }
            }
            return result;
        }
    }
}
Speaking English as a French Frog

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: -={ Challenge }=- Nested Summing
« Reply #13 on: July 17, 2010, 08:31:26 AM »
One more for the mix  8-)

Code: [Select]
(defun LM:SumList3 ( l )
  (cond
    ( (not l)  0)
    ( (atom l) l)
    ( (vl-list-length l) (apply '+ (mapcar 'LM:SumList3 l)))
    ( (+ (LM:SumList3 (car l)) (LM:SumList3 (cdr l))))
  )
)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: -={ Challenge }=- Nested Summing
« Reply #14 on: July 17, 2010, 09:13:21 AM »
Another way:

Code: [Select]
(defun sumlist (l)
  (eval
    (read
      (strcat "(+ "
      (vl-string-translate "(.)" "   " (vl-princ-to-string l))
      ")"
      )
    )
  )
)
« Last Edit: July 17, 2010, 01:29:36 PM by gile »
Speaking English as a French Frog