Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

(dkubb) Re: (1) bushy recursion a good thing ?

by dkubb (Deacon)
on Sep 15, 2001 at 02:53 UTC ( #112577=note: print w/replies, xml ) Need Help??


in reply to bushy recursion a good thing ?

The following does what you want without recursion:

#!/usr/bin/perl -w use strict; use constant MIN => 0; use constant MAX => 40000; my($low, $high, $mid) = (MIN, MAX); print "Think of a number between $low and $high\n"; while(1) { $mid = int(($low + $high) / 2); last if $low >= $high; print "How about $mid? Is it (l)ow, (h)igh or (c)orrect? : "; my $answer = lc(substr <STDIN>, 0, 1); $answer eq 'c' ? last : $answer eq 'l' ? $low = $mid + 1 : $answer eq 'h' ? $high = $mid - 1 : print "bad input\n"; } print "The number you are thinking of is $mid!\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://112577]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2022-12-07 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?