Swapping values of two variables
tony.clarke5 at googlemail.com
tony.clarke5 at googlemail.com
Fri Jan 30 02:50:04 CET 2009
On Jan 30, 12:29 am, Eric Kang <y... 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?
How about:
def transpose(x, y):
print x, y, 'becomes: ',
x = x + y
y = x - y
x = x - y
print x, ' ', y
transpose(1,3)
transpose (9,8)
More information about the Python-list
mailing list