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

0 Members and 1 Guest are viewing this topic.

#### Mark

• Custom Title
• Seagull
• Posts: 28708
##### (Challenge) Closest integer to n
« on: March 11, 2005, 08:35:01 AM »
Write a function that returns the closest integer to n given a list of integers.

Example:
Code: [Select]
`(setq lst '(45 23 63 5 8 56 74 32 96 144 95))`
return the number closest to 10;

Have fun.
TheSwamp.org  (serving the CAD community since 2003)

#### MP

• Seagull
• Posts: 17750
• Have thousands of dwgs to process? Contact me.
##### (Challenge) Closest integer to n
« Reply #1 on: March 11, 2005, 09:14:23 AM »
Oops.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client

#### whdjr

• Guest
##### (Challenge) Closest integer to n
« Reply #2 on: March 11, 2005, 10:13:25 AM »
Rules?

#### Mark

• Custom Title
• Seagull
• Posts: 28708
##### (Challenge) Closest integer to n
« Reply #3 on: March 11, 2005, 10:27:41 AM »
Quote from: whdjr
Rules?

Nope!! Go for it, let's see what ya got.

commence posting, post at will..............
TheSwamp.org  (serving the CAD community since 2003)

#### whdjr

• Guest
##### (Challenge) Closest integer to n
« Reply #4 on: March 11, 2005, 10:38:06 AM »
Quote from: Mark Thomas
commence posting, post at will..............

NO! NO! NOT AT ME!

Seriously.

Here's mine:
Code: [Select]
`(setq lst '(45 23 63 5 8 56 74 32 96 144 95))(defun closest (nlst / int pos)  (initget (+ 1 4))  (setq int (getint "\nEnter a number: "))  (setq pos (vl-position      0      (vl-sort-i (mapcar '(lambda (n) (abs (- n int))) nlst) '>)    )  )  (princ (strcat "The number closest to " (itoa int) " is " (itoa (nth pos nlst)) "." )  ))`

#### whdjr

• Guest
##### (Challenge) Closest integer to n
« Reply #5 on: March 11, 2005, 11:50:29 AM »
Here's one that works with negatives as well:
Code: [Select]
`(setq lst2 '(-45 -23 -63 5 -8 56 74 32 -96 -144 95))(defun closest2 (nlst / int pos)  (initget 1)  (setq int (getint "\nEnter a number: "))  (setq pos (vl-position      (apply 'min     (setq pos (mapcar 'abs (mapcar '(lambda (n) (- n int)) nlst)))      )      pos    )  )  (princ (strcat "The number closest to " (itoa int) " is " (itoa (nth pos nlst)) "." )  ))`

#### CAB

• Global Moderator
• Seagull
• Posts: 10395
##### (Challenge) Closest integer to n
« Reply #6 on: March 11, 2005, 12:43:20 PM »
I came up with two versions.
Code: [Select]
`;;  version one(defun nearest (num lst / a)  (foreach x lst    (if (or (null a)            (< (abs(- num x)) (car a))        )      (setq a (list (abs(- num x)) x))    )  )  (cadr a))`

and after some thought version 2
Code: [Select]
`(defun nearest2 (num lst / a)  (car (vl-sort lst '(lambda (x y) (< (abs(- num x)) (abs(- num y)) )))))`
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970

#### Kerry

• Mesozoic relic
• Seagull
• Posts: 11654
• class keyThumper<T>:ILazy<T>
##### Re: (Challenge) Closest integer to n
« Reply #7 on: February 02, 2010, 02:59:04 AM »
< ... >
and after some thought version 2
Code: [Select]
`(defun nearest2 (num lst / a)  (car (vl-sort lst '(lambda (x y) (< (abs(- num x)) (abs(- num y)) )))))`

That is SO pretty !!
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

#### ElpanovEvgeniy

• Water Moccasin
• Posts: 1544
• Moscow (Russia)
##### Re: (Challenge) Closest integer to n
« Reply #8 on: February 02, 2010, 03:46:55 AM »
so there is less computation
Code: [Select]
`(defun test (n l) (nth (car (vl-sort-i (mapcar '(lambda (a) (abs (- n a))) l) '<)) l))`
Stay home. Stay safe. Save lives.

#### Joe Burke

• Guest
##### Re: (Challenge) Closest integer to n
« Reply #9 on: February 02, 2010, 06:17:21 AM »
Seems to me this could return zero or two. IOW, both answers are correct within context.

Command: (nearest2 1 '(0 2 3))
0

#### CAB

• Global Moderator
• Seagull
• Posts: 10395
##### Re: (Challenge) Closest integer to n
« Reply #10 on: February 02, 2010, 08:08:09 AM »
Thanks Kerry.

As always very nice Evgeniy.

That appears to be correct Joe.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970

#### Joe Burke

• Guest
##### Re: (Challenge) Closest integer to n
« Reply #11 on: February 02, 2010, 10:28:31 AM »
I would probably use a genric function like this.

(defun round (value to)
(if (zerop to) value
(* (atoi (rtos (/ (float value) to) 2 0)) to)))

It works works with integer or real values passed. Plus it's numerically correct rounding. Not a qurirk of code rounding.

#### Lee Mac

• Seagull
• Posts: 12499
• London, England
##### Re: (Challenge) Closest integer to n
« Reply #12 on: February 02, 2010, 10:46:21 AM »
I think Evgeniy has put this one to bed

#### VovKa

• Swamp Rat
• Posts: 1352
• Ukraine
##### Re: (Challenge) Closest integer to n
« Reply #13 on: February 02, 2010, 11:27:22 AM »
ugly but should be the fastest in most cases
Code: [Select]
`(defun nearest133 (num lst / a b c)  (setq b (car lst) a (abs (- num b))  )  (while (setq lst (cdr lst))    (if (< (setq c (abs (- num (car lst)))) a)      (setq b (car lst)     a c      )    )  )  b)`

#### Lee Mac

• Seagull
• Posts: 12499
• London, England
##### Re: (Challenge) Closest integer to n
« Reply #14 on: February 02, 2010, 01:35:40 PM »
Late entry, really just a modification of Will's

Code: [Select]
`(defun Closest-LEEMAC (num lst / vlst)  (nth    (vl-position      (apply (function min)        (setq vlst          (mapcar            (function              (lambda (x) (abs (- x num)))) lst))) vlst) lst))`