Re: Swapping values of two variables

2009-01-30 Thread Grant Edwards
On 2009-01-31, Aahz wrote: >>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 It l

Re: Swapping values of two variables

2009-01-30 Thread Aahz
In article , Eric Kang 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

Re: Swapping values of two variables

2009-01-30 Thread Grant Edwards
> Grant Edwards wrote: > > On 2009-01-30, MRAB wrote: > > > >>> What is the minimum amount of extra memory required to exchange two > >>> 32-bit quantities? What would be the pseudocode that achieves this > >>> minimum? > >> x ^= y > >> y ^= x > >> x ^= y > >> > >> This is really only of use when

Re: Swapping values of two variables

2009-01-30 Thread Christian Heimes
Steven D'Aprano schrieb: > Ints in Python are *objects*, not 32-bit quantities. An int is 12 bytes > (96 bits) in size; a long will use as much memory as needed. If your > application needs to optimize a swap of two ints, then Python is probably > going to be much too memory-intensive for you.

Re: Swapping values of two variables

2009-01-30 Thread MRAB
Grant Edwards wrote: > On 2009-01-30, MRAB wrote: > >>> What is the minimum amount of extra memory required to exchange two >>> 32-bit quantities? What would be the pseudocode that achieves this >>> minimum? >> x ^= y >> y ^= x >> x ^= y >> >> This is really only of use when working in assembly l

Re: Swapping values of two variables

2009-01-29 Thread Steven D'Aprano
On Thu, 29 Jan 2009 21:23:48 -0800, Kottiyath wrote: > Is it possible to swap two floats without a variable? In Python? Sure. f = 1.23 g = 2.87 f, g = g, f This idiom is independent of the types of the objects: x = "hello world" y = [1, 2.0, None, "xyz", {}] x, y = y, x In other languages?

Re: Swapping values of two variables

2009-01-29 Thread tony . clarke5
On Jan 30, 3:31 am, Steven D'Aprano wrote: > On Thu, 29 Jan 2009 17:50:04 -0800, tony.clarke5 wrote: > > On Jan 30, 12:29 am, Eric Kang 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 > >> f

Re: Swapping values of two variables

2009-01-29 Thread Grant Edwards
On 2009-01-30, MRAB wrote: >> What is the minimum amount of extra memory required to exchange two >> 32-bit quantities? What would be the pseudocode that achieves this >> minimum? > > x ^= y > y ^= x > x ^= y > > This is really only of use when working in assembly language. And rarely then. ;)

Re: Swapping values of two variables

2009-01-29 Thread Kottiyath
On Jan 30, 8:31 am, Steven D'Aprano wrote: > On Thu, 29 Jan 2009 17:50:04 -0800, tony.clarke5 wrote: > > On Jan 30, 12:29 am, Eric Kang 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 > >> f

Re: Swapping values of two variables

2009-01-29 Thread Steven D'Aprano
On Thu, 29 Jan 2009 17:50:04 -0800, tony.clarke5 wrote: > On Jan 30, 12:29 am, Eric Kang 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 >>

Re: Swapping values of two variables

2009-01-29 Thread Steven D'Aprano
On Thu, 29 Jan 2009 16:29:11 -0800, Eric Kang 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 Yes. > But could the swapping be done using less

Re: Swapping values of two variables

2009-01-29 Thread tony . clarke5
On Jan 30, 12:29 am, Eric Kang 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?

Re: Swapping values of two variables

2009-01-29 Thread MRAB
Eric Kang 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