0 Members and 2 Guests are viewing this topic.
Ok Code: [Select](defun test3 (n l / a b) (if (vl-position n l) n (cond ((not (setq l (vl-sort (cons n l) (function <)) a (cadr (member n l)) ) ;_ setq ) ;_ not (cadr (member n (reverse l))) ) ((not (setq b (cadr (member n (reverse l)))))a) ((if(< (abs (- n a)) (abs (- n b))) a b)) ) ;_ cond ) ;_ if)
(defun test3 (n l / a b) (if (vl-position n l) n (cond ((not (setq l (vl-sort (cons n l) (function <)) a (cadr (member n l)) ) ;_ setq ) ;_ not (cadr (member n (reverse l))) ) ((not (setq b (cadr (member n (reverse l)))))a) ((if(< (abs (- n a)) (abs (- n b))) a b)) ) ;_ cond ) ;_ if)
Evgeniy, i tested your code and got the same results as Lee didi just can't see how member can outrun vl-position
(defun test4 (n l / a b i) (cond ((not (setq l (vl-sort (cons (float n) l) (function <)) i (vl-position (float n) l) a (nth (1- i) l) ) ;_ setq ) ;_ not (nth (1+ i) l) ) ((not (setq b (nth (1+ i) l))) a) ((if (< (abs (- n a)) (abs (- n b))) a b ) ;_ if ) ) ;_ cond)
(defun wiz-near4 (n lst / a d e f g) (if (vl-position n lst) n (setq lst (vl-sort (cons n lst) (function >)) lst (cons -1e6 (reverse (cons 1e6 lst))) a (vl-position n lst) d (nth (1+ a) lst) e (nth (1- a) lst) f (- d n) g (if (equal e n f) e d ) ) ))
(defun c:inttst (/ a lst ct b c d e) (setq a (getint "\nEnter integer: ") lst '(45 23 63 5 8 56 74 32 96 144 95) ct 0 ct2 (- (length lst) 1) lst (vl-sort lst '<) pos (vl-position a lst) b (nth 0 lst) d (- b a) c (nth ct2 lst) e (- a c)) (cond ((/= pos nil) (princ (strcat "\nThere was an exact match: " (itoa (nth pos lst)))) ) (T (while (< ct (length lst)) (cond ((> (nth ct lst) a) (setq b (nth ct lst) d (- b a)) ) ) (setq ct (+ ct 1)) ) (while (> ct2 -1) (cond ((< (nth ct2 lst) a) (setq c (nth ct2 lst) e (- a c)) ) ) (setq ct2 (- ct2 1)) ) (if (< d e) (princ (strcat "\nThe closest number to " (itoa a) " is: " (itoa (nth (- ct 1) lst)))) (princ (strcat "\nThe closest number to " (itoa a) " is: " (itoa (nth (+ ct2 1) lst)))) ) ) ) (princ))
class Array def nearest(input) self.sort_by{|x| (x-input).abs}.first endendnumbers = [45, 23, 63, 5, 8, 56, 74, 32, 96, 144, 95]input = 10puts numbers.nearest input # returns 8puts [1,2,3].nearest 5 #returns 3
Evgeniy, excelent move with float now it's faster than minebut bad news for (test4 1 '(1 2 3))
(defun test4 (n l / a b i) (cond ((zerop (setq l (vl-sort (cons (float n) l) (function <)) i (vl-position (float n) l) ) ) (cadr l) ) ((not (setq a (nth (1- i) l) b (nth (1+ i) l) ) ) a ) ((if (< (abs (- n a)) (abs (- n b))) a b ) ) ))
and who said dead threads shouldn't be resurrected
(defun test413 (n l / a b i) (if (zerop (setq l (vl-sort (cons (float n) l) (function <)) i (vl-position (float n) l) ) ) (cadr l) (if (or (not (setq a (nth (1- i) l) b (nth (1+ i) l) ) ) (equal n a (- b n)) ) a b ) ))
(vl-sort '(0 1 2 0 1 2)'<) ;; =>> '(1 2 3)but(vl-sort '(0. 1. 2. 0. 1. 2.)'<) ;; =>> '(0.0 1.0 1.0 2.0 2.0)
ever useful to you:Code: [Select](vl-sort '(0 1 2 0 1 2)'<) ;; =>> '(1 2 3)< .. >
(vl-sort '(0 1 2 0 1 2)'<) ;; =>> '(1 2 3)< .. >
uhmmm ... that's (0 1 2) , yes ?