Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Small integer swapping

+1
−4

In this simple challenge, create a short program that takes 2 ints 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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

How is the mentioned loophole a loophole? (6 comments)

7 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+1
−0

Ruby, 24 bytes

gets=~/ /;puts$`,$'*2+$`

Try this online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Japt, 3 bytes

I/O as an array.

cUé

Try it

cUé     :Implicit input of array U
c       :Concatenate
 Ué     :  U rotated right once
History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

I removed the "loophole". (1 comment)
+1
−0

APL (Dyalog Extended), 3 bytes

⊢⍮⌽

Try it online!

 the argument

 paired up with

 its reverse

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

I removed the "loophole". (1 comment)
+1
−0

Python 3.8 (pre-release), 39 bytes

print(x:=input(),y:=input())
print(y,x)

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Vyxal o, 5 bytes

~$","

Try it Online!

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"

Try it Online!

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)
History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

I removed the "loophole". (1 comment)
+1
−0

J, 3 bytes

,|.

Try it online!

This hook y f (g y) appends y with its reverse.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

C (clang), 63 bytes

x,y;main(){scanf("%i%i",&x,&y);printf("%i %i\n%i %i",x,y,y,x);}

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »