• Wir genießen das Vertrauen von:
EntwicklungSpaß

Second PHP Challenge

By Juli 15, 2013 3 Kommentare

As the challenge we’ve given you last week was a real hit, we want to provoke you again!

So… you are given 2 integral numbers: a and b.

Request: Write a code sequence that interchanges the values of  these 2, without using additional variables.

Join the discussion 3 Kommentare

  • Liviu sagt:

    $a= $b-$a;
    $b=$a-$b;
    $a=$a+$b;

  • unu sagt:

    $a ^= $b ^= $a ^= $b;

  • andreea.paraschiv sagt:

    Well done, guys!

    Our solutions were:
    1.
    $a = $a + $b
    $b = $a – $b
    $a = $a – $b

    2.
    $a = $a * $b;
    $b = $a / $b;
    $a = $a / $b;

    3.
    $a = $a ^ $b;
    $b = $a ^ $b;
    $a = $a ^ $b;

    4.
    list($a, $b) = array($b, $a);
    OR
    list($a,$b) = array_reverse(array($a,$b));

    5.
    extract(array(‚a’=>$b, ‚b’=>$a));

    6.
    file_put_contents(‚a.txt‘,$a);
    file_put_contents(‚b.txt‘,$b);

    $a = file_get_contents(‚b.txt‘);
    $b = file_get_contents(‚a.txt‘);

    7.
    $a = $a <> strlen(decbin($b));
    $a = $b << (strlen(decbin($a)) – strlen(decbin($b))) ^ $a;

    So.. when it comes to web development, we do get creative!

Kommentar hinterlassen