Author Topic: (challenge) count items in a list and return  (Read 10146 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) count items in a list and return
« Reply #15 on: September 20, 2004, 10:27:48 AM »
Ok.. how about this then...it is a little slower but it works ....
Code: [Select]

(defun cnt ( lst / count item maxnum )
  (setq maxnum 0)
  (mapcar '(lambda (x)
        (setq count (-(length lst)(length (vl-remove x lst))))
        (cond
 ((and (> count maxnum)(not(member x item)))(setq maxnum count item (list x)))
 ((and (= count maxnum)(not(member x item)))(setq maxnum count item (append (list x) item)))
))
        lst)
  (list maxnum (reverse item))
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) count items in a list and return
« Reply #16 on: September 20, 2004, 10:29:42 AM »
Quote from: David D Bethel
(member) is very good choice for small lists.  It gets real bogged down with large ones.  Same thing with (append).  Use (cons) & (reverse) for multiple modifies.  -David

Sounds like someone wants to race!! :D

anyone have a function that can generate a large list to test these functions against?
TheSwamp.org  (serving the CAD community since 2003)

SMadsen

  • Guest
(challenge) count items in a list and return
« Reply #17 on: September 20, 2004, 10:32:56 AM »
Quote from: Keith
Ok.. how about this then...

Very neat   8)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) count items in a list and return
« Reply #18 on: September 20, 2004, 10:34:36 AM »
man I love this stuff....

Maybe we should have a VBA challenge...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

SMadsen

  • Guest
(challenge) count items in a list and return
« Reply #19 on: September 20, 2004, 10:36:03 AM »
Quote from: Mark Thomas
Sounds like someone wants to race!! :D

Mr. Madsen thinks he'll forefeit. With MEMBER in recursion ... hmmm

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
(challenge) count items in a list and return
« Reply #20 on: September 20, 2004, 10:52:08 AM »
[quote="Mark Thomas]...
anyone have a function that can generate a large list to test these functions against?[/quote] I think i have one, let me look.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) count items in a list and return
« Reply #21 on: September 20, 2004, 10:56:24 AM »
Preliminary results are in! Run on 1000 item list of random integers. Same list for all tests.

In order of results:

David's IREP
(runit 'irep llst)
(3 4) Repeats 116 Times
Result: -
Time: 47 millisecs

MP's TALLYMAX
(runit 'tallymax llst)
Result: ((4 . 116) (3 . 116))
Time: 47 millisecs

Keith's CNT
(runit 'cnt llst)
Result: (116 (4 3))
Time: 1547 millisecs

SMadsen's TALLYHO
(runit 'tallyho llst)
Result: ((116 . 3) (116 . 4))
Time: 3579 millisecs

Mark's ITEM-COUNT
(runit 'item-count llst)
Result: (4 . 116)
Time: 10250 millisecs


RUNIT code:
Code: [Select]
(defun runIt (func arg / time result)
  (gc)
  (setq time (getvar "MILLISECS"))
  (setq result (apply func (list arg)))
  (setq time (- (getvar "MILLISECS") time))
  (gc)
  (mapcar 'princ (list "\nResult: " result "\nTime: " time " millisecs"))
  (princ)
)

SMadsen

  • Guest
(challenge) count items in a list and return
« Reply #22 on: September 20, 2004, 11:03:05 AM »
Code for building 1000 item list of random integers
Code: [Select]
(defun randnum (/ modulus multiplier increment random)
  (if (not seed)(setq seed (getvar "DATE")))
  (setq modulus    65536 multiplier 25173 increment 13849
        seed       (rem (+ (* multiplier seed) increment) modulus)
        random     (/ seed modulus))
)

(defun largelist (/ ret)
  (repeat 1000 (setq ret (cons (fix (* 10 (randnum))) ret))))

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(challenge) count items in a list and return
« Reply #23 on: September 20, 2004, 11:13:59 AM »
Quote from: SMadsen
Preliminary results are in! Run on 1000 item list of random integers. Same list for all tests.
Mark's ITEM-COUNT
(runit 'item-count llst)
Result: (4 . 116)
Time: 10250 millisecs

OUCH!! man that hurts. back to the drawing board
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
(challenge) count items in a list and return
« Reply #24 on: September 20, 2004, 11:14:09 AM »
Glad you had one Stig. Cause i couldnt find one.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
(challenge) count items in a list and return
« Reply #25 on: September 20, 2004, 11:23:54 AM »
I always carry a large list function around in my wallet. Don't you, Se7en??

Mark, I could have sworn mine was slower but .. well .. now I'm happy again *neener*

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(challenge) count items in a list and return
« Reply #26 on: September 20, 2004, 11:36:40 AM »
Ok here is my attempt at something different.

Perhaps the vl-remove is not reliable enough to use but it's the
only thing I could come up with that has not been done.

Code: [Select]
(defun item_cnt (lst / item itm cnt)
  (setq item   '(0 nil))
  (while lst
    (setq itm (car lst)
          cnt (- (length lst)(length (setq lst (vl-remove itm lst)))))
    (cond
      ((> cnt (car item)) (setq item (list cnt itm)))
      ((= cnt (car item)) (setq item (list cnt itm (cadr item))))
    )
  )
  item
)
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.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) count items in a list and return
« Reply #27 on: September 20, 2004, 11:43:43 AM »
Well, I am at least glad I was in the middle of the pack and not at the bottom ...
Incedently I changed a few things in the code and tested it here, I came up with a much better run time of 203ms and 205ms on a random list of 1000 elements.

Code: [Select]

(defun cnt ( lst / count item maxnum )
  (setq maxnum 0)
  (foreach x lst
    (setq count (-(length lst)(length (vl-remove x lst))))
    (cond
      ((< count maxnum) nil)
      ((> count maxnum)(setq maxnum count item (list x)))
      ((not(member x item))(setq item (append (list x) item)))
    )
  )
  (list maxnum (reverse item))
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(challenge) count items in a list and return
« Reply #28 on: September 20, 2004, 11:49:14 AM »
Ooops, didn't look at you code Keith. Guess I didn't even get in the game
this go round. :?
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.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
(challenge) count items in a list and return
« Reply #29 on: September 20, 2004, 11:55:22 AM »
Ok third try .. since we are only testing numbers at this point try this one ...

CAB ... that is amazingly similar :roll:

Code: [Select]

(defun cnt ( lst / count item maxnum )
  (setq maxnum 0)
  (if (numberp (car lst))
    (setq shtlst (vl-sort lst '<))
    (setq shtlst lst)
  )
  (foreach x shtlst
    (setq count (-(length lst)(length (vl-remove x lst))))
    (cond
      ((< count maxnum) nil)
      ((> count maxnum)(setq maxnum count item (list x)))
      ((not(member x item))(setq item (append (list x) item)))
    )
  )
  (list maxnum (reverse item))
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie