32
\$\begingroup\$

Given an array of positive integers and two distinct valid indices, return the array with the two elements corresponding to the two indices swapped.

You may choose to use 0-indexing or 1-indexing, but the testcases below will be 0-indexed.

array        m n output
[1,2,3,4]    0 1 [2,1,3,4]
[5,8,9]      0 2 [9,8,5]
[11,13,15,3] 1 2 [11,15,13,3]
[11,13,15,3] 2 1 [11,15,13,3]
[11,15,15,3] 2 1 [11,15,15,3]

This is . Shortest answer in bytes wins. Standard loopholes apply.

\$\endgroup\$
14
  • \$\begingroup\$ Related, related. \$\endgroup\$
    – Leaky Nun
    Jun 22 '17 at 10:28
  • 2
    \$\begingroup\$ Huh, this may well be a task that many golfing languages have a hard time with but most practical languages find easy. (Lists with mutable elements aren't a common thing for golfing languages to have.) If that is the case, it'll be quite interesting. (The golfing languages will probably still win, though, because they're so much terser they can get away with a more complex algorithm.) \$\endgroup\$
    – user62131
    Jun 22 '17 at 10:36
  • 8
    \$\begingroup\$ Surprised this probably isn't a dupe, but this challenge is actually creative, since it's a real challenge for many golfing languages out there. \$\endgroup\$ Jun 22 '17 at 10:45
  • \$\begingroup\$ @LeakyNun I've got downvotes (and even delete votes) like that in the past, don't worry too much about it... \$\endgroup\$ Jun 22 '17 at 10:46
  • \$\begingroup\$ Can m and n be taken as an array? \$\endgroup\$
    – Okx
    Jun 22 '17 at 11:23

36 Answers 36

1
2
0
\$\begingroup\$

awk, 31 bytes

{c=$a;$a=$b;$b=c;a=$1;b=$2}NR>1

Try it online!

Takes input in the format

1 2
1 2 3 4

and outputs as

2 1 3 4

(1-indexed).

Explanation

The entire program is a missing pattern with an action followed by a pattern with a missing action.

Since a missing pattern runs on each line, the code inside the braces runs for both input lines. The c=$a;$a=$b;$b=c; part swaps the two values at indices a and b (through the temporary variable c). This only has an effect on the second line, since on the first line a and b are not yet defined. The a=$1;b=$2 part defines a to be the first field and b to be the second field, which sets the appropriate values for the first part to run on the second line.

Since a missing action is equivalent to {print}, the pattern prints every line it matches. This pattern in particular is NR>1: that is, print whenever the line number is greater than 1, which happens to be line 2. This runs after the swapping of values has taken place, thus completing the task.

\$\endgroup\$
0
\$\begingroup\$

q/kdb+, 17 bytes

Solution:

{@[x;(|)y;:;x y]}

Example:

q){@[x;(|)y;:;x y]}[1 2 3 4;0 1]
2 1 3 4

Explanation:

A q version of the k answer by Simon. Apply the assign : function to x at indices reverse-y with value of x indexed at y. Broken down you can see more clearly:

q)x:1 2 3 4
q)y:0 1
q)x y
1 2
q)(|)y
1 0
q)x(|)y
2 1
q)@[x;(|)y;:;x y]
2 1 3 4
\$\endgroup\$
0
\$\begingroup\$

Powershell, 64 49 bytes

$f=$args;$t=$f[0];($t[$f[1]],$t[$f[2]]=$t[$f[2]],$t[$f[1]])>$;$t

param($a,$b,$c)($a[$b],$a[$c]=$a[$c],$a[$b])>$;$a

At least somewhat better solution.

Also I have no idea why redirecting to $ even works ^^

\$\endgroup\$
0
\$\begingroup\$

Japt, 15 bytes

£gY¶V?W:Y¶W?V:Y

Test it


Explanation

       :Implicit input of array U and integers V & W
£      :Map over the array (with Y being the index of the current element), replacing each element with...
g      :  The element in the array at index...
Y¶V?W  :    W, if Y equals V
:Y¶W?V :    or V, if Y equals W
:Y     :    or just Y otherwise
       :Implicit output of modified array
\$\endgroup\$
2
  • \$\begingroup\$ Nice technique, looks a little verbose though... but I can't find a way to golf it. \$\endgroup\$ Jun 22 '17 at 16:52
  • \$\begingroup\$ @ETHproductions: yeah, I'm still convinced there's a shorter way, just haven't had the time to come back and find it and it looks like I won't until tomorrow :( \$\endgroup\$
    – Shaggy
    Jun 22 '17 at 16:54
0
\$\begingroup\$

V, 10 bytes

ÀGYÀGVpVp

Try it online!

This takes input with each number to a line, and indices as program arguments. 1-indexed.

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

C, 42 bytes (one extra argument on function call)

f(a,m,n,t)int*a;{t=a[m];a[m]=a[n];a[n]=t;}

Try it online

C, 44 bytes

f(a,m,n)int*a;{int t=a[m];a[m]=a[n];a[n]=t;}
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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