Swapping values of two variables

Aahz aahz at pythoncraft.com
Fri Jan 30 19:11:42 EST 2009


In article <mailman.8334.1233277403.3487.python-list at python.org>,
Eric Kang  <ysk1 at sfu.ca> wrote:
>
>In python, I set:
>
>x=1
>y=3
>
>z = x
>x = y
>y = z
>
>
>This gave me 3 1, which are the values of x and y swapped.
>The following would have given me the same result:
>x, y = y, x
>
>
>
>But could the swapping be done using less extra memory than this? What
>is the minimum amount of extra memory required to exchange two 32-bit
>quantities? What would be the pseudocode that achieves this minimum?

This looks like a homework problem to me....
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote 
programs, then the first woodpecker that came along would destroy civilization.



More information about the Python-list mailing list