Small integer swapping
In this simple challenge, create a short program that takes 2 int
s as input, output them, swap their places, then output them again. Those 2 integers can't be the same number. The program with the shortest solution wins! Loopholes are banned in this challenge.
An example C program for the challenge (mostly ungolfed):
#include <stdio.h>
int main() {
int x, y;
scanf("%i %i", &x, &y);
printf("%i %i", x, y);
int z = x;
x = y;
y = z;
printf("%i %i", x, y);
}
You can make 2 answers in one post: 1 with output swapping, and 1 with variable swapping.
J, 3 bytes ```J ,|. ``` …
2y ago
Ruby, 24 bytes ```ruby get …
2y ago
[C (clang)], 63 bytes …
3y ago
[Python 3.8 (pre-release)], 39 …
3y ago
Japt, 3 bytes I/O as an arr …
3y ago
[APL (Dyalog Extended)], 3 byt …
3y ago
Vyxal `o`, 5 bytes ``` $", …
3y ago
7 answers
Vyxal o
, 5 bytes
~$","
I'd say this counts, because $
is quite literally "swap the top two items on the stack".
Explained
~$","
~$ # Swap the top two items, but peek instead of pop
", # pair and output
" # pair what remains and output with the `o` flag
But if variables are absolutely needed:
Vyxal o
, 29 bytes
→x→y",←y→temp←x→y←temp→x←y←x"
Explained
→x→y",←y→temp←x→y←temp→x←y←x"
→x→y # put the two inputs into x and y
", # print [y, x]
←y→temp # temp = y
←x→y # y = x
←temp→x # x = temp
←y←x" # print [y, x] (now swapped)
C (clang), 63 bytes
x,y;main(){scanf("%i%i",&x,&y);printf("%i %i\n%i %i",x,y,y,x);}
1 comment thread