Skip to main content
deleted 25 characters in body
Source Link
Wheat Wizard
  • 100k
  • 22
  • 282
  • 661

Prolog (SWI), 7560 bytes

p([A,B],[BB]+[B,A]).
p([A,B,32|U],[B32|U]+[B,A,32|Y]):-p(U,Y)U+Y,!.
p([A|U],[A|Y])[A|U]+[A|Y]:-p(U,Y)U+Y.

Try it online!Try it online!

Explanation

First we define the base case:

p([A,B],[B,A]).

This means that the last two letters will always be swapped.

Then we define what happens if we are right next to a space:

p([A,B,32|U],[B,A,32|Y]):-p(U,Y),!.

Two strings match if right before a space the letters before the space are swapped and the remainder if the strings match. We then use ! to cut.

Our last case is if we are not next to a space the first two letters need to match.

p([A|U],[A|Y]):-p(U,Y).

Prolog (SWI), 75 bytes

p([A,B],[B,A]).
p([A,B,32|U],[B,A,32|Y]):-p(U,Y),!.
p([A|U],[A|Y]):-p(U,Y).

Try it online!

Explanation

First we define the base case:

p([A,B],[B,A]).

This means that the last two letters will always be swapped.

Then we define what happens if we are right next to a space:

p([A,B,32|U],[B,A,32|Y]):-p(U,Y),!.

Two strings match if right before a space the letters before the space are swapped and the remainder if the strings match. We then use ! to cut.

Our last case is if we are not next to a space the first two letters need to match.

p([A|U],[A|Y]):-p(U,Y).

Prolog (SWI), 60 bytes

[A,B]+[B,A].
[A,B,32|U]+[B,A,32|Y]:-U+Y,!.
[A|U]+[A|Y]:-U+Y.

Try it online!

Explanation

First we define the base case:

p([A,B],[B,A]).

This means that the last two letters will always be swapped.

Then we define what happens if we are right next to a space:

p([A,B,32|U],[B,A,32|Y]):-p(U,Y),!.

Two strings match if right before a space the letters before the space are swapped and the remainder if the strings match. We then use ! to cut.

Our last case is if we are not next to a space the first two letters need to match.

p([A|U],[A|Y]):-p(U,Y).
Source Link
Wheat Wizard
  • 100k
  • 22
  • 282
  • 661

Prolog (SWI), 75 bytes

p([A,B],[B,A]).
p([A,B,32|U],[B,A,32|Y]):-p(U,Y),!.
p([A|U],[A|Y]):-p(U,Y).

Try it online!

Explanation

First we define the base case:

p([A,B],[B,A]).

This means that the last two letters will always be swapped.

Then we define what happens if we are right next to a space:

p([A,B,32|U],[B,A,32|Y]):-p(U,Y),!.

Two strings match if right before a space the letters before the space are swapped and the remainder if the strings match. We then use ! to cut.

Our last case is if we are not next to a space the first two letters need to match.

p([A|U],[A|Y]):-p(U,Y).