PHP puzzle

So I've decided to make a little PHP puzzle.

The following program contains 3 test cases that you need to make pass. To do that, you should replace XXX in the assignments to $a, $b and $c by the right magic values.

<?php
header('Content-Type: text/plain');

function t_1($x) {
    switch ($x) {
        case 'one': return false;
        case 0:     return !is_string($x);
        default:    return false;
    }
}

function t_2($x) {
    $k = array('one thousand', 2000, 'a363b8d13575101a0226e8d0d054f2e', 'php');
    return in_array(md5($x), $k);
}

function t_3($x) {
    $k = '1020304050607';
    $x = (string)$x;
    return strlen($x) < strlen($k) && $x == $k;
}

echo "1..3\n";

$a = XXX;
echo t_1($a) ? 'ok 1' : 'not ok 1', "\n";

$b = XXX;
echo t_2($b) ? 'ok 2' : 'not ok 2', "\n";

$c = XXX;
echo t_3($c) ? 'ok 3' : 'not ok 3', "\n";

If you've done everything right, the output should look like this:

1..3
ok 1
ok 2
ok 3

(Thanks to http://www.reddit.com/r/lolphp for inspiration.)

2 Comments

The first one is easy. The second is a little more of a challenge. The third, I'm still thinking about.

OK, here are the answers, base64-encoded so as not to spoil the fun. There are at least two correct answers for $a; and theoretically an infinite number of solutions for $b.

function reveal ($answer) {
	return eval("return(".base64_decode($answer).");");
}
 
$a = reveal('bnVsbA==');
$b = reveal('NDM0Mw==');
$c = reveal('JzB4RUQ4RURDNDlBRic=');

Leave a comment

About mauke

user-pic I blog about Perl.