Author Topic: In order : few of each atomo in a list...quickly ?  (Read 2563 times)

0 Members and 1 Guest are viewing this topic.

SOFITO_SOFT

  • Guest
In order : few of each atomo in a list...quickly ?
« on: March 24, 2011, 09:19:23 AM »
Hello all:
An aid to optimize speed ?
I have a function to count how many atoms are equal in a list and sort by amount  .... but it is slow ...
If anyone knows what speed it up and share, be grateful.
Code: [Select]
(defun cuantos-de-cada-en-lista (l / ll lll cont n) ; few-to-each-in-list 
  (foreach n l
    (if (not (member n ll ) )
      (setq ll ( cons n ll  ) )
    )
  ) 
  (foreach n ll
      (setq cont 0)
      ( foreach nn l
         ( if ( eq nn n )
           ( setq cont (1+ cont ) )
         )
      )
      ( setq lll ( cons cont lll ) )
  )
  (list ( reverse ll ) lll )
)

(defun orden-cuantos-de-cada-en-lista (l / ll lll  cuales cual cuan maxi posi maxi-cual )  ;order-few-of-each-in-list
( setq cuales ( cuantos-de-cada-en-lista l ) )
( setq cual ( car cuales ) )
( setq cuan ( cadr cuales ) )
( while cual
  ( setq maxi ( apply 'max cuan ) )
  ( setq posi ( vl-position maxi cuan ) )
  ( setq maxi-cual ( nth posi cual ) )   
  ( setq ll ( cons  maxi-cual ll ) )
  ( setq lll ( cons  maxi  lll ) )
  ( setq cual ( vl-remove maxi-cual cual ) )
  ( setq cuan ( remove-ele maxi cuan ) )
)
( list
  ( reverse ll )
  ( reverse lll )
)
)
;;;; FROM GILE....THANKS...
(defun remove-ele (ele lst)
  (if (equal ele (car lst))
    (cdr lst)
    (cons (car lst) (remove-ele ele (cdr lst)))
  )
)
;(orden-cuantos-de-cada-en-lista '( () () 1 NIL NIL NIL 1 1 2 3 4 4 4 4 4 4 ))
;---> ((4 nil 1 2 3) (6 5 3 1 1))
Greetings... :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: In order : few of each atomo in a list...quickly ?
« Reply #1 on: March 24, 2011, 09:58:30 AM »
Using code I already had from here, this may provide some inspiration:

Code: [Select]
(defun LM:ListOccurrences ( l )
  (if l
    (cons
      (cons (car l) (- (length l) (length (setq l (vl-remove (car l) (cdr l))))))
      (LM:ListOccurrences l)
    )
  )
)

(defun test ( l )
  (vl-sort (LM:ListOccurrences l) '(lambda ( a b ) (> (cdr a) (cdr b))))
)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: In order : few of each atomo in a list...quickly ?
« Reply #2 on: March 24, 2011, 10:14:08 AM »
In the format you wanted, things are slightly more difficult  :|

Code: [Select]
(defun LM:ListOccurrences ( a b c )
  (if c
    (LM:ListOccurrences (cons (car c) a) (cons (- (length c) (length (setq c (vl-remove (car c) (cdr c))))) b) c)
    (list a b)
  )
)

(defun test ( l / x )
  ( (lambda ( i ) (mapcar '(lambda ( a ) (mapcar '(lambda ( b ) (nth b a)) i)) x)) 
    (vl-sort-i (cadr (setq x (LM:ListOccurrences nil nil l))) '>)
  )
)

SOFITO_SOFT

  • Guest
Re: In order : few of each atomo in a list...quickly ?
« Reply #3 on: March 24, 2011, 11:21:27 AM »
Hello all:
Lee : the format of  returned "dotted list" is also a good option...
I'm going to change my program for it. Is for about 1000 items with 30 or 40 values differents
Very fast response and functions....Thanks again.... :-)
Greetings

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: In order : few of each atomo in a list...quickly ?
« Reply #4 on: March 24, 2011, 11:33:15 AM »
You're welcome Sofito_Soft  :-)