View Single Post
Old 01-04-2008, 04:13 PM   #1 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Big Grin The most inventive way to switch 2 values

OK! A little game for you all

The only objective is to switch the values of $a and $b. The values of $a and $b are 1 and 2, respectively:

php Code:
$a = 1;
$b = 2;

So for example this is an easy way to do it:

php Code:
$c = $a;
$a = $b;
$b = $c;

However! We're not looking for the simplest way to do it, or the fastest, but rather, the most inventive way to do it! There are a couple of rules and those are:
  1. eval and create_function functions are disallowed.
  2. 8 lines maximum but lines with only { and } on do not count towards that total. This does not include the 2 initial assignments of $a and $b, or any echoes of the values $a or $b.
  3. You must have only one semi-colon per line (;) unless of course it's used elsewhere apart from indicating the end of a line. Thus you're not allowed to fuse 2 lines together into 1. The exception is a for loop to where you're allowed 2.

So basically $a must equal 2 and $b must equal 1 at the end of your code.

I'll start us off:

php Code:
list($a, $b) = str_split(strrev($a . $b), 1);

var_dump($a);
var_dump($b);

Put that mind to work! Oh, and just for Karl, you can't just set $a to 2 and $b to 1 You have to reference the first 2 variables set!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote