Author Topic: (Challenge) Closest integer to n  (Read 11828 times)

0 Members and 1 Guest are viewing this topic.

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: (Challenge) Closest integer to n
« Reply #45 on: February 03, 2010, 04:06:24 PM »
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
)
Evgeniy, i tested your code and got the same results as Lee did
i just can't see how member can outrun vl-position

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Closest integer to n
« Reply #46 on: February 03, 2010, 04:47:03 PM »
Quote
Evgeniy, i tested your code and got the same results as Lee did
i just can't see how member can outrun vl-position

Indeed, now I got the results that are similar to yours.
Perhaps I was wrong.
I'm sorry, I'm sorry.

I have a new idea :

Code: [Select]
(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
)

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: (Challenge) Closest integer to n
« Reply #47 on: February 03, 2010, 05:04:54 PM »
Evgeniy, excelent move with float :)
now it's faster than mine
but bad news for (test4 1 '(1 2 3))

wizman

  • Bull Frog
  • Posts: 290
Re: (Challenge) Closest integer to n
« Reply #48 on: February 03, 2010, 05:29:57 PM »
Code: [Select]
(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
                  )
        )
    )
)

..be back tom..

cmwade77

  • Swamp Rat
  • Posts: 1443
Re: (Challenge) Closest integer to n
« Reply #49 on: February 03, 2010, 06:38:15 PM »
I am sure that there are far faster and better on here, but I thought I would try my hand at it before reading the entier thread, so here is my entry:
Code: [Select]
(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)
)
It works, but now I am going to go through the thread and see if there is one that looks like it will run faster. This is actually something that can be helpful in some routines that I am working on.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: (Challenge) Closest integer to n
« Reply #50 on: February 03, 2010, 07:19:09 PM »
Chris,

It'd be better if your function took two arguments (the number and lst), and didn't have user input/print out - then it can be tested in a benchmarking function  :-)

SomeCallMeDave

  • Guest
Re: (Challenge) Closest integer to n
« Reply #51 on: February 03, 2010, 08:31:01 PM »
Any room for some Ruby?

Code: [Select]
class Array
  def nearest(input)
    self.sort_by{|x| (x-input).abs}.first
  end
end

numbers = [45, 23, 63, 5, 8, 56, 74, 32, 96, 144, 95]

input = 10

puts numbers.nearest input   # returns 8


puts [1,2,3].nearest 5 #returns 3 


ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Closest integer to n
« Reply #52 on: February 04, 2010, 12:39:45 AM »
Evgeniy, excelent move with float :)
now it's faster than mine
but bad news for (test4 1 '(1 2 3))


Code: [Select]
(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
        )
       )
 )
)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (Challenge) Closest integer to n
« Reply #53 on: February 04, 2010, 03:05:11 AM »

:)


and who said dead threads shouldn't be resurrected  :?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Closest integer to n
« Reply #54 on: February 04, 2010, 03:07:46 AM »

:)


and who said dead threads shouldn't be resurrected  :?

I love all threads *Challenge*  :-)

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: (Challenge) Closest integer to n
« Reply #55 on: February 04, 2010, 04:50:18 AM »
i've changed cond to ifs and stolen wizman's equal
Code: [Select]
(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
    )
  )
)
« Last Edit: February 04, 2010, 04:55:20 AM by VovKa »

wizman

  • Bull Frog
  • Posts: 290
Re: (Challenge) Closest integer to n
« Reply #56 on: February 04, 2010, 04:56:03 AM »
i think that would be it Vovka, i like also EE's float: (vl-position 1.0 '(1 1 1 1.0)) ;3

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Closest integer to n
« Reply #57 on: February 04, 2010, 05:48:42 AM »
ever useful to you:

Code: [Select]
(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)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (Challenge) Closest integer to n
« Reply #58 on: February 04, 2010, 05:53:40 AM »
ever useful to you:

Code: [Select]
(vl-sort '(0 1 2 0 1 2)'<)       ;; =>> '(1 2 3)
< .. >


uhmmm ...  that's (0 1 2) , yes ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (Challenge) Closest integer to n
« Reply #59 on: February 04, 2010, 06:25:45 AM »
uhmmm ...  that's (0 1 2) , yes ?

I was not sufficiently attentive.
Thank you!