-2
\$\begingroup\$

There are two objects:

Object1, Object2

You can easily swap them by:

Object3 = Object1
Object1 = Object2
Object2 = Object3

However, you must solve this problem by using no temporary object and Object1 should be changed to Object 2. (and vice versa)

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Here we have a prime example of what is wrong with challenges that don't have "an objective winning criteria" as currently required by the FAQ: three different answer that are all completely correct. So how will people judge them? All that we have here is a popularity context. \$\endgroup\$ Nov 11 '12 at 20:31
  • 2
    \$\begingroup\$ @dmckee, the problem here isn't lack of objective winning criteria, but of any winning criteria. \$\endgroup\$
    – ugoren
    Nov 11 '12 at 21:18
  • \$\begingroup\$ Would this count in Python? a,b=b,a \$\endgroup\$
    – mbomb007
    Nov 26 '18 at 20:55
4
\$\begingroup\$

Python

I'll say it first.

object1, object2 = object2, object1
\$\endgroup\$
2
  • \$\begingroup\$ Also possible in Go. However, I think a temporary variable may still be made in the compiled code. \$\endgroup\$
    – Max Morin
    Nov 12 '12 at 15:22
  • \$\begingroup\$ This is also valid Ruby. \$\endgroup\$
    – steenslag
    Nov 15 '12 at 20:16
2
\$\begingroup\$

Mathematica

A standard feature:

a = 1; b = 2;
Print[a, " ", b];
{a, b} = {b, a};
Print[a, " ", b];

Output

(*
1 2

2 1
*)
\$\endgroup\$
2
\$\begingroup\$

PostScript

In PostScript, it's common to use the stack instead of named variables, so a simple

exch

would do the job. But if you have variables, you can do it like

/a 1 def
/b 2 def
/a b /b a def def
\$\endgroup\$
2
\$\begingroup\$

If you have access to the memory address of these objects, you can swap them by xor-ing their addresses.

// return 0 for success, 1 for error
int swap(void *object1, void *object2, int size)
{
    for (int i = 0; i < size; i++)
    {
        char *x = (char *)(object1 + i);
        char *y = (char *)(object2 + i);
        if (x == y)
            return 1;
        *x ^= *y;
        *y ^= *x;
        *x ^= *y;
    }
    return 0;
}

per dmckee suggestion for clarity

\$\endgroup\$
2
  • \$\begingroup\$ The solution is to pass a size as well, cast to char* and then xor swap the characters guarding against the same-value failure mode. Not as pretty, but standards compliant and working. \$\endgroup\$ Nov 11 '12 at 21:44
  • \$\begingroup\$ Only answer which doesn't do a,b = b,a. +1 :) \$\endgroup\$
    – beary605
    Nov 12 '12 at 22:16
1
\$\begingroup\$

Perl

($object1, $object2) = ($object2, $object1);

Using Perl's list assertions feels like cheating, but it's a pretty common feature.

\$\endgroup\$
1
\$\begingroup\$

PHP

Objects / arrays / resources:

list($a, $b) = [$b, $a];

Integers / booleans / strings of equal length:

$a ^= $b ^= $a ^= $b;
\$\endgroup\$
1
\$\begingroup\$

Forth

swap

As i have to include more than 30 characters, here's a link to a Forth primer

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.