Re: Newb ??
On Thu, 10 Nov 2005 17:00:38 +0000, Norman Silverstone
<norman@littlet ank.org> declaimed the following in comp.lang.pytho n:
[color=blue]
> Thanks for that but I think it is too simplistic. It appears OK for the
> first guess, which is 50 but, what about the next guess. If the guess is
> too high then the next guess has to be 50/2. However, if it is too low
> then the next guess must be first guess + (100-second guess)/2. In general
> terms, if guess is too high then next guess must (guess - lowest
> possible)/2 and if too low then it is guess + (highest possible -
> guess)/2.
>[/color]
I'm having trouble following that -- I think you may be complicating
things more than you need to...
The basic algorithm only needs to know the "current" low and high
end of the range.
new_guess = (current_low + current_high) / 2
Match: break out of loop, with a successful guess
Low: current_low = new_guess; repeat #could be new_guess +1
#as you know it can not
#be "new_guess"
High: current_high = new_guess; repeat #or new_guess-1
Say the number is 78
low high guess result
0 100 (0+100)/2 => 50 LOW
50 100 (50+100)/2 => 75 LOW
75 100 (75+100)/2 => 88 HIGH (rounded up)
75 88 (75+88)/2 => 82 HIGH (rounded up)
75 82 (75+82)/2 => 79 HIGH (rounded up)
75 79 (75+79)/2 => 77 LOW
77 79 (77+79)/2 => 78 MATCH
NOTE: if you use the +1/-1 adjustments (to eliminate the guess
itself from being an end-point, as you know it can not be that value)
the above will converge to a match one step sooner.
--[color=blue]
> =============== =============== =============== =============== == <
> wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wulfraed@dm.net | Bestiaria Support Staff <
> =============== =============== =============== =============== == <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]
On Thu, 10 Nov 2005 17:00:38 +0000, Norman Silverstone
<norman@littlet ank.org> declaimed the following in comp.lang.pytho n:
[color=blue]
> Thanks for that but I think it is too simplistic. It appears OK for the
> first guess, which is 50 but, what about the next guess. If the guess is
> too high then the next guess has to be 50/2. However, if it is too low
> then the next guess must be first guess + (100-second guess)/2. In general
> terms, if guess is too high then next guess must (guess - lowest
> possible)/2 and if too low then it is guess + (highest possible -
> guess)/2.
>[/color]
I'm having trouble following that -- I think you may be complicating
things more than you need to...
The basic algorithm only needs to know the "current" low and high
end of the range.
new_guess = (current_low + current_high) / 2
Match: break out of loop, with a successful guess
Low: current_low = new_guess; repeat #could be new_guess +1
#as you know it can not
#be "new_guess"
High: current_high = new_guess; repeat #or new_guess-1
Say the number is 78
low high guess result
0 100 (0+100)/2 => 50 LOW
50 100 (50+100)/2 => 75 LOW
75 100 (75+100)/2 => 88 HIGH (rounded up)
75 88 (75+88)/2 => 82 HIGH (rounded up)
75 82 (75+82)/2 => 79 HIGH (rounded up)
75 79 (75+79)/2 => 77 LOW
77 79 (77+79)/2 => 78 MATCH
NOTE: if you use the +1/-1 adjustments (to eliminate the guess
itself from being an end-point, as you know it can not be that value)
the above will converge to a match one step sooner.
--[color=blue]
> =============== =============== =============== =============== == <
> wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wulfraed@dm.net | Bestiaria Support Staff <
> =============== =============== =============== =============== == <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]
Comment