SPOILER Re: SPUG: scalar swap challenge
Lee Wilson
devnull at devnullsoftware.com
Wed Oct 8 16:47:38 CDT 2003
Sheesh, I sent this earlier, but I think I sent it to an individual rather
than the list. Doh =)
One of my favorite interview questions is to ask what this code does.
Assume A and B are of int type.
B=(A/2+A/2+(A%2==1?1:0))+(A=B)-B;
This line can reduce to:
B = A + (A=B) - B;
Answer: it swaps A and B without using a temp var, and in one line of
code. I think this second line would work in perl for any scalar type,
actually (assuming you put $A and $B =) ).
On Wed, 8 Oct 2003, Tom Legrady wrote:
> # Your $a/$b swap code goes here.
> $a ^= $b;
> $b ^= $a;
> $a ^= $b;
==============================================================================
Lee Wilson - INTP http://www.devnullsoftware.com
Software Developer / RealNetworks http://www.realarcade.com
Home:425-895-9868 Cell:425-890-5463 Office: 206-892-6410
==============================================================================
There are 10 kinds of people in the world:
the people who understand ternary,
the people who dont, but care,
and the people who don't understand or care.
==============================================================================
More information about the spug-list
mailing list