Re: [PHP] PHP Brainteasers 2011

2011-05-23 Thread Daniel Brown
On Fri, May 20, 2011 at 12:55, Marc Guay m...@jkcommunications.com wrote:
 I imagine this one's been done before, but maybe not in the same way

I believe it was, but not quite the same, you're right.

If at first you don't succeed, try, try again.

Nice one, Marc.



 ?php

        $result = succeed();

        while (!$result){
                try{
                        $result = succeed();

                }
                catch (Exception $e){
                        echo $e;
                }
        }
        echo hell yeah;

        function succeed(){
                $a = rand(1,2);

                switch($a){
                        case 1:
                                trigger_error('fml');
                                break;
                        case 2:
                                return TRUE;
                                break;
                }
        }
 ?

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php





-- 
/Daniel P. Brown
Network Infrastructure Manager
http://www.php.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP Brainteasers 2011

2011-05-23 Thread tedd

At 11:29 AM -0400 5/23/11, Daniel Brown wrote:

On Fri, May 20, 2011 at 12:55, Marc Guay m...@jkcommunications.com wrote:

 I imagine this one's been done before, but maybe not in the same way


I believe it was, but not quite the same, you're right.

If at first you don't succeed, try, try again.


If at first you don't succeed, eat a donut -- the urge will pass.

Cheers,

tedd

--
---
http://sperling.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] PHP Brainteasers 2011

2011-05-20 Thread Marc Guay
I imagine this one's been done before, but maybe not in the same way


?php

$result = succeed();

while (!$result){
try{
$result = succeed();

}
catch (Exception $e){
echo $e;
}
}
echo hell yeah;

function succeed(){
$a = rand(1,2);

switch($a){
case 1:
trigger_error('fml');
break;
case 2:
return TRUE;
break;
}   
}
?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] PHP Brainteasers 2011

2011-05-20 Thread Steve Staples
Just wondering if anyone has done anything for this?   I personally
haven't had any ideas come to mind yet... 

Looking forward to seeing them!!!  (once they come)


Steve


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] PHP Brainteasers 2011

2011-05-20 Thread Alex Nikitin
what's the scope?

I have some brain-teasing functions i've been working on, but they are far
from bulletproof, but here is an example

function float_int($significand) {
$sign = ($significand0) ? true  : false;
$significand = abs($significand);
$drep = (decbin( (int) $significand));
$frep = ;

for($i = 0; $i = 22; $i++) {
$significand = ($significand - (int) $significand)*2;
if($significand == 0) break;
$frep .= ($significand = 1) ? 1 : 0;
}

$significand = preg_replace(/^0*1/, ,
$drep.$frep);

$significand = (strlen($significand)  23) ? str_pad($significand, 23,
0) : substr($significand, 0,
23);


if($drep) {
$exponent = decbin(126+strlen($drep));
} else {
$tmp = preg_split('/1/', $drep.$frep, 2, PREG_SPLIT_OFFSET_CAPTURE);
$exponent = decbin((-$tmp[1][1])+127);
}
$f = bindec($exponent.$significand);
return (!$sign) ? $f : $f^0x;
}

I guess the function name and variable names will offer some assistance in
determining what this does, but i think its an interesting brain-bender, not
very useful, because its pretty slow, but still :)

-- Alex --

--
The trouble with programmers is that you can never tell what a programmer is
doing until it’s too late.  ~Seymour Cray



On Fri, May 20, 2011 at 12:49 PM, Steve Staples sstap...@mnsi.net wrote:

 Just wondering if anyone has done anything for this?   I personally
 haven't had any ideas come to mind yet...

 Looking forward to seeing them!!!  (once they come)


 Steve


 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php