Re: select

new topic     » goto parent     » topic index » view thread      » older message » newer message

------=_NextPart_000_003E_01C014DE.28561800
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Here is a generic solution when you have several possible numbers and =
want the closest--since a binary search is used, this will work for =
thousands of numbers.

--Tested code ahead

global function select(atom x,sequence choices)
-- assumes choices is sorted and has at least two elements,=20
-- and all elements are atoms
--uses binary search
integer len,hi,lo,mid,c
len=3Dlength(choices)
hi=3Dlen
lo=3D1=20
while hi>=3Dlo do
   mid=3Dfloor((hi+lo)/2)
   c=3Dchoices[mid]
   if x=3Dc then
      return x
   elsif x>c then
      lo=3Dmid+1
   else
      hi=3Dmid-1
   end if
end while
if lo=3D1 then return choices[1] end if
if hi=3Dlen then return choices[len] end if
if x-choices[hi]<choices[lo]-x then
   return choices[hi]
else=20
   return choices[lo]
end if
end function

-- Mike Nelson
  ----- Original Message -----=20
  From: =0E=20
  To: EUPHORIA at LISTSERV.MUOHIO.EDU=20
  Sent: Friday, September 01, 2000 9:06 PM
  Subject: select


  Hi coders and friends.
  I have a little problem . I want to select the number closest to the =
number that is inputed by the user . Example : I have two numbers , 10 =
and 30 . the user inputs the number 25 , so the program MUST select the =
number closest , and that number is 30 . How can I do this ? Thanks . O, =
and by the way , I just made my first REAL program , a awsome sprite =
editor . And you can save the pic you made to files in sequences e.g. =
{1,12,66,8,29 ....... . And it took me 30 min flat ! Its realy easy now =
.=20

  Ferdinand

------=_NextPart_000_003E_01C014DE.28561800
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2919.6307" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>Here is a generic solution when you have several =
possible=20
numbers and want the closest--since a binary search is used, this will =
work for=20
thousands of numbers.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>--Tested code ahead</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>global function select(atom x,sequence =
choices)<BR>-- assumes=20
choices is sorted and has at least two elements, <BR>-- and all elements =
are=20
atoms<BR>--uses binary search<BR>integer=20
len,hi,lo,mid,c<BR>len=3Dlength(choices)<BR>hi=3Dlen<BR>lo=3D1 <BR>while =
hi&gt;=3Dlo=20
do<BR>&nbsp;&nbsp; mid=3Dfloor((hi+lo)/2)<BR>&nbsp;&nbsp;=20
c=3Dchoices[mid]<BR>&nbsp;&nbsp; if x=3Dc =
then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
return x<BR>&nbsp;&nbsp; elsif x&gt;c =
then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
lo=3Dmid+1<BR>&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
hi=3Dmid-1<BR>&nbsp;&nbsp; end if<BR>end while<BR>if lo=3D1 then return =
choices[1]=20
end if<BR>if hi=3Dlen then return choices[len] end if<BR>if=20
x-choices[hi]&lt;choices[lo]-x then<BR>&nbsp;&nbsp; return =
choices[hi]<BR>else=20
<BR>&nbsp;&nbsp; return choices[lo]<BR>end if<BR>end =
function</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>-- Mike Nelson</FONT></DIV>
<BLOCKQUOTE=20
style=3D"BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: =
0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
  <A href=3D"mailto:mwfch at MWEB.CO.ZA" title=3Dmwfch at MWEB.CO.ZA>=0E</A> =
</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A=20
  href=3D"mailto:EUPHORIA at LISTSERV.MUOHIO.EDU"=20
  title=3DEUPHORIA at LISTSERV.MUOHIO.EDU>EUPHORIA at LISTSERV.MUOHIO.EDU</A> =
</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Friday, September 01, =
2000 9:06=20
  PM</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> select</DIV>
  <DIV><BR></DIV>
  <DIV><FONT face=3DArial>Hi coders and friends.</FONT></DIV>
  <DIV>I have a little problem . I want to select the number closest to =
the=20
  number that is inputed by the user . Example : I have two numbers , 10 =
and 30=20
  . the user inputs the number 25 , so the program MUST select the =
number=20
  closest , and that number is 30 . How can I do this ? Thanks . O, and =
by the=20
  way , I just made my first REAL program , a awsome sprite editor . And =
you can=20
  save the pic you made to files in sequences e.g. {1,12,66,8,29 ....... =
. And=20
  it took me 30 min flat ! Its realy easy now . </DIV>
  <DIV>&nbsp;</DIV>

------=_NextPart_000_003E_01C014DE.28561800--

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu