Author Topic: Group list of numbers by fuzz value  (Read 1794 times)

0 Members and 1 Guest are viewing this topic.

dgpuertas

  • Newt
  • Posts: 87
Re: Group list of numbers by fuzz value
« Reply #15 on: July 08, 2024, 12:55:27 PM »

more easy, preorder

Code: [Select]
(defun groupequalfuzzDistance ( lst fuz / rtn x mx)

    (setq lst (vl-sort lst (function <)))

    (while lst
        (setq x (list (car lst))
      mx (+ (car x) fuz)
      lst (vl-remove-if (function (lambda (y) (if (<= y mx) (setq x (cons y x) mx (+ y fuz))))) (cdr lst))
              rtn (cons (reverse x) rtn)
             
        )
    )
(reverse rtn)
)

Lee Mac

  • Seagull
  • Posts: 12939
  • London, England
Re: Group list of numbers by fuzz value
« Reply #16 on: July 08, 2024, 01:23:47 PM »
For a sorted list, a nested while loop is likely to be more performant than vl-remove-if, as the nested loop can cease when an item outside of the given tolerance is reached -
Code - Auto/Visual Lisp: [Select]
  1. (defun groupdoublesbyfuzz ( l f / g r x y )
  2.     (setq l (vl-sort l '<)) ;; Assumes doubles
  3.     (while (setq x (car l))
  4.         (setq g (list x)
  5.               l (cdr l)
  6.         )
  7.         (while (equal (setq y (car l)) x f)
  8.             (setq g (cons y g)
  9.                   l (cdr l)
  10.                   x y
  11.             )
  12.         )
  13.         (setq r (cons (reverse g) r))
  14.     )
  15.     (reverse r)
  16. )

ribarm

  • Gator
  • Posts: 3327
  • Marko Ribar, architect
Re: Group list of numbers by fuzz value
« Reply #17 on: July 08, 2024, 01:51:02 PM »
Based on @dgpuertas request...

Code - Auto/Visual Lisp: [Select]
  1. (defun groupnumsbyfuzz ( lst fuzz / _vl-sort-i x y r )
  2.  
  3.   (defun _vl-sort-i ( lst fun )
  4.     (mapcar (function (lambda ( x ) (nth x lst))) (vl-sort-i lst fun))
  5.   )
  6.  
  7.   (setq lst (_vl-sort-i lst (function <)))
  8.   (while (setq x (car lst))
  9.     (if (and (cadr lst) (<= (abs (- x (if y (last y) (cadr lst)))) fuzz))
  10.       (setq y (cons x y))
  11.       (if (and y (> (abs (- x (last y))) fuzz))
  12.         (progn
  13.           (setq r (cons (reverse y) r) y nil)
  14.           (if (and (cadr lst) (> (abs (- x (cadr lst))) fuzz))
  15.             (setq r (cons (list x) r))
  16.             (setq y (cons x y))
  17.           )
  18.         )
  19.         (setq r (cons (list x) r))
  20.       )
  21.     )
  22.     (setq lst (cdr lst))
  23.   )
  24.   (reverse r)
  25. )
  26.  
  27. (setq *lst* (list 360.243 378.578 388.578 479.098 488.516 489.098 500 506.926 507 511.829 519.16 520.502 520.917 521.829 522.216 616.657 620.048 624.996 626.657 629.326 720.487 899.991 899.994 899.996 900.002 900.005 900.005 900.005 999.999 1000.0 1000.0 1000.0 1000.01 1000.01 1000.01 2922.88 3109.41 3379.1 3400 3517.33 3521.58 3900 4000.0 4000 4000.0 4000.0 4000.0 5108.05 5183.91 5252.41 5258.41 5273.26 5273.32 5273.72 5294.62 5434.47 5438.9 5438.94 5444.94 5459.34 5459.84 5477.59 5609.36 5609.42 5630.26 5755.84 5811.98 5900.78 5919.05 5998.51 6157.25))
  28.  
  29. (groupnumsbyfuzz *lst* 1.5)
  30. ((360.243) (378.578) (388.578) (479.098) (488.516 489.098) (500) (506.926 507) (511.829) (519.16 520.502) (520.917 521.829 522.216) (616.657) (620.048) (624.996) (626.657) (629.326) (720.487) (899.991 899.994 899.996 900.002 900.005 900.005 900.005) (999.999 1000.0 1000.0 1000.0 1000.01 1000.01 1000.01) (2922.88) (3109.41) (3379.1) (3400) (3517.33) (3521.58) (3900) (4000.0 4000.0 4000.0 4000 4000.0) (5108.05) (5183.91) (5252.41) (5258.41) (5273.26 5273.32 5273.72) (5294.62) (5434.47) (5438.9 5438.94) (5444.94) (5459.34 5459.84) (5477.59) (5609.36 5609.42) (5630.26) (5755.84) (5811.98) (5900.78) (5919.05) (5998.51) (6157.25))
  31.  
« Last Edit: July 08, 2024, 01:55:23 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube