News for hackers. Stuff that matters.
Re:Why we don't hire programmers based on puzzles and tricks by zeteo on Friday February 22, 2013 @02:30PM (#5266201)
Come to think of it, swapping two variables in place is a horrible question - because you can't really do it! A statement like

a = a + b

requires the (invisible) presence of a register where a + b (or xor or whatever) is placed right before being written back to a. But then you might as well use this register directly for the swap instead of dabbling around with one-to-one functions.

Re:Why we don't hire programmers based on puzzles and tricks by steveklabnik on Friday February 22, 2013 @02:41PM (#5266255)
Also, it depends on which language you're using...

    $ irb
    irb(main):001:0> a = 1
    => 1
    irb(main):002:0> b = 2
    => 2
    irb(main):003:0> b, a = a, b
    => [1, 2]
    irb(main):004:0> puts "#{a} #{b}"
    2 1
    => nil
(obviously this has the same flaw you mention, the interpreter is doing stuff for you. It follows the letter of the 'no intermediate variables' only.)
Re:Why we don't hire programmers based on puzzles and tricks by michaelfairley on Friday February 22, 2013 @03:03PM (#5266422)
You can also follow the letter in C, Java, etc.:

  a ^= b
  b ^= a
  a ^= b
Re:Why we don't hire programmers based on puzzles and tricks by jervisfm on Friday February 22, 2013 @07:12PM (#5267826)
What does that do?
Re:Why we don't hire programmers based on puzzles and tricks by steveklabnik on Friday February 22, 2013 @07:37PM (#5267901)
Re:Why we don't hire programmers based on puzzles and tricks by jervisfm on Friday February 22, 2013 @11:03PM (#5268498)
Thanks for the pointer.
Re:Why we don't hire programmers based on puzzles and tricks by simcop2387 on Friday February 22, 2013 @06:08PM (#5267534)
Just to add a perl version for the ruby and python ones.

($a, $b) = ($b, $a)

Re:Why we don't hire programmers based on puzzles and tricks by chris_mahan on Friday February 22, 2013 @03:55PM (#5266729)
python: a, b = b, a