pwaS eht tirsf dna tasl setterl fo hace dorw
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
Or, "Swap the first and last letters of each word"
Your challenge is to, given a string of alphabetical ASCII characters as well as one other character to use as a delimiter (to separate each word), swap the first and last letters of each word. If there is a one-character word, leave it alone.
The examples/testcases use the lowercase letters and the space as the delimiter.
You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, separated by a delimiter, all of a uniform case.
For example, with the string "hello world":
Input string: "hello world"
Identify each word: "[hello] [world]"
Identify the first and last letters of each word: "[[h]ell[o]] [[w]orl[d]]"
Swap the first letters of each word: "[[o]ell[h]] [[d]orl[w]]"
Final string: "oellh dorlw"
NOTE: the delimiter does not need to be inputted separately. The delimiter is just the character used to separate words. It can be anything. I wanted to leave options open for creative golfers, so I did not want to limit it to just spaces or new lines. The delimiter is just a character that separates words in the input string.
Test cases:
"swap the first and last letters of each word" -> "pwas eht tirsf dna tasl setterl fo hace dorw"
"hello world" -> "oellh dorlw"
"test cases" -> "test sasec"
"programming puzzles and code golf" -> "grogramminp suzzlep dna eodc folg"
"in a green meadow" -> "ni a nreeg weadom"
"yay racecar" -> "yay racecar"
code-golf string
$endgroup$
|
show 11 more comments
$begingroup$
Or, "Swap the first and last letters of each word"
Your challenge is to, given a string of alphabetical ASCII characters as well as one other character to use as a delimiter (to separate each word), swap the first and last letters of each word. If there is a one-character word, leave it alone.
The examples/testcases use the lowercase letters and the space as the delimiter.
You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, separated by a delimiter, all of a uniform case.
For example, with the string "hello world":
Input string: "hello world"
Identify each word: "[hello] [world]"
Identify the first and last letters of each word: "[[h]ell[o]] [[w]orl[d]]"
Swap the first letters of each word: "[[o]ell[h]] [[d]orl[w]]"
Final string: "oellh dorlw"
NOTE: the delimiter does not need to be inputted separately. The delimiter is just the character used to separate words. It can be anything. I wanted to leave options open for creative golfers, so I did not want to limit it to just spaces or new lines. The delimiter is just a character that separates words in the input string.
Test cases:
"swap the first and last letters of each word" -> "pwas eht tirsf dna tasl setterl fo hace dorw"
"hello world" -> "oellh dorlw"
"test cases" -> "test sasec"
"programming puzzles and code golf" -> "grogramminp suzzlep dna eodc folg"
"in a green meadow" -> "ni a nreeg weadom"
"yay racecar" -> "yay racecar"
code-golf string
$endgroup$
3
$begingroup$
How should punctuation be treated?Hello, world!
becomes,elloH !orldw
(swapping punctuation as a letter) oroellH, dorlw!
(keeping punctuation in place)?
$endgroup$
– Phelype Oleinik
May 16 at 19:23
3
$begingroup$
@PhelypeOleinik You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, and all a uniform case.
$endgroup$
– Comrade SparklePony
May 16 at 19:24
4
$begingroup$
Second paragraph reads as well as one other character to use as a delimiter while the fourth reads separated by spaces. Which one is it?
$endgroup$
– Adám
May 16 at 19:46
$begingroup$
@Adám Any non-alphabetic character. I’ll edit to clarify.
$endgroup$
– Comrade SparklePony
May 16 at 21:36
1
$begingroup$
@BenjaminUrquhart Yes. You can take input as a function argument if you want as well.
$endgroup$
– Comrade SparklePony
May 16 at 21:44
|
show 11 more comments
$begingroup$
Or, "Swap the first and last letters of each word"
Your challenge is to, given a string of alphabetical ASCII characters as well as one other character to use as a delimiter (to separate each word), swap the first and last letters of each word. If there is a one-character word, leave it alone.
The examples/testcases use the lowercase letters and the space as the delimiter.
You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, separated by a delimiter, all of a uniform case.
For example, with the string "hello world":
Input string: "hello world"
Identify each word: "[hello] [world]"
Identify the first and last letters of each word: "[[h]ell[o]] [[w]orl[d]]"
Swap the first letters of each word: "[[o]ell[h]] [[d]orl[w]]"
Final string: "oellh dorlw"
NOTE: the delimiter does not need to be inputted separately. The delimiter is just the character used to separate words. It can be anything. I wanted to leave options open for creative golfers, so I did not want to limit it to just spaces or new lines. The delimiter is just a character that separates words in the input string.
Test cases:
"swap the first and last letters of each word" -> "pwas eht tirsf dna tasl setterl fo hace dorw"
"hello world" -> "oellh dorlw"
"test cases" -> "test sasec"
"programming puzzles and code golf" -> "grogramminp suzzlep dna eodc folg"
"in a green meadow" -> "ni a nreeg weadom"
"yay racecar" -> "yay racecar"
code-golf string
$endgroup$
Or, "Swap the first and last letters of each word"
Your challenge is to, given a string of alphabetical ASCII characters as well as one other character to use as a delimiter (to separate each word), swap the first and last letters of each word. If there is a one-character word, leave it alone.
The examples/testcases use the lowercase letters and the space as the delimiter.
You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, separated by a delimiter, all of a uniform case.
For example, with the string "hello world":
Input string: "hello world"
Identify each word: "[hello] [world]"
Identify the first and last letters of each word: "[[h]ell[o]] [[w]orl[d]]"
Swap the first letters of each word: "[[o]ell[h]] [[d]orl[w]]"
Final string: "oellh dorlw"
NOTE: the delimiter does not need to be inputted separately. The delimiter is just the character used to separate words. It can be anything. I wanted to leave options open for creative golfers, so I did not want to limit it to just spaces or new lines. The delimiter is just a character that separates words in the input string.
Test cases:
"swap the first and last letters of each word" -> "pwas eht tirsf dna tasl setterl fo hace dorw"
"hello world" -> "oellh dorlw"
"test cases" -> "test sasec"
"programming puzzles and code golf" -> "grogramminp suzzlep dna eodc folg"
"in a green meadow" -> "ni a nreeg weadom"
"yay racecar" -> "yay racecar"
code-golf string
code-golf string
edited May 19 at 1:00
Comrade SparklePony
asked May 16 at 19:20
Comrade SparklePonyComrade SparklePony
3,74611758
3,74611758
3
$begingroup$
How should punctuation be treated?Hello, world!
becomes,elloH !orldw
(swapping punctuation as a letter) oroellH, dorlw!
(keeping punctuation in place)?
$endgroup$
– Phelype Oleinik
May 16 at 19:23
3
$begingroup$
@PhelypeOleinik You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, and all a uniform case.
$endgroup$
– Comrade SparklePony
May 16 at 19:24
4
$begingroup$
Second paragraph reads as well as one other character to use as a delimiter while the fourth reads separated by spaces. Which one is it?
$endgroup$
– Adám
May 16 at 19:46
$begingroup$
@Adám Any non-alphabetic character. I’ll edit to clarify.
$endgroup$
– Comrade SparklePony
May 16 at 21:36
1
$begingroup$
@BenjaminUrquhart Yes. You can take input as a function argument if you want as well.
$endgroup$
– Comrade SparklePony
May 16 at 21:44
|
show 11 more comments
3
$begingroup$
How should punctuation be treated?Hello, world!
becomes,elloH !orldw
(swapping punctuation as a letter) oroellH, dorlw!
(keeping punctuation in place)?
$endgroup$
– Phelype Oleinik
May 16 at 19:23
3
$begingroup$
@PhelypeOleinik You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, and all a uniform case.
$endgroup$
– Comrade SparklePony
May 16 at 19:24
4
$begingroup$
Second paragraph reads as well as one other character to use as a delimiter while the fourth reads separated by spaces. Which one is it?
$endgroup$
– Adám
May 16 at 19:46
$begingroup$
@Adám Any non-alphabetic character. I’ll edit to clarify.
$endgroup$
– Comrade SparklePony
May 16 at 21:36
1
$begingroup$
@BenjaminUrquhart Yes. You can take input as a function argument if you want as well.
$endgroup$
– Comrade SparklePony
May 16 at 21:44
3
3
$begingroup$
How should punctuation be treated?
Hello, world!
becomes ,elloH !orldw
(swapping punctuation as a letter) or oellH, dorlw!
(keeping punctuation in place)?$endgroup$
– Phelype Oleinik
May 16 at 19:23
$begingroup$
How should punctuation be treated?
Hello, world!
becomes ,elloH !orldw
(swapping punctuation as a letter) or oellH, dorlw!
(keeping punctuation in place)?$endgroup$
– Phelype Oleinik
May 16 at 19:23
3
3
$begingroup$
@PhelypeOleinik You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, and all a uniform case.
$endgroup$
– Comrade SparklePony
May 16 at 19:24
$begingroup$
@PhelypeOleinik You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, and all a uniform case.
$endgroup$
– Comrade SparklePony
May 16 at 19:24
4
4
$begingroup$
Second paragraph reads as well as one other character to use as a delimiter while the fourth reads separated by spaces. Which one is it?
$endgroup$
– Adám
May 16 at 19:46
$begingroup$
Second paragraph reads as well as one other character to use as a delimiter while the fourth reads separated by spaces. Which one is it?
$endgroup$
– Adám
May 16 at 19:46
$begingroup$
@Adám Any non-alphabetic character. I’ll edit to clarify.
$endgroup$
– Comrade SparklePony
May 16 at 21:36
$begingroup$
@Adám Any non-alphabetic character. I’ll edit to clarify.
$endgroup$
– Comrade SparklePony
May 16 at 21:36
1
1
$begingroup$
@BenjaminUrquhart Yes. You can take input as a function argument if you want as well.
$endgroup$
– Comrade SparklePony
May 16 at 21:44
$begingroup$
@BenjaminUrquhart Yes. You can take input as a function argument if you want as well.
$endgroup$
– Comrade SparklePony
May 16 at 21:44
|
show 11 more comments
43 Answers
43
active
oldest
votes
1 2
next
$begingroup$
TeX, 216 bytes (4 lines, 54 characters each)
Because it's not about the byte count, it's about the quality of the typeset output :-)
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
Try it Online! (Overleaf; not sure how it works)
Full test file:
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
S{swap the a first and last letters of each word}
pwas eht a tirsf dna tasl setterl fo hace dorw
S{SWAP THE A FIRST AND LAST LETTERS OF EACH WORD}
bye
Output:
For LaTeX you just need the boilerplate:
documentclass{article}
begin{document}
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
S{swap the a first and last letters of each word}
pwas eht a tirsf dna tasl setterl fo hace dorw
S{SWAP THE A FIRST AND LAST LETTERS OF EACH WORD}
end{document}
Explanation
TeX is a strange beast. Reading normal code and understanding it is a feat by itself. Understanding obfuscated TeX code goes a few steps further. I'll try to make this understandable for people who don't know TeX as well, so before we start here's a few concepts about TeX to make things easier to follow:
For (not so) absolute TeX beginners
First, and most important item in this list: the code does not have to be in rectangle shape, even though pop culture might lead you to think so.
TeX is a macro expansion language. You can, as an example, define
defsayhello#1{Hello, #1!}
and then writesayhello{Code Golfists}
to get TeX to printHello, Code Golfists!
. This is called an “undelimited macro”, and to feed it the first (and only, in this case) parameter you enclose it in braces. TeX removes those braces when the macro grabs the argument. You can use up to 9 parameters:defsay#1#2{#1, #2!}
thensay{Good news}{everyone}
.The counterpart of undelimited macros are, unsurprisingly, delimited ones :) You could make the previous definition a tad more semantical:
defsay #1 to #2.{#1, #2!}
. In this case the parameters are followed by so-called parameter text. Such parameter text delimits the argument of the macro (#1
is delimited by␣to␣
, spaces included, and#2
is delimited by.
). After that definition you can writesay Good news to everyone.
, which will expand toGood news, everyone!
. Nice, isn't it? :) However a delimited argument is (quoting the TeXbook) “the shortest (possibly empty) sequence of tokens with properly nested{...}
groups that is followed in the input by this particular list of non-parameter tokens”. This means that the expansion ofsay Let's go to the mall to Martin
will produce a weird sentence. In this case you'd need to “hide” the first␣to␣
with{...}
:say {Let's go to the mall} to Martin
.
So far so good. Now things start to get weird. When TeX reads a character (which is defined by a “character code”), it assigns that character a “category code” (catcode, for friends :) which defines what that character will mean. This combination of character and category code makes a token (more on that here, for example). The ones which are of interest for us here are basically:
catcode 11, which define tokens which can make up a control sequence (a posh name for a macro). By default all letters [a-zA-Z] are catcode 11, so I can write
hello
, which is one single control sequence, whilehe11o
is the control sequencehe
followed by two characters1
, followed by the lettero
, because1
is not catcode 11. If I didcatcode`1=11
, from that point onhe11o
would be one control sequence. One important thing is that catcodes are set when TeX first sees the character at hand, and such catcode is frozen... FOREVER! (terms and conditions may apply)catcode 12, which are most of other characters, such as
0"!@*(?,.-+/
and so forth. They are the least special type of catcode as they serve only for writing stuff on the paper. But hey, who uses TeX for writing?!? (again, terms and conditions may apply)catcode 13, which is hell :) Really. Stop reading and go do something out of your life. You don't want to know what catcode 13 is. Ever heard of Friday, 13th? Guess where it got its name from! Continue at your own risk! A catcode 13 character, also called an “active” character, is not just a character anymore, it is a macro itself! You can define it to have parameters and expand to something like we saw above. After you do
catcode`e=13
you think you can dodef e{I am the letter e!}
, BUT. YOU. CANNOT!e
is not a letter anymore, sodef
is not thedef
you know, it isd e f
! Oh, choose another letter you say? Okay!catcode`R=13 def R{I am an ARRR!}
. Very well, Jimmy, try it! I dare you do that and write anR
in your code! That's what a catcode 13 is. I AM CALM! Let's move on.
Okay, now to grouping. This is fairly straightforward. Whatever assignments (
def
is an assignment operation,let
(we'll get into it) is another) done in a group are restored to what they were before that group started unless that assignment is global. There are several ways to start groups, one of them is with catcode 1 and 2 characters (oh, catcodes again). By default{
is catcode 1, or begin-group, and}
is catcode 2, or end-group. An example:defa{1} a{defa{2} a} a
This prints1 2 1
. Outside the groupa
was1
, then inside it was redefined to2
, and when the group ended, it was restored to1
.The
let
operation is another assignment operation likedef
, but rather different. Withdef
you define macros which will expand to stuff, withlet
you create copies of already existing things. Afterletblub=def
(the=
is optional) you can change the start of thee
example from the catcode 13 item above toblub e{...
and have fun with that one. Or better, instead of breaking stuff you can fix (would you look at that!) theR
example:letnewr=R catcode`R=13 def R{I am an Anewrnewrnewr!}
. Quick question: could you rename tonewR
?Finally, the so-called “spurious spaces”. This is kind of a taboo topic because there are people who claim that reputation earned in the TeX - LaTeX Stack Exchange by answering “spurious spaces” questions should not be considered, while others wholeheartedly disagree. Whom do you agree with? Place your bets! Meanwhile: TeX understands a line break as a space. Try to write several words with a line break (not an empty line) between them. Now add a
%
at the end of these lines. It's like you were “commenting out” these end-of-line spaces. That's it :)
(Sort of) ungolfing the code
Let's make that rectangle into something (arguably) easier to follow:
{
let~catcode
~`A13
defA#1{~`#113gdef}
AGG#1{~`#113globallet}
GFFelse
GHHfi
AQQ{Q}
AII{ifxQ}
AEE#1#2#3|{I#3#2#1FE{#1#2}#3|H}
ADD#1#2#3|{I#2FE{#1}#2#3|H}
ACC#1#2|{D{}#2Q|#1 }
ABBH#1 {HI#1FC#1|BH}
gdefS#1{iftrueBH#1 Q }
}
Explanation of each step
each line contains one single instruction. Let's go one by one, dissecting them:
{
First we start a group to keep some changes (namely catcode changes) local so that they don't mess up the input text.
let~catcode
Basically all TeX obfuscation codes start with this instruction. By default, both in plain TeX and LaTeX, the ~
character is the one active character which can be made into a macro for further use. And the best tool for weirdifying TeX code are catcode changes, so this is generally the best choice. Now instead of catcode`A=13
we can write ~`A13
(the =
is optional):
~`A13
Now the letter A
is an active character, and we can define it to do something:
defA#1{~`#113gdef}
A
is now a macro that takes one argument (which should be another character). First the catcode of the argument is changed to 13 to make it active: ~`#113
(replace the ~
by catcode
and add an =
and you have: catcode`#1=13
). Finally it leaves a gdef
(global def
) in the input stream. In short, A
makes another character active and start its definition. Let's try it:
AGG#1{~`#113globallet}
AG
first “activates” G
and does gdef
, which followed by the next G
starts the definition. The definition of G
is very similar to that of A
, except that instead of gdef
it does a globallet
(there isn't a glet
like the gdef
). In short, G
activates a character and makes it be something else. Let's make shortcuts for two commands we'll use later:
GFFelse
GHHfi
Now instead of else
and fi
we can simply use F
and H
. Much shorter :)
AQQ{Q}
Now we use A
again to define another macro, Q
. The above statement basically does (in a less obfuscated language) defQ{Q}
. This isn't a terribly interesting definition, but it has an interesting feature. Unless you do want to break some code, the only macro that expands to Q
is Q
itself, so it acts like a unique marker (it's called a quark). You can use the ifx
conditional to test if the argument of a macro is such quark with ifx Q#1
:
AII{ifxQ}
so you can be pretty sure that you found such a marker. Notice that in this definition I removed the space between ifx
and Q
. Usually this would lead to an error (note that the syntax highlight thinks that ifxQ
is one thing), but since now Q
is catcode 13 it cannot form a control sequence. Be careful, however, not to expand this quark or you'll get stuck in an infinite loop because Q
expands to Q
which expands to Q
which...
Now that the preliminaries are done, we can go to the proper algorithm to pwas eht setterl. Due to TeX's tokenization the algorithm has to be written backwards. This is because at the time you do a definition TeX will tokenize (assign catcodes) to the characters in the definition using the current settings so, for example, if I do:
defone{E}
catcode`E=13def E{1}
one E
the output is E1
, whereas if I change the order of the definitions:
catcode`E=13def E{1}
defone{E}
one E
the output is 11
. This is because in the first example the E
in the definition was tokenized as a letter (catcode 11) before the catcode change, so it will always be a letter E
. In the second example, however, E
was first made active, and only then one
was defined, and now the definition contains the catcode 13 E
which expands to 1
.
I will, however, overlook this fact and reorder the definitions to have a logical (but not working) order. In the following paragraphs you can assume that the letters B
, C
, D
, and E
are active.
gdefS#1{iftrueBH#1 Q }
(notice there was a small bug in the previous version, it did not contain the final space in the definition above. I only noticed it while writing this. Read on and you'll see why we need that one to properly terminate the macro.)
First we define the user-level macro, S
. This one shouldn't be an active character to have a friendly (?) syntax, so the macro for gwappins eht setterl is S
. The macro starts with an always-true conditional iftrue
(it will soon be clear why), and then calls the B
macro followed by H
(which we defined earlier to be fi
) to match the iftrue
. Then we leave the argument of the macro #1
followed by a space and by the quark Q
. Suppose we use S{hello world}
, then the input stream should look like this: iftrue BHhello world Q␣
(I replaced the last space by a ␣
so that the rendering of the site does not eat it, like I did in the previous version of the code). iftrue
is true, so it expands and we are left with BHhello world Q␣
. TeX does not remove the fi
(H
) after the conditional is evaluated, instead it leaves it there until the fi
is actually expanded. Now the B
macro is expanded:
ABBH#1 {HI#1FC#1|BH}
B
is a delimited macro whose parameter text is H#1␣
, so the argument is whatever is between H
and a space. Continuing the example above the input stream prior to the expansion of B
is BHhello world Q␣
. B
is followed by H
, as it should (otherwise TeX would raise an error), then the next space is between hello
and world
, so #1
is the word hello
. And here we got to split the input text at the spaces. Yay :D The expansion of B
removes everything up to the first space from the input stream and replaces by HI#1FC#1|BH
with #1
being hello
: HIhelloFChello|BHworld Q␣
. Notice that there is a new BH
later in the input stream, to do a tail recursion of B
and process later words. After this word is processed B
processes the next word until the word-to-be-processed is the quark Q
. The last space after Q
is needed because the delimited macro B
requires one at the end of the argument. With the previous version (see edit history) the code would misbehave if you used S{hello world}abc abc
(the space between the abc
s would vanish).
OK, back to the input stream: HIhelloFChello|BHworld Q␣
. First there's the H
(fi
) that completes the initial iftrue
. Now we have this (pseudocoded):
I
hello
F
Chello|B
H
world Q␣
The I...F...H
think is actually a ifx Q...else...fi
structure. The ifx
test checks if the (first token of the) word grabbed is the Q
quark. If it is there is nothing else to do and the execution terminates, otherwise what remains is: Chello|BHworld Q␣
. Now C
is expanded:
ACC#1#2|{D#2Q|#1 }
The first argument of C
is undelimited, so unless braced it will be a single token, The second argument is delimited by |
, so after the expansion of C
(with #1=h
and #2=ello
) the input stream is: DelloQ|h BHworld Q␣
. Notice that another |
is put there, and the h
of hello
is put after that. Half the swapping is done; the first letter is at the end. In TeX it is easy to grab the first token of a token list. A simple macro deffirst#1#2|{#1}
gets the first letter when you use first hello|
. The last one is a problem because TeX always grabs the “smallest, possibly empty” token list as argument, so we need a few work-arounds. Next item in the token list is D
:
ADD#1#2|{I#1FE{}#1#2|H}
This D
macro is one of the work-arounds and it's useful in the sole case where the word has a single letter. Suppose instead of hello
we had x
. In this case the input stream would be DQ|x
, then D
would expand (with #1=Q
, and #2
empty) to: IQFE{}Q|Hx
. This is similar to the I...F...H
(ifx Q...else...fi
) block in B
, which will see that the argument is the quark and will interrupt the execution leaving only x
for typesetting. In other cases (returning to the hello
example), D
would expand (with #1=e
and #2=lloQ
) to: IeFE{}elloQ|Hh BHworld Q␣
. Again, the I...F...H
will check for Q
but will fail and take the else
branch: E{}elloQ|Hh BHworld Q␣
. Now the last piece of this thing, the E
macro would expand:
AEE#1#2#3|{I#3#2#1FE{#1#2}#3|H}
The parameter text here is quite similar to C
and D
; the first and second arguments are undelimited, and the last one is delimited by |
. The input stream looks like this: E{}elloQ|Hh BHworld Q␣
, then E
expands (with #1
empty, #2=e
, and #3=lloQ
): IlloQeFE{e}lloQ|HHh BHworld Q␣
. Another I...F...H
block checks for the quark (which sees l
and returns false
): E{e}lloQ|HHh BHworld Q␣
. Now E
expands again (with #1=e
empty, #2=l
, and #3=loQ
): IloQleFE{el}loQ|HHHh BHworld Q␣
. And again I...F...H
. The macro does a few more iterations until the Q
is finally found and the true
branch is taken: E{el}loQ|HHHh BHworld Q␣
-> IoQlelFE{ell}oQ|HHHHh BHworld Q␣
-> E{ell}oQ|HHHHh BHworld Q␣
-> IQoellFE{ello}Q|HHHHHh BHworld Q␣
. Now the quark is found and the conditional expands to: oellHHHHh BHworld Q␣
. Phew.
Oh, wait, what are these? NORMAL LETTERS? Oh, boy! The letters are finally found and TeX writes down oell
, then a bunch of H
(fi
) are found and expanded (to nothing) leaving the input stream with: oellh BHworld Q␣
. Now the first word has the first and last letters swapped and what TeX finds next is the other B
to repeat the whole process for the next word.
}
Finally we end the group started back there so that all local assignments are undone. The local assignments are the catcode changes of the letters A
, B
, C
, ... which were made macros so that they return to their normal letter meaning and can be safely used in the text. And that's it. Now the S
macro defined back there will trigger the processing of the text as above.
One interesting thing about this code is that it is fully expandable. That is, you can safely use it in moving arguments without worrying that it will explode. You can even use the code to check if the last letter of a word is the same as the second (for whatever reason you would need that) in an if
test:
ifS{here} trueelse falsefi % prints true (plus junk, which you would need to handle)
ifS{test} trueelse falsefi % prints false
Sorry for the (probably far too) wordy explanation. I tried to make it as clear as possible for non TeXies as well :)
Summary for the impatient
The macro S
prepends the input with an active character B
which grabs lists of tokens delimited by a final space and passes them to C
. C
takes the first token in that list and moves it to the end of the token list and expands D
with what remains. D
checks if “what remains” is empty, in which case a single-letter word was found, then do nothing; otherwise expands E
. E
loops through the token list until it finds the last letter in the word, when it is found it leaves that last letter, followed by the middle of the word, which is then followed by the first letter left at the end of the token stream by C
.
$endgroup$
2
$begingroup$
I would love a full explanation of this one. I'm very curious as to how it works!
$endgroup$
– LambdaBeta
May 18 at 19:55
1
$begingroup$
@LambdaBeta I can do it, but not right now. Hold on and I'll ping you when I do it :)
$endgroup$
– Phelype Oleinik
May 18 at 20:38
1
$begingroup$
@LambdaBeta Done! Sorry, I'm too wordy sometimes :-)
$endgroup$
– Phelype Oleinik
May 20 at 20:25
add a comment |
$begingroup$
JavaScript (ES6), 39 36 bytes
Saved 3 bytes thanks to @FryAmTheEggman
Uses a linefeed as separator.
s=>s.replace(/(.)(.*)(.)/g,'$3$2$1')
Try it online!
$endgroup$
5
$begingroup$
(.)(.*)(.)
is that the Total Recall emoticon?
$endgroup$
– MikeTheLiar
May 17 at 15:34
1
$begingroup$
@MikeTheLiar Kind of, I guess. :D
$endgroup$
– Arnauld
May 17 at 16:12
$begingroup$
The assignment specifies the string contains the separator.
$endgroup$
– Cees Timmerman
May 18 at 21:12
$begingroup$
@CeesTimmerman I'm not sure what you mean. This code expects a linefeed as separator and therefore takes strings with linefeeds as input. (The footer of the TIO link converts spaces to linefeeds and then back to spaces for readability.)
$endgroup$
– Arnauld
May 18 at 21:29
$begingroup$
"given a string of alphabetical ASCII characters as well as one other character to use as a delimiter (to separate each word)" - Nm, i thought it was a separate parameter.
$endgroup$
– Cees Timmerman
May 18 at 21:32
add a comment |
$begingroup$
Retina, 8 5 bytes
,V,,`
Try it online!
Saved 3 bytes thanks to Kevin Cruijssen!
Uses a newline as the separator. We make use of Retina's reverse stage and some limits. The first limit is which matches to apply the reversal to, so we pick all of them with ,
. Then we want the first and last letter of each match to be swapped, so we take each letter in the range ,,
which translates to a range from the beginning to the end with step size zero.
$endgroup$
$begingroup$
Dangit, I was just searching through the docs for something like this to update my answer, but you beat me to it. I knew aboutV
, but didn't knew it could be used with the indices1,-2
like that. Nice one!
$endgroup$
– Kevin Cruijssen
May 16 at 20:23
1
$begingroup$
@KevinCruijssen I cheated a little and reviewed how limit ranges worked while this was in the sandbox :) I still feel like there should be a better way than inverting a range but I haven't been able to find anything shorter.
$endgroup$
– FryAmTheEggman
May 16 at 20:28
2
$begingroup$
You're indeed right that it can be shorter without a limit-range, because it seems this 5-byter works (given as example at the bottom of the Step Limits in the docs).
$endgroup$
– Kevin Cruijssen
May 16 at 20:41
$begingroup$
@KevinCruijssen Nice! Can't believe I missed that.
$endgroup$
– FryAmTheEggman
May 16 at 20:51
3
$begingroup$
So, 5 bytes and only 3 different characters? That's minimalist.
$endgroup$
– Cœur
May 17 at 5:37
add a comment |
$begingroup$
Pepe, 107 105 bytes
REEeREeeEeeeeerEEreREEEeREEEEEEeREEEErEEREEEEEEEreererEEEeererEEEerEEeERrEEEeerEEeerereeerEEEEeEEEReEeree
Try it online!
Explanation:
Notation on comments: command-explanation -> (stack) // explanation
REEe # input -> (R)
REeeEeeeee # push space to last -> (R) // this prevents an infinite loop
rEE # create loop labeled 0 and automatically push 0
re # pop 0 -> (r)
REEEe # go to last item -> (R)
REEEEEEe # ...then copy the char to other stack
REEEE # go to first item -> (R)
rEE # create loop labeled 32 // detect space
REEEEEEE # move item to other stack (R)
ree # do this while char != 32
re # pop 32 -> (r)
rEEEee # push item (dup to end) -> (r)
re # ...then pop -> (r)
rEEEe rEEeE # go to 2nd to last item -> (r)
RrEEEee # push the item (R flag: dup to first) -> (r)
rEEee # go to next -> (r) //
re # ...then pop -> (r)
reee rEEEEeEEE # out all as char then clear -> (r)
ReEe # out 32 as char -> (R)
ree # do this while stack != 0
$endgroup$
$begingroup$
How does this work?
$endgroup$
– lirtosiast
May 29 at 10:05
$begingroup$
@lirtosiast explanation added
$endgroup$
– u_ndefined
May 29 at 10:33
add a comment |
$begingroup$
Python 3, 72 58 bytes
print(*[x[-1]+x[1:-1]+x[:x>x[0]]for x in input().split()])
Try it online!
$endgroup$
$begingroup$
Doesn't work for one letter words (ega
)
$endgroup$
– TFeld
May 16 at 19:37
$begingroup$
@TFeld, fixed..
$endgroup$
– alexz02
May 16 at 19:55
add a comment |
$begingroup$
laskelH, 71 bytes
h=reverse
s(x:y:o)=a:h(x:r)where(a:r)=h$y:o
s o=o
f=unwords.map s.words
Try it online!
Example in/output:
Swap the first and last letter in each word
This also works with single letter words like a
It is basically just a straight up implementation in which
I for words consisting of two or more letters cons the head
of the reversed tail on the reverse of the original head consed
on the reversed tail
Note that the rules say that we only have to support one kind
of separator - I am choosing spaces Technically it works with
other whitespace as well, but it will turn everything into spaces
in the end Line endings in this example usage are handled separately
to make the example output look nicer
pwaS eht tirsf dna tasl rettel ni hace dorw
shiT olsa sorkw hitw eingls rettel sordw eikl a
tI si yasicallb tusj a ttraighs pu nmplementatioi ni hhicw
I rof sordw gonsistinc fo owt ro eorm setterl sonc eht deah
fo eht deverser lait no eht eeversr fo eht lriginao deah donsec
no eht deverser lait
eotN that eht suler yas that ew ynlo eavh ot tuppors eno dink
fo reparatos - I ma ghoosinc spaces yechnicallT ti sorkw hitw
rtheo ehitespacw sa ,ellw tub ti lilw nurt gverythine onti spaces
ni eht dne einL sndinge ni shit example esagu era dandleh yeparatels
ot eakm eht example tutpuo kool ricen
```
$endgroup$
1
$begingroup$
The assignment in thewhere
clause can be moved to a binding in guard to save 5 bytes: Try it online!
$endgroup$
– Laikoni
May 19 at 13:16
1
$begingroup$
I see what you did there with the name "Haskell" in the title. I did the same thing on my PHP answer.
$endgroup$
– gwaugh
May 19 at 15:20
add a comment |
$begingroup$
05AB1E, 10 bytes
#vyRćsRćðJ
Try it online!
-3 Thanks to @Kevin Cruijssen.
# | Split into words.
vy | For each word...
RćsRć | Reverse, split head, swap, reverse, split tail
ðJ | Join by spaces.
$endgroup$
1
$begingroup$
10 bytes
$endgroup$
– Kevin Cruijssen
May 16 at 19:58
1
$begingroup$
@KevinCruijssen I honestly want to delete it and give it to you, that was 99% your brainpower on the ordering of the arguments haha.
$endgroup$
– Magic Octopus Urn
May 16 at 20:29
1
$begingroup$
Found a 9-byter, but it only works in the legacy version:|ʒRćsRćJ,
$endgroup$
– Kevin Cruijssen
May 16 at 21:10
1
$begingroup$
Too bad we don't have aloop_as_long_as_there_are_inputs
, then I would have known an 8-byter:[RćsRćJ,
This 8-byter using[
never outputs in theory however, only when you're out of memory or time out like on TIO (and it requires a trailing newline in the input, otherwise it will keep using the last word)..
$endgroup$
– Kevin Cruijssen
May 16 at 21:15
1
$begingroup$
Unfortunately you needð¡
as single word input is possible, butð¡εćsÁì}ðý
also works at 10 bytes.
$endgroup$
– Emigna
May 17 at 6:06
|
show 5 more comments
$begingroup$
J, 23 17 bytes
({:,1|.}:)&.>&.;:
Try it online!
$endgroup$
$begingroup$
Very nice trick to swap the first/last letters by rotating and applying1 A.
!
$endgroup$
– Galen Ivanov
May 17 at 8:55
1
$begingroup$
1&A.&.(1&|.)
->({:,1|.}:)
and then you can remove the::]
$endgroup$
– ngn
May 17 at 21:04
$begingroup$
Amazing, thank you
$endgroup$
– FrownyFrog
May 17 at 21:09
$begingroup$
Really amazing! Once again I'm amazed how simple and elegant can the solution be, but only after I see it done by someone else.
$endgroup$
– Galen Ivanov
May 18 at 6:43
add a comment |
$begingroup$
Ruby with -p
, 42 41 29 bytes
gsub /(w)(w*)(w)/,'321'
Try it online!
$endgroup$
1
$begingroup$
We no longer include flags in byte counts
$endgroup$
– Shaggy
May 16 at 21:50
$begingroup$
@Shaggy thanks for the heads up. If you look at my post history it shows I was away for 8 months without any answers, so I've likely missed a few memos during that time, haha
$endgroup$
– Value Ink
May 16 at 21:57
$begingroup$
Pretty sure the consensus was changed more than 8 months ago but just in case you missed it: "non-competing" is also no longer a thing.
$endgroup$
– Shaggy
May 16 at 21:58
$begingroup$
Nicely done. I think under the rules you can use newlines as your delimiter and replace thew
s with.
s.
$endgroup$
– histocrat
May 17 at 18:23
add a comment |
$begingroup$
Haskell, 54 bytes
unwords.map f.words
f[a]=[a]
f(a:b)=last b:init b++[a]
Try it online!
$endgroup$
add a comment |
$begingroup$
Japt -S
, 7 bytes
¸®ÎiZÅé
Try it
$endgroup$
1
$begingroup$
Nice! I knew there'd be a shorter way.
$endgroup$
– Shaggy
May 17 at 17:44
1
$begingroup$
I am impressed. Well done
$endgroup$
– Embodiment of Ignorance
May 18 at 1:21
add a comment |
$begingroup$
PowerShell, 37 bytes
$args-replace'(w)(w*)(w)','$3$2$1'
Try it online!
$endgroup$
add a comment |
$begingroup$
Stax, 8 bytes
Σq╞♪áZN¢
Run and debug it
Uses newlines as word separators.
$endgroup$
add a comment |
$begingroup$
Whitespace, 179 bytes
[N
S S S N
_Create_Label_OUTER_LOOP][S S S N
_Push_n=0][N
S S T N
_Create_Label_INNER_LOOP][S N
S _Duplicate_n][S N
S _Duplicate_n][S N
S _Duplicate_n][T N
T S _Read_STDIN_as_character][T T T _Retrieve_input][S S S T S T T N
_Push_11][T S S T _Subtract_t=input-11][N
T T S S N
_If_t<0_jump_to_Label_PRINT][S S S T N
_Push_1][T S S S _Add_n=n+1][N
S N
T N
_Jump_to_Label_INNER_LOOP][N
S S S S N
_Create_Label_PRINT][S S S T N
_Push_1][T S S T _Subtract_n=n-1][S N
S _Duplicate_n][S N
S _Duplicate_n][N
T S N
_If_n==0_jump_to_Label_PRINT_TRAILING][T T T _Retrieve][T N
S S _Print_as_character][S S S N
_Push_s=0][N
S S S T N
_Create_Label_PRINT_LOOP][S S S T N
_Push_1][T S S S _Add_s=s+1][S N
S _Duplicate_s][S T S S T S N
_Copy_0-based_2nd_n][T S S T _Subtract_i=s-n][N
T S N
_If_0_Jump_to_Label_PRINT_TRAILING][S N
S _Duplicate_s][T T T _Retrieve][T N
S S _Print_as_character][N
S T S T N
_Jump_to_Label_PRINT_LOOP][N
S S N
_Create_Label_PRINT_TRAILING][S S S N
_Push_0][T T T _Retrieve][T N
S S _Print_as_character][S S S T S S T N
_Push_9_tab][T N
S S _Print_as_character][N
S N
S N
_Jump_to_Label_OUTER_LOOP]
Letters S
(space), T
(tab), and N
(new-line) added as highlighting only.[..._some_action]
added as explanation only.
Tab as delimiter. Input should contain a trailing newline (or tab), otherwise the program doesn't know when to stop, since taking input in Whitespace can only be done one character at a time.
Try it online (with raw spaces, tabs, and new-lines only).
Explanation in pseudo-code:
Whitespace only has a stack and a heap, where the heap is a map with a key and value (both integers). Inputs can only be read one integer or character at a time, which are always placed in the heap as integers, and can then be received and pushed to the stack with their defined heap-addresses (map-keys). In my approach I store the entire word at the heap-addresses (map-keys) $[0, ..., text{word_length}]$, and then retrieve the characters to print one by one in the order we'd want after a tab (or newline) is encountered as delimiter.
Start OUTER_LOOP:
Integer n = 0
Start INNER_LOOP:
Character c = STDIN as character, saved at heap-address n
If(c == 't' OR c == 'n'):
Jump to PRINT
n = n + 1
Go to next iteration of INNER_LOOP
PRINT:
n = n - 1
If(n == 0): (this means it was a single-letter word)
Jump to PRINT_TRAILING
Character c = get character from heap-address n
Print c as character
Integer s = 0
Start PRINT_LOOP:
s = s + 1
If(s - n == 0):
Jump to PRINT_TRAILING
Character c = get character from heap-address s
Print c as character
Go to next iteration of PRINT_LOOP
PRINT_TRAILING:
Character c = get character from heap-address 0
Print c as character
Print 't'
Go to next iteration of OUTER_LOOP
The program terminates with an error when it tries to read a character when none is given in TIO (or it hangs waiting for an input in some Whitespace compilers like vii5ard).
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 58 bytes
StringReplace[#,a:u~~w:u..~~b:u:>b<>w<>a/.{u->Except@#2}]&
Try it online!
-22 bytes from @attinat
-12 bytes from @M.Stern
$endgroup$
$begingroup$
70 bytes usingStringReplace
withStringExpression
s
$endgroup$
– attinat
May 17 at 7:25
1
$begingroup$
64 bytes usingStringTake
instead ofStringReplace
:StringRiffle[StringSplit@##~StringTake~{{-1},{2,-2},{1}},#2,""]&
$endgroup$
– Roman
May 18 at 17:29
2
$begingroup$
Here is a more direct approach:StringReplace[#, a : u ~~ w : u .. ~~ b : u :> b <> w <> a /. {u -> Except@#2}] &
$endgroup$
– M. Stern
May 19 at 7:44
1
$begingroup$
{ and } are optional :)
$endgroup$
– M. Stern
May 19 at 7:55
1
$begingroup$
55 bytes, also fixes 2-character words
$endgroup$
– attinat
May 19 at 23:59
add a comment |
$begingroup$
QuadR, 20 bytes
(w)(w*)(w)
321
Simply make three capturing groups consisting of 1, 0-or-more, and 1 word-characters, then reverses their order.
Try it online!
$endgroup$
add a comment |
$begingroup$
APL+WIN, 50 bytes
(∊¯1↑¨s),¨1↓¨(¯1↓¨s),¨↑¨s←((+s=' ')⊂s←' ',⎕)~¨' '
Prompts for string and uses space as the delimiter.
Try it online! Courtesy of Dyalog Classic
$endgroup$
add a comment |
$begingroup$
Japt -S
, 10 bytes
Convinced there has to be a shorter approach (and I was right) but this'll do for now.
¸ËhJDg)hDÌ
Try it
¸ËhJDg)hDÌ :Implicit input of string
¸ :Split on spaces
Ë :Map each D
h : Set the character at
J : Index -1 to
Dg : The first character in D
) : End set
h : Set the first character to
DÌ : The last character in D
:Implicit output, joined by spaces
$endgroup$
$begingroup$
Much shorter than my 12 byter:¸®Ì+Zs1J +Zg
$endgroup$
– Embodiment of Ignorance
May 17 at 3:07
$begingroup$
@EmbodimentofIgnorance, that's where I started, too, but it would have failed on single character words. You could save a byte on that, though, with¸®ÎiZÌ+Zs1J
.
$endgroup$
– Shaggy
May 17 at 8:01
1
$begingroup$
@EmbodimentofIgnorance Found a 7 byter
$endgroup$
– Oliver
May 17 at 17:21
add a comment |
$begingroup$
sed, 64 bytes
sed -E 's/b([[:alpha:]])([[:alpha:]]*)([[:alpha:]])b/321/g'
$endgroup$
$begingroup$
Sure, we could use.
instead of[[:alpha:]]
, but it would actually have to be[^ ]
, which reduces it to 43, but breaks on punctuation and such. Using[a-zA-Z]
brings it up to 55, by which point I'm just hankering after those sweet, sweet human readable entities...
$endgroup$
– Rich
May 18 at 1:08
2
$begingroup$
You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, separated by a delimiter, all of a uniform case.
In other words, you don't need to worry about punctuation "breaking" your code and can just safely go for[^ ]
;)
$endgroup$
– Value Ink
May 18 at 1:15
$begingroup$
@ValueInk Yeah, but then[^ ]
should be[^[:space:]]
which brings it to 67 chars.
$endgroup$
– Rich
May 20 at 19:17
$begingroup$
"a delimiter" means you can ensure that the delimiter is always a regular space. Who uses tabs in a sentence anyways??
$endgroup$
– Value Ink
May 20 at 21:24
$begingroup$
Okay. Seems like "code golf" is a game where you're meant to find ways to get a drone to drop the ball in the hole instead of actually doing the work. Thanks for the shitty welcome.
$endgroup$
– Rich
May 23 at 2:32
|
show 3 more comments
$begingroup$
sed, 34 bytes
And presumably the pattern idea will work with most RE tools (and I do know there are differences between standard RE and extended RE).
s,b(w)(w*)(w)b,321,g
Try it online!
$endgroup$
1
$begingroup$
Welcome to PPCG! Methinks that the greedy nature of regex matches means you can cut theb
from the match: Try it online!
$endgroup$
– Value Ink
May 20 at 21:29
$begingroup$
Agreed @ValueInk -- but I was being accurate with the match. Removing theb
will lead to 30 bytes.
$endgroup$
– PJF
May 21 at 14:45
$begingroup$
Use -E for advanced regex and you can use unescaped parentheses. Use.
for the swapped chars and you can lose another two chars. This brings yours down to 26 bytes; one of the smallest readable solutions.s,b(.)(w*)(.)b,321,g
$endgroup$
– Rich
May 24 at 0:01
1
$begingroup$
- nope, I'm wrong, you need thew
s at the ends.s,b(w)(w*)(w)b,321,g
28 chars.
$endgroup$
– Rich
May 24 at 0:13
$begingroup$
Nice work @rich, but as I said I know about standard and extended RE. I just chose to make it standard and readable. The anchors would be required which I neglected to mention in my reply to ValueInk.
$endgroup$
– PJF
May 24 at 13:28
|
show 1 more comment
$begingroup$
Ruby, 53 bytes
gets.split(" ").map{|z|print z[-1]+z[1..-2]+z[0]," "}
I tried it without regex. The output prints each word on a new line. If that's against the rules, let me know and I'll fix it.
Ungolfed:
gets.split(" ").map {|z|
print z[-1] + z[1..-2] + z[0], " "
}
$endgroup$
$begingroup$
Welcome to PPCG! Printing each word on a new line should be fine, but your old solution ofp
was no good because that added quotes to the output. You could always useputs
instead since that one auto-appends the newline and is shorter thanprint
! Also, if you callsplit
with no arguments it automatically splits on spaces.
$endgroup$
– Value Ink
May 23 at 4:06
add a comment |
$begingroup$
8088 Assembly, IBM PC DOS, 39 38 bytes
$ xxd pwas.com
00000000: d1ee ac8a c8fd 03f1 c604 244e 8bfe ac3c ..........$N...<
00000010: 2075 098a 2586 6402 8825 8bfe e2f0 b409 u..%.d..%......
00000020: ba82 00cd 21c3
Unassembled:
D1 EE SHR SI, 1 ; point SI to DOS PSP (080H)
AC LODSB ; load string length into AL
8A C8 MOV CL, AL ; load string length into CX for loop
FD STD ; set LODSB to decrement
03 F1 ADD SI, CX ; point SI to end of string
C6 04 24 MOV BYTE PTR[SI], '$' ; put a '$' DOS string terminator at end
4E DEC SI ; start at last char of word
8B FE MOV DI, SI ; point DI to last char of word
CHR_LOOP:
AC LODSB ; load next (previous?) char into AL
3C 20 CMP AL, ' ' ; is it a space?
75 0A JNE END_CHR ; if so, continue loop
8A 25 MOV AH, [DI] ; put last char in AH
86 64 02 XCHG AH, [SI][2] ; swap memory contents of first char with last
; (unfortunately XCHG cannot swap mem to mem)
88 25 MOV [DI], AH ; put first char value into last char position
8B FE MOV DI, SI ; point DI last char of word
END_CHR:
E2 EF LOOP CHR_LOOP ; continue loop
B4 09 MOV AH, 9 ; DOS display string function
BA 0082 MOV DX, 082H ; output string is at memory address 82H
CD 21 INT 21H ; display string to screen
C3 RET ; return to DOS
Standalone PC DOS executable. Input via command line args, output to screen.
Download and test PWAS.COM.
$endgroup$
add a comment |
$begingroup$
Perl 5 -p
, 24 bytes
s/(w)(w*)(w)/$3$2$1/g
Try it online!
$endgroup$
$begingroup$
You can get it to 21 by using the newline as separator: Try it online!
$endgroup$
– wastl
May 17 at 17:38
add a comment |
$begingroup$
Batch, 141 bytes
@set t=
@for %%w in (%*)do @call:c %%w
@echo%t%
@exit/b
:c
@set s=%1
@if not %s%==%s:~,1% set s=%s:~-1%%s:~1,-1%%s:~,1%
@set t=%t% %s%
Takes input as command-line parameters. String manipulation is dire in Batch at best, and having to special-case single-letter words doesn't help.
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 90 bytes
n=>n.Split().Any(x=>WriteLine(x.Length<2?x:x.Last()+x.Substring(1,x.Length-2)+x[0])is int)
Uses newline as delimiter, though really any whitespace can be used.
Try it online!
$endgroup$
$begingroup$
SelectMany (that is, map and flatten) for 84 bytes, but outputs a single trailing space. Try it online!
$endgroup$
– someone
May 17 at 8:17
add a comment |
$begingroup$
Icon, 76 bytes
link segment
procedure f(s)
w:=!seglist(s,' ')&w[1]:=:w[-1]&writes(w)&x
end
Try it online!
$endgroup$
add a comment |
$begingroup$
Java, 110 109 bytes
-1 bytes by using a newline for a delimeter
s->{int l;for(var i:s.split("n"))System.out.println(i.charAt(l=i.length()-1)+i.substring(1,l)+i.charAt(0));}
TIO
$endgroup$
$begingroup$
Does this work for single-letter words?
$endgroup$
– Neil
May 17 at 0:19
$begingroup$
@Neil no because I'm bad. I'll fix later.
$endgroup$
– Benjamin Urquhart
May 17 at 0:20
$begingroup$
109 by using newline as delimiter
$endgroup$
– Embodiment of Ignorance
May 17 at 4:55
add a comment |
$begingroup$
Haskell, 75 74 bytes
Fixed a bug pointed at by Cubic and also golfed down 1 byte.
f=unwords.map(#v).words
x#g=g(r$tail x)++[x!!0]
r=reverse
v=
v x=r$x#r
Try it online!
$endgroup$
$begingroup$
map g
is shorter than(g<$>)
$endgroup$
– Cubic
May 17 at 10:17
1
$begingroup$
Also, if you look at your test case you'll see it doesn't work for one letter words, it turnsa
intoaa
$endgroup$
– Cubic
May 17 at 10:20
add a comment |
$begingroup$
Scala, 100 bytes
(b:String,c:String)=>b.split(c)map(f=>f.tail.lastOption++:(f.drop(1).dropRight(1)+f.head))mkString c
$endgroup$
add a comment |
$begingroup$
T-SQL, 126 bytes
SELECT STRING_AGG(STUFF(STUFF(value,1,1,RIGHT(value,1)),LEN(value),1,LEFT(value,1)),' ')
FROM STRING_SPLIT((SELECT*FROM t),' ')
Input is via a pre-existing table t with varchar field v, per our IO standards.
Reading from back to front, STRING_SPLIT
breaks a string into individual rows via a delimiter, STUFF
modifies the characters at the specified positions, then STRING_AGG
mashes them back together again.
$endgroup$
add a comment |
1 2
next
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f185674%2fpwas-eht-tirsf-dna-tasl-setterl-fo-hace-dorw%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
43 Answers
43
active
oldest
votes
43 Answers
43
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 2
next
$begingroup$
TeX, 216 bytes (4 lines, 54 characters each)
Because it's not about the byte count, it's about the quality of the typeset output :-)
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
Try it Online! (Overleaf; not sure how it works)
Full test file:
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
S{swap the a first and last letters of each word}
pwas eht a tirsf dna tasl setterl fo hace dorw
S{SWAP THE A FIRST AND LAST LETTERS OF EACH WORD}
bye
Output:
For LaTeX you just need the boilerplate:
documentclass{article}
begin{document}
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
S{swap the a first and last letters of each word}
pwas eht a tirsf dna tasl setterl fo hace dorw
S{SWAP THE A FIRST AND LAST LETTERS OF EACH WORD}
end{document}
Explanation
TeX is a strange beast. Reading normal code and understanding it is a feat by itself. Understanding obfuscated TeX code goes a few steps further. I'll try to make this understandable for people who don't know TeX as well, so before we start here's a few concepts about TeX to make things easier to follow:
For (not so) absolute TeX beginners
First, and most important item in this list: the code does not have to be in rectangle shape, even though pop culture might lead you to think so.
TeX is a macro expansion language. You can, as an example, define
defsayhello#1{Hello, #1!}
and then writesayhello{Code Golfists}
to get TeX to printHello, Code Golfists!
. This is called an “undelimited macro”, and to feed it the first (and only, in this case) parameter you enclose it in braces. TeX removes those braces when the macro grabs the argument. You can use up to 9 parameters:defsay#1#2{#1, #2!}
thensay{Good news}{everyone}
.The counterpart of undelimited macros are, unsurprisingly, delimited ones :) You could make the previous definition a tad more semantical:
defsay #1 to #2.{#1, #2!}
. In this case the parameters are followed by so-called parameter text. Such parameter text delimits the argument of the macro (#1
is delimited by␣to␣
, spaces included, and#2
is delimited by.
). After that definition you can writesay Good news to everyone.
, which will expand toGood news, everyone!
. Nice, isn't it? :) However a delimited argument is (quoting the TeXbook) “the shortest (possibly empty) sequence of tokens with properly nested{...}
groups that is followed in the input by this particular list of non-parameter tokens”. This means that the expansion ofsay Let's go to the mall to Martin
will produce a weird sentence. In this case you'd need to “hide” the first␣to␣
with{...}
:say {Let's go to the mall} to Martin
.
So far so good. Now things start to get weird. When TeX reads a character (which is defined by a “character code”), it assigns that character a “category code” (catcode, for friends :) which defines what that character will mean. This combination of character and category code makes a token (more on that here, for example). The ones which are of interest for us here are basically:
catcode 11, which define tokens which can make up a control sequence (a posh name for a macro). By default all letters [a-zA-Z] are catcode 11, so I can write
hello
, which is one single control sequence, whilehe11o
is the control sequencehe
followed by two characters1
, followed by the lettero
, because1
is not catcode 11. If I didcatcode`1=11
, from that point onhe11o
would be one control sequence. One important thing is that catcodes are set when TeX first sees the character at hand, and such catcode is frozen... FOREVER! (terms and conditions may apply)catcode 12, which are most of other characters, such as
0"!@*(?,.-+/
and so forth. They are the least special type of catcode as they serve only for writing stuff on the paper. But hey, who uses TeX for writing?!? (again, terms and conditions may apply)catcode 13, which is hell :) Really. Stop reading and go do something out of your life. You don't want to know what catcode 13 is. Ever heard of Friday, 13th? Guess where it got its name from! Continue at your own risk! A catcode 13 character, also called an “active” character, is not just a character anymore, it is a macro itself! You can define it to have parameters and expand to something like we saw above. After you do
catcode`e=13
you think you can dodef e{I am the letter e!}
, BUT. YOU. CANNOT!e
is not a letter anymore, sodef
is not thedef
you know, it isd e f
! Oh, choose another letter you say? Okay!catcode`R=13 def R{I am an ARRR!}
. Very well, Jimmy, try it! I dare you do that and write anR
in your code! That's what a catcode 13 is. I AM CALM! Let's move on.
Okay, now to grouping. This is fairly straightforward. Whatever assignments (
def
is an assignment operation,let
(we'll get into it) is another) done in a group are restored to what they were before that group started unless that assignment is global. There are several ways to start groups, one of them is with catcode 1 and 2 characters (oh, catcodes again). By default{
is catcode 1, or begin-group, and}
is catcode 2, or end-group. An example:defa{1} a{defa{2} a} a
This prints1 2 1
. Outside the groupa
was1
, then inside it was redefined to2
, and when the group ended, it was restored to1
.The
let
operation is another assignment operation likedef
, but rather different. Withdef
you define macros which will expand to stuff, withlet
you create copies of already existing things. Afterletblub=def
(the=
is optional) you can change the start of thee
example from the catcode 13 item above toblub e{...
and have fun with that one. Or better, instead of breaking stuff you can fix (would you look at that!) theR
example:letnewr=R catcode`R=13 def R{I am an Anewrnewrnewr!}
. Quick question: could you rename tonewR
?Finally, the so-called “spurious spaces”. This is kind of a taboo topic because there are people who claim that reputation earned in the TeX - LaTeX Stack Exchange by answering “spurious spaces” questions should not be considered, while others wholeheartedly disagree. Whom do you agree with? Place your bets! Meanwhile: TeX understands a line break as a space. Try to write several words with a line break (not an empty line) between them. Now add a
%
at the end of these lines. It's like you were “commenting out” these end-of-line spaces. That's it :)
(Sort of) ungolfing the code
Let's make that rectangle into something (arguably) easier to follow:
{
let~catcode
~`A13
defA#1{~`#113gdef}
AGG#1{~`#113globallet}
GFFelse
GHHfi
AQQ{Q}
AII{ifxQ}
AEE#1#2#3|{I#3#2#1FE{#1#2}#3|H}
ADD#1#2#3|{I#2FE{#1}#2#3|H}
ACC#1#2|{D{}#2Q|#1 }
ABBH#1 {HI#1FC#1|BH}
gdefS#1{iftrueBH#1 Q }
}
Explanation of each step
each line contains one single instruction. Let's go one by one, dissecting them:
{
First we start a group to keep some changes (namely catcode changes) local so that they don't mess up the input text.
let~catcode
Basically all TeX obfuscation codes start with this instruction. By default, both in plain TeX and LaTeX, the ~
character is the one active character which can be made into a macro for further use. And the best tool for weirdifying TeX code are catcode changes, so this is generally the best choice. Now instead of catcode`A=13
we can write ~`A13
(the =
is optional):
~`A13
Now the letter A
is an active character, and we can define it to do something:
defA#1{~`#113gdef}
A
is now a macro that takes one argument (which should be another character). First the catcode of the argument is changed to 13 to make it active: ~`#113
(replace the ~
by catcode
and add an =
and you have: catcode`#1=13
). Finally it leaves a gdef
(global def
) in the input stream. In short, A
makes another character active and start its definition. Let's try it:
AGG#1{~`#113globallet}
AG
first “activates” G
and does gdef
, which followed by the next G
starts the definition. The definition of G
is very similar to that of A
, except that instead of gdef
it does a globallet
(there isn't a glet
like the gdef
). In short, G
activates a character and makes it be something else. Let's make shortcuts for two commands we'll use later:
GFFelse
GHHfi
Now instead of else
and fi
we can simply use F
and H
. Much shorter :)
AQQ{Q}
Now we use A
again to define another macro, Q
. The above statement basically does (in a less obfuscated language) defQ{Q}
. This isn't a terribly interesting definition, but it has an interesting feature. Unless you do want to break some code, the only macro that expands to Q
is Q
itself, so it acts like a unique marker (it's called a quark). You can use the ifx
conditional to test if the argument of a macro is such quark with ifx Q#1
:
AII{ifxQ}
so you can be pretty sure that you found such a marker. Notice that in this definition I removed the space between ifx
and Q
. Usually this would lead to an error (note that the syntax highlight thinks that ifxQ
is one thing), but since now Q
is catcode 13 it cannot form a control sequence. Be careful, however, not to expand this quark or you'll get stuck in an infinite loop because Q
expands to Q
which expands to Q
which...
Now that the preliminaries are done, we can go to the proper algorithm to pwas eht setterl. Due to TeX's tokenization the algorithm has to be written backwards. This is because at the time you do a definition TeX will tokenize (assign catcodes) to the characters in the definition using the current settings so, for example, if I do:
defone{E}
catcode`E=13def E{1}
one E
the output is E1
, whereas if I change the order of the definitions:
catcode`E=13def E{1}
defone{E}
one E
the output is 11
. This is because in the first example the E
in the definition was tokenized as a letter (catcode 11) before the catcode change, so it will always be a letter E
. In the second example, however, E
was first made active, and only then one
was defined, and now the definition contains the catcode 13 E
which expands to 1
.
I will, however, overlook this fact and reorder the definitions to have a logical (but not working) order. In the following paragraphs you can assume that the letters B
, C
, D
, and E
are active.
gdefS#1{iftrueBH#1 Q }
(notice there was a small bug in the previous version, it did not contain the final space in the definition above. I only noticed it while writing this. Read on and you'll see why we need that one to properly terminate the macro.)
First we define the user-level macro, S
. This one shouldn't be an active character to have a friendly (?) syntax, so the macro for gwappins eht setterl is S
. The macro starts with an always-true conditional iftrue
(it will soon be clear why), and then calls the B
macro followed by H
(which we defined earlier to be fi
) to match the iftrue
. Then we leave the argument of the macro #1
followed by a space and by the quark Q
. Suppose we use S{hello world}
, then the input stream should look like this: iftrue BHhello world Q␣
(I replaced the last space by a ␣
so that the rendering of the site does not eat it, like I did in the previous version of the code). iftrue
is true, so it expands and we are left with BHhello world Q␣
. TeX does not remove the fi
(H
) after the conditional is evaluated, instead it leaves it there until the fi
is actually expanded. Now the B
macro is expanded:
ABBH#1 {HI#1FC#1|BH}
B
is a delimited macro whose parameter text is H#1␣
, so the argument is whatever is between H
and a space. Continuing the example above the input stream prior to the expansion of B
is BHhello world Q␣
. B
is followed by H
, as it should (otherwise TeX would raise an error), then the next space is between hello
and world
, so #1
is the word hello
. And here we got to split the input text at the spaces. Yay :D The expansion of B
removes everything up to the first space from the input stream and replaces by HI#1FC#1|BH
with #1
being hello
: HIhelloFChello|BHworld Q␣
. Notice that there is a new BH
later in the input stream, to do a tail recursion of B
and process later words. After this word is processed B
processes the next word until the word-to-be-processed is the quark Q
. The last space after Q
is needed because the delimited macro B
requires one at the end of the argument. With the previous version (see edit history) the code would misbehave if you used S{hello world}abc abc
(the space between the abc
s would vanish).
OK, back to the input stream: HIhelloFChello|BHworld Q␣
. First there's the H
(fi
) that completes the initial iftrue
. Now we have this (pseudocoded):
I
hello
F
Chello|B
H
world Q␣
The I...F...H
think is actually a ifx Q...else...fi
structure. The ifx
test checks if the (first token of the) word grabbed is the Q
quark. If it is there is nothing else to do and the execution terminates, otherwise what remains is: Chello|BHworld Q␣
. Now C
is expanded:
ACC#1#2|{D#2Q|#1 }
The first argument of C
is undelimited, so unless braced it will be a single token, The second argument is delimited by |
, so after the expansion of C
(with #1=h
and #2=ello
) the input stream is: DelloQ|h BHworld Q␣
. Notice that another |
is put there, and the h
of hello
is put after that. Half the swapping is done; the first letter is at the end. In TeX it is easy to grab the first token of a token list. A simple macro deffirst#1#2|{#1}
gets the first letter when you use first hello|
. The last one is a problem because TeX always grabs the “smallest, possibly empty” token list as argument, so we need a few work-arounds. Next item in the token list is D
:
ADD#1#2|{I#1FE{}#1#2|H}
This D
macro is one of the work-arounds and it's useful in the sole case where the word has a single letter. Suppose instead of hello
we had x
. In this case the input stream would be DQ|x
, then D
would expand (with #1=Q
, and #2
empty) to: IQFE{}Q|Hx
. This is similar to the I...F...H
(ifx Q...else...fi
) block in B
, which will see that the argument is the quark and will interrupt the execution leaving only x
for typesetting. In other cases (returning to the hello
example), D
would expand (with #1=e
and #2=lloQ
) to: IeFE{}elloQ|Hh BHworld Q␣
. Again, the I...F...H
will check for Q
but will fail and take the else
branch: E{}elloQ|Hh BHworld Q␣
. Now the last piece of this thing, the E
macro would expand:
AEE#1#2#3|{I#3#2#1FE{#1#2}#3|H}
The parameter text here is quite similar to C
and D
; the first and second arguments are undelimited, and the last one is delimited by |
. The input stream looks like this: E{}elloQ|Hh BHworld Q␣
, then E
expands (with #1
empty, #2=e
, and #3=lloQ
): IlloQeFE{e}lloQ|HHh BHworld Q␣
. Another I...F...H
block checks for the quark (which sees l
and returns false
): E{e}lloQ|HHh BHworld Q␣
. Now E
expands again (with #1=e
empty, #2=l
, and #3=loQ
): IloQleFE{el}loQ|HHHh BHworld Q␣
. And again I...F...H
. The macro does a few more iterations until the Q
is finally found and the true
branch is taken: E{el}loQ|HHHh BHworld Q␣
-> IoQlelFE{ell}oQ|HHHHh BHworld Q␣
-> E{ell}oQ|HHHHh BHworld Q␣
-> IQoellFE{ello}Q|HHHHHh BHworld Q␣
. Now the quark is found and the conditional expands to: oellHHHHh BHworld Q␣
. Phew.
Oh, wait, what are these? NORMAL LETTERS? Oh, boy! The letters are finally found and TeX writes down oell
, then a bunch of H
(fi
) are found and expanded (to nothing) leaving the input stream with: oellh BHworld Q␣
. Now the first word has the first and last letters swapped and what TeX finds next is the other B
to repeat the whole process for the next word.
}
Finally we end the group started back there so that all local assignments are undone. The local assignments are the catcode changes of the letters A
, B
, C
, ... which were made macros so that they return to their normal letter meaning and can be safely used in the text. And that's it. Now the S
macro defined back there will trigger the processing of the text as above.
One interesting thing about this code is that it is fully expandable. That is, you can safely use it in moving arguments without worrying that it will explode. You can even use the code to check if the last letter of a word is the same as the second (for whatever reason you would need that) in an if
test:
ifS{here} trueelse falsefi % prints true (plus junk, which you would need to handle)
ifS{test} trueelse falsefi % prints false
Sorry for the (probably far too) wordy explanation. I tried to make it as clear as possible for non TeXies as well :)
Summary for the impatient
The macro S
prepends the input with an active character B
which grabs lists of tokens delimited by a final space and passes them to C
. C
takes the first token in that list and moves it to the end of the token list and expands D
with what remains. D
checks if “what remains” is empty, in which case a single-letter word was found, then do nothing; otherwise expands E
. E
loops through the token list until it finds the last letter in the word, when it is found it leaves that last letter, followed by the middle of the word, which is then followed by the first letter left at the end of the token stream by C
.
$endgroup$
2
$begingroup$
I would love a full explanation of this one. I'm very curious as to how it works!
$endgroup$
– LambdaBeta
May 18 at 19:55
1
$begingroup$
@LambdaBeta I can do it, but not right now. Hold on and I'll ping you when I do it :)
$endgroup$
– Phelype Oleinik
May 18 at 20:38
1
$begingroup$
@LambdaBeta Done! Sorry, I'm too wordy sometimes :-)
$endgroup$
– Phelype Oleinik
May 20 at 20:25
add a comment |
$begingroup$
TeX, 216 bytes (4 lines, 54 characters each)
Because it's not about the byte count, it's about the quality of the typeset output :-)
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
Try it Online! (Overleaf; not sure how it works)
Full test file:
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
S{swap the a first and last letters of each word}
pwas eht a tirsf dna tasl setterl fo hace dorw
S{SWAP THE A FIRST AND LAST LETTERS OF EACH WORD}
bye
Output:
For LaTeX you just need the boilerplate:
documentclass{article}
begin{document}
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
S{swap the a first and last letters of each word}
pwas eht a tirsf dna tasl setterl fo hace dorw
S{SWAP THE A FIRST AND LAST LETTERS OF EACH WORD}
end{document}
Explanation
TeX is a strange beast. Reading normal code and understanding it is a feat by itself. Understanding obfuscated TeX code goes a few steps further. I'll try to make this understandable for people who don't know TeX as well, so before we start here's a few concepts about TeX to make things easier to follow:
For (not so) absolute TeX beginners
First, and most important item in this list: the code does not have to be in rectangle shape, even though pop culture might lead you to think so.
TeX is a macro expansion language. You can, as an example, define
defsayhello#1{Hello, #1!}
and then writesayhello{Code Golfists}
to get TeX to printHello, Code Golfists!
. This is called an “undelimited macro”, and to feed it the first (and only, in this case) parameter you enclose it in braces. TeX removes those braces when the macro grabs the argument. You can use up to 9 parameters:defsay#1#2{#1, #2!}
thensay{Good news}{everyone}
.The counterpart of undelimited macros are, unsurprisingly, delimited ones :) You could make the previous definition a tad more semantical:
defsay #1 to #2.{#1, #2!}
. In this case the parameters are followed by so-called parameter text. Such parameter text delimits the argument of the macro (#1
is delimited by␣to␣
, spaces included, and#2
is delimited by.
). After that definition you can writesay Good news to everyone.
, which will expand toGood news, everyone!
. Nice, isn't it? :) However a delimited argument is (quoting the TeXbook) “the shortest (possibly empty) sequence of tokens with properly nested{...}
groups that is followed in the input by this particular list of non-parameter tokens”. This means that the expansion ofsay Let's go to the mall to Martin
will produce a weird sentence. In this case you'd need to “hide” the first␣to␣
with{...}
:say {Let's go to the mall} to Martin
.
So far so good. Now things start to get weird. When TeX reads a character (which is defined by a “character code”), it assigns that character a “category code” (catcode, for friends :) which defines what that character will mean. This combination of character and category code makes a token (more on that here, for example). The ones which are of interest for us here are basically:
catcode 11, which define tokens which can make up a control sequence (a posh name for a macro). By default all letters [a-zA-Z] are catcode 11, so I can write
hello
, which is one single control sequence, whilehe11o
is the control sequencehe
followed by two characters1
, followed by the lettero
, because1
is not catcode 11. If I didcatcode`1=11
, from that point onhe11o
would be one control sequence. One important thing is that catcodes are set when TeX first sees the character at hand, and such catcode is frozen... FOREVER! (terms and conditions may apply)catcode 12, which are most of other characters, such as
0"!@*(?,.-+/
and so forth. They are the least special type of catcode as they serve only for writing stuff on the paper. But hey, who uses TeX for writing?!? (again, terms and conditions may apply)catcode 13, which is hell :) Really. Stop reading and go do something out of your life. You don't want to know what catcode 13 is. Ever heard of Friday, 13th? Guess where it got its name from! Continue at your own risk! A catcode 13 character, also called an “active” character, is not just a character anymore, it is a macro itself! You can define it to have parameters and expand to something like we saw above. After you do
catcode`e=13
you think you can dodef e{I am the letter e!}
, BUT. YOU. CANNOT!e
is not a letter anymore, sodef
is not thedef
you know, it isd e f
! Oh, choose another letter you say? Okay!catcode`R=13 def R{I am an ARRR!}
. Very well, Jimmy, try it! I dare you do that and write anR
in your code! That's what a catcode 13 is. I AM CALM! Let's move on.
Okay, now to grouping. This is fairly straightforward. Whatever assignments (
def
is an assignment operation,let
(we'll get into it) is another) done in a group are restored to what they were before that group started unless that assignment is global. There are several ways to start groups, one of them is with catcode 1 and 2 characters (oh, catcodes again). By default{
is catcode 1, or begin-group, and}
is catcode 2, or end-group. An example:defa{1} a{defa{2} a} a
This prints1 2 1
. Outside the groupa
was1
, then inside it was redefined to2
, and when the group ended, it was restored to1
.The
let
operation is another assignment operation likedef
, but rather different. Withdef
you define macros which will expand to stuff, withlet
you create copies of already existing things. Afterletblub=def
(the=
is optional) you can change the start of thee
example from the catcode 13 item above toblub e{...
and have fun with that one. Or better, instead of breaking stuff you can fix (would you look at that!) theR
example:letnewr=R catcode`R=13 def R{I am an Anewrnewrnewr!}
. Quick question: could you rename tonewR
?Finally, the so-called “spurious spaces”. This is kind of a taboo topic because there are people who claim that reputation earned in the TeX - LaTeX Stack Exchange by answering “spurious spaces” questions should not be considered, while others wholeheartedly disagree. Whom do you agree with? Place your bets! Meanwhile: TeX understands a line break as a space. Try to write several words with a line break (not an empty line) between them. Now add a
%
at the end of these lines. It's like you were “commenting out” these end-of-line spaces. That's it :)
(Sort of) ungolfing the code
Let's make that rectangle into something (arguably) easier to follow:
{
let~catcode
~`A13
defA#1{~`#113gdef}
AGG#1{~`#113globallet}
GFFelse
GHHfi
AQQ{Q}
AII{ifxQ}
AEE#1#2#3|{I#3#2#1FE{#1#2}#3|H}
ADD#1#2#3|{I#2FE{#1}#2#3|H}
ACC#1#2|{D{}#2Q|#1 }
ABBH#1 {HI#1FC#1|BH}
gdefS#1{iftrueBH#1 Q }
}
Explanation of each step
each line contains one single instruction. Let's go one by one, dissecting them:
{
First we start a group to keep some changes (namely catcode changes) local so that they don't mess up the input text.
let~catcode
Basically all TeX obfuscation codes start with this instruction. By default, both in plain TeX and LaTeX, the ~
character is the one active character which can be made into a macro for further use. And the best tool for weirdifying TeX code are catcode changes, so this is generally the best choice. Now instead of catcode`A=13
we can write ~`A13
(the =
is optional):
~`A13
Now the letter A
is an active character, and we can define it to do something:
defA#1{~`#113gdef}
A
is now a macro that takes one argument (which should be another character). First the catcode of the argument is changed to 13 to make it active: ~`#113
(replace the ~
by catcode
and add an =
and you have: catcode`#1=13
). Finally it leaves a gdef
(global def
) in the input stream. In short, A
makes another character active and start its definition. Let's try it:
AGG#1{~`#113globallet}
AG
first “activates” G
and does gdef
, which followed by the next G
starts the definition. The definition of G
is very similar to that of A
, except that instead of gdef
it does a globallet
(there isn't a glet
like the gdef
). In short, G
activates a character and makes it be something else. Let's make shortcuts for two commands we'll use later:
GFFelse
GHHfi
Now instead of else
and fi
we can simply use F
and H
. Much shorter :)
AQQ{Q}
Now we use A
again to define another macro, Q
. The above statement basically does (in a less obfuscated language) defQ{Q}
. This isn't a terribly interesting definition, but it has an interesting feature. Unless you do want to break some code, the only macro that expands to Q
is Q
itself, so it acts like a unique marker (it's called a quark). You can use the ifx
conditional to test if the argument of a macro is such quark with ifx Q#1
:
AII{ifxQ}
so you can be pretty sure that you found such a marker. Notice that in this definition I removed the space between ifx
and Q
. Usually this would lead to an error (note that the syntax highlight thinks that ifxQ
is one thing), but since now Q
is catcode 13 it cannot form a control sequence. Be careful, however, not to expand this quark or you'll get stuck in an infinite loop because Q
expands to Q
which expands to Q
which...
Now that the preliminaries are done, we can go to the proper algorithm to pwas eht setterl. Due to TeX's tokenization the algorithm has to be written backwards. This is because at the time you do a definition TeX will tokenize (assign catcodes) to the characters in the definition using the current settings so, for example, if I do:
defone{E}
catcode`E=13def E{1}
one E
the output is E1
, whereas if I change the order of the definitions:
catcode`E=13def E{1}
defone{E}
one E
the output is 11
. This is because in the first example the E
in the definition was tokenized as a letter (catcode 11) before the catcode change, so it will always be a letter E
. In the second example, however, E
was first made active, and only then one
was defined, and now the definition contains the catcode 13 E
which expands to 1
.
I will, however, overlook this fact and reorder the definitions to have a logical (but not working) order. In the following paragraphs you can assume that the letters B
, C
, D
, and E
are active.
gdefS#1{iftrueBH#1 Q }
(notice there was a small bug in the previous version, it did not contain the final space in the definition above. I only noticed it while writing this. Read on and you'll see why we need that one to properly terminate the macro.)
First we define the user-level macro, S
. This one shouldn't be an active character to have a friendly (?) syntax, so the macro for gwappins eht setterl is S
. The macro starts with an always-true conditional iftrue
(it will soon be clear why), and then calls the B
macro followed by H
(which we defined earlier to be fi
) to match the iftrue
. Then we leave the argument of the macro #1
followed by a space and by the quark Q
. Suppose we use S{hello world}
, then the input stream should look like this: iftrue BHhello world Q␣
(I replaced the last space by a ␣
so that the rendering of the site does not eat it, like I did in the previous version of the code). iftrue
is true, so it expands and we are left with BHhello world Q␣
. TeX does not remove the fi
(H
) after the conditional is evaluated, instead it leaves it there until the fi
is actually expanded. Now the B
macro is expanded:
ABBH#1 {HI#1FC#1|BH}
B
is a delimited macro whose parameter text is H#1␣
, so the argument is whatever is between H
and a space. Continuing the example above the input stream prior to the expansion of B
is BHhello world Q␣
. B
is followed by H
, as it should (otherwise TeX would raise an error), then the next space is between hello
and world
, so #1
is the word hello
. And here we got to split the input text at the spaces. Yay :D The expansion of B
removes everything up to the first space from the input stream and replaces by HI#1FC#1|BH
with #1
being hello
: HIhelloFChello|BHworld Q␣
. Notice that there is a new BH
later in the input stream, to do a tail recursion of B
and process later words. After this word is processed B
processes the next word until the word-to-be-processed is the quark Q
. The last space after Q
is needed because the delimited macro B
requires one at the end of the argument. With the previous version (see edit history) the code would misbehave if you used S{hello world}abc abc
(the space between the abc
s would vanish).
OK, back to the input stream: HIhelloFChello|BHworld Q␣
. First there's the H
(fi
) that completes the initial iftrue
. Now we have this (pseudocoded):
I
hello
F
Chello|B
H
world Q␣
The I...F...H
think is actually a ifx Q...else...fi
structure. The ifx
test checks if the (first token of the) word grabbed is the Q
quark. If it is there is nothing else to do and the execution terminates, otherwise what remains is: Chello|BHworld Q␣
. Now C
is expanded:
ACC#1#2|{D#2Q|#1 }
The first argument of C
is undelimited, so unless braced it will be a single token, The second argument is delimited by |
, so after the expansion of C
(with #1=h
and #2=ello
) the input stream is: DelloQ|h BHworld Q␣
. Notice that another |
is put there, and the h
of hello
is put after that. Half the swapping is done; the first letter is at the end. In TeX it is easy to grab the first token of a token list. A simple macro deffirst#1#2|{#1}
gets the first letter when you use first hello|
. The last one is a problem because TeX always grabs the “smallest, possibly empty” token list as argument, so we need a few work-arounds. Next item in the token list is D
:
ADD#1#2|{I#1FE{}#1#2|H}
This D
macro is one of the work-arounds and it's useful in the sole case where the word has a single letter. Suppose instead of hello
we had x
. In this case the input stream would be DQ|x
, then D
would expand (with #1=Q
, and #2
empty) to: IQFE{}Q|Hx
. This is similar to the I...F...H
(ifx Q...else...fi
) block in B
, which will see that the argument is the quark and will interrupt the execution leaving only x
for typesetting. In other cases (returning to the hello
example), D
would expand (with #1=e
and #2=lloQ
) to: IeFE{}elloQ|Hh BHworld Q␣
. Again, the I...F...H
will check for Q
but will fail and take the else
branch: E{}elloQ|Hh BHworld Q␣
. Now the last piece of this thing, the E
macro would expand:
AEE#1#2#3|{I#3#2#1FE{#1#2}#3|H}
The parameter text here is quite similar to C
and D
; the first and second arguments are undelimited, and the last one is delimited by |
. The input stream looks like this: E{}elloQ|Hh BHworld Q␣
, then E
expands (with #1
empty, #2=e
, and #3=lloQ
): IlloQeFE{e}lloQ|HHh BHworld Q␣
. Another I...F...H
block checks for the quark (which sees l
and returns false
): E{e}lloQ|HHh BHworld Q␣
. Now E
expands again (with #1=e
empty, #2=l
, and #3=loQ
): IloQleFE{el}loQ|HHHh BHworld Q␣
. And again I...F...H
. The macro does a few more iterations until the Q
is finally found and the true
branch is taken: E{el}loQ|HHHh BHworld Q␣
-> IoQlelFE{ell}oQ|HHHHh BHworld Q␣
-> E{ell}oQ|HHHHh BHworld Q␣
-> IQoellFE{ello}Q|HHHHHh BHworld Q␣
. Now the quark is found and the conditional expands to: oellHHHHh BHworld Q␣
. Phew.
Oh, wait, what are these? NORMAL LETTERS? Oh, boy! The letters are finally found and TeX writes down oell
, then a bunch of H
(fi
) are found and expanded (to nothing) leaving the input stream with: oellh BHworld Q␣
. Now the first word has the first and last letters swapped and what TeX finds next is the other B
to repeat the whole process for the next word.
}
Finally we end the group started back there so that all local assignments are undone. The local assignments are the catcode changes of the letters A
, B
, C
, ... which were made macros so that they return to their normal letter meaning and can be safely used in the text. And that's it. Now the S
macro defined back there will trigger the processing of the text as above.
One interesting thing about this code is that it is fully expandable. That is, you can safely use it in moving arguments without worrying that it will explode. You can even use the code to check if the last letter of a word is the same as the second (for whatever reason you would need that) in an if
test:
ifS{here} trueelse falsefi % prints true (plus junk, which you would need to handle)
ifS{test} trueelse falsefi % prints false
Sorry for the (probably far too) wordy explanation. I tried to make it as clear as possible for non TeXies as well :)
Summary for the impatient
The macro S
prepends the input with an active character B
which grabs lists of tokens delimited by a final space and passes them to C
. C
takes the first token in that list and moves it to the end of the token list and expands D
with what remains. D
checks if “what remains” is empty, in which case a single-letter word was found, then do nothing; otherwise expands E
. E
loops through the token list until it finds the last letter in the word, when it is found it leaves that last letter, followed by the middle of the word, which is then followed by the first letter left at the end of the token stream by C
.
$endgroup$
2
$begingroup$
I would love a full explanation of this one. I'm very curious as to how it works!
$endgroup$
– LambdaBeta
May 18 at 19:55
1
$begingroup$
@LambdaBeta I can do it, but not right now. Hold on and I'll ping you when I do it :)
$endgroup$
– Phelype Oleinik
May 18 at 20:38
1
$begingroup$
@LambdaBeta Done! Sorry, I'm too wordy sometimes :-)
$endgroup$
– Phelype Oleinik
May 20 at 20:25
add a comment |
$begingroup$
TeX, 216 bytes (4 lines, 54 characters each)
Because it's not about the byte count, it's about the quality of the typeset output :-)
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
Try it Online! (Overleaf; not sure how it works)
Full test file:
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
S{swap the a first and last letters of each word}
pwas eht a tirsf dna tasl setterl fo hace dorw
S{SWAP THE A FIRST AND LAST LETTERS OF EACH WORD}
bye
Output:
For LaTeX you just need the boilerplate:
documentclass{article}
begin{document}
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
S{swap the a first and last letters of each word}
pwas eht a tirsf dna tasl setterl fo hace dorw
S{SWAP THE A FIRST AND LAST LETTERS OF EACH WORD}
end{document}
Explanation
TeX is a strange beast. Reading normal code and understanding it is a feat by itself. Understanding obfuscated TeX code goes a few steps further. I'll try to make this understandable for people who don't know TeX as well, so before we start here's a few concepts about TeX to make things easier to follow:
For (not so) absolute TeX beginners
First, and most important item in this list: the code does not have to be in rectangle shape, even though pop culture might lead you to think so.
TeX is a macro expansion language. You can, as an example, define
defsayhello#1{Hello, #1!}
and then writesayhello{Code Golfists}
to get TeX to printHello, Code Golfists!
. This is called an “undelimited macro”, and to feed it the first (and only, in this case) parameter you enclose it in braces. TeX removes those braces when the macro grabs the argument. You can use up to 9 parameters:defsay#1#2{#1, #2!}
thensay{Good news}{everyone}
.The counterpart of undelimited macros are, unsurprisingly, delimited ones :) You could make the previous definition a tad more semantical:
defsay #1 to #2.{#1, #2!}
. In this case the parameters are followed by so-called parameter text. Such parameter text delimits the argument of the macro (#1
is delimited by␣to␣
, spaces included, and#2
is delimited by.
). After that definition you can writesay Good news to everyone.
, which will expand toGood news, everyone!
. Nice, isn't it? :) However a delimited argument is (quoting the TeXbook) “the shortest (possibly empty) sequence of tokens with properly nested{...}
groups that is followed in the input by this particular list of non-parameter tokens”. This means that the expansion ofsay Let's go to the mall to Martin
will produce a weird sentence. In this case you'd need to “hide” the first␣to␣
with{...}
:say {Let's go to the mall} to Martin
.
So far so good. Now things start to get weird. When TeX reads a character (which is defined by a “character code”), it assigns that character a “category code” (catcode, for friends :) which defines what that character will mean. This combination of character and category code makes a token (more on that here, for example). The ones which are of interest for us here are basically:
catcode 11, which define tokens which can make up a control sequence (a posh name for a macro). By default all letters [a-zA-Z] are catcode 11, so I can write
hello
, which is one single control sequence, whilehe11o
is the control sequencehe
followed by two characters1
, followed by the lettero
, because1
is not catcode 11. If I didcatcode`1=11
, from that point onhe11o
would be one control sequence. One important thing is that catcodes are set when TeX first sees the character at hand, and such catcode is frozen... FOREVER! (terms and conditions may apply)catcode 12, which are most of other characters, such as
0"!@*(?,.-+/
and so forth. They are the least special type of catcode as they serve only for writing stuff on the paper. But hey, who uses TeX for writing?!? (again, terms and conditions may apply)catcode 13, which is hell :) Really. Stop reading and go do something out of your life. You don't want to know what catcode 13 is. Ever heard of Friday, 13th? Guess where it got its name from! Continue at your own risk! A catcode 13 character, also called an “active” character, is not just a character anymore, it is a macro itself! You can define it to have parameters and expand to something like we saw above. After you do
catcode`e=13
you think you can dodef e{I am the letter e!}
, BUT. YOU. CANNOT!e
is not a letter anymore, sodef
is not thedef
you know, it isd e f
! Oh, choose another letter you say? Okay!catcode`R=13 def R{I am an ARRR!}
. Very well, Jimmy, try it! I dare you do that and write anR
in your code! That's what a catcode 13 is. I AM CALM! Let's move on.
Okay, now to grouping. This is fairly straightforward. Whatever assignments (
def
is an assignment operation,let
(we'll get into it) is another) done in a group are restored to what they were before that group started unless that assignment is global. There are several ways to start groups, one of them is with catcode 1 and 2 characters (oh, catcodes again). By default{
is catcode 1, or begin-group, and}
is catcode 2, or end-group. An example:defa{1} a{defa{2} a} a
This prints1 2 1
. Outside the groupa
was1
, then inside it was redefined to2
, and when the group ended, it was restored to1
.The
let
operation is another assignment operation likedef
, but rather different. Withdef
you define macros which will expand to stuff, withlet
you create copies of already existing things. Afterletblub=def
(the=
is optional) you can change the start of thee
example from the catcode 13 item above toblub e{...
and have fun with that one. Or better, instead of breaking stuff you can fix (would you look at that!) theR
example:letnewr=R catcode`R=13 def R{I am an Anewrnewrnewr!}
. Quick question: could you rename tonewR
?Finally, the so-called “spurious spaces”. This is kind of a taboo topic because there are people who claim that reputation earned in the TeX - LaTeX Stack Exchange by answering “spurious spaces” questions should not be considered, while others wholeheartedly disagree. Whom do you agree with? Place your bets! Meanwhile: TeX understands a line break as a space. Try to write several words with a line break (not an empty line) between them. Now add a
%
at the end of these lines. It's like you were “commenting out” these end-of-line spaces. That's it :)
(Sort of) ungolfing the code
Let's make that rectangle into something (arguably) easier to follow:
{
let~catcode
~`A13
defA#1{~`#113gdef}
AGG#1{~`#113globallet}
GFFelse
GHHfi
AQQ{Q}
AII{ifxQ}
AEE#1#2#3|{I#3#2#1FE{#1#2}#3|H}
ADD#1#2#3|{I#2FE{#1}#2#3|H}
ACC#1#2|{D{}#2Q|#1 }
ABBH#1 {HI#1FC#1|BH}
gdefS#1{iftrueBH#1 Q }
}
Explanation of each step
each line contains one single instruction. Let's go one by one, dissecting them:
{
First we start a group to keep some changes (namely catcode changes) local so that they don't mess up the input text.
let~catcode
Basically all TeX obfuscation codes start with this instruction. By default, both in plain TeX and LaTeX, the ~
character is the one active character which can be made into a macro for further use. And the best tool for weirdifying TeX code are catcode changes, so this is generally the best choice. Now instead of catcode`A=13
we can write ~`A13
(the =
is optional):
~`A13
Now the letter A
is an active character, and we can define it to do something:
defA#1{~`#113gdef}
A
is now a macro that takes one argument (which should be another character). First the catcode of the argument is changed to 13 to make it active: ~`#113
(replace the ~
by catcode
and add an =
and you have: catcode`#1=13
). Finally it leaves a gdef
(global def
) in the input stream. In short, A
makes another character active and start its definition. Let's try it:
AGG#1{~`#113globallet}
AG
first “activates” G
and does gdef
, which followed by the next G
starts the definition. The definition of G
is very similar to that of A
, except that instead of gdef
it does a globallet
(there isn't a glet
like the gdef
). In short, G
activates a character and makes it be something else. Let's make shortcuts for two commands we'll use later:
GFFelse
GHHfi
Now instead of else
and fi
we can simply use F
and H
. Much shorter :)
AQQ{Q}
Now we use A
again to define another macro, Q
. The above statement basically does (in a less obfuscated language) defQ{Q}
. This isn't a terribly interesting definition, but it has an interesting feature. Unless you do want to break some code, the only macro that expands to Q
is Q
itself, so it acts like a unique marker (it's called a quark). You can use the ifx
conditional to test if the argument of a macro is such quark with ifx Q#1
:
AII{ifxQ}
so you can be pretty sure that you found such a marker. Notice that in this definition I removed the space between ifx
and Q
. Usually this would lead to an error (note that the syntax highlight thinks that ifxQ
is one thing), but since now Q
is catcode 13 it cannot form a control sequence. Be careful, however, not to expand this quark or you'll get stuck in an infinite loop because Q
expands to Q
which expands to Q
which...
Now that the preliminaries are done, we can go to the proper algorithm to pwas eht setterl. Due to TeX's tokenization the algorithm has to be written backwards. This is because at the time you do a definition TeX will tokenize (assign catcodes) to the characters in the definition using the current settings so, for example, if I do:
defone{E}
catcode`E=13def E{1}
one E
the output is E1
, whereas if I change the order of the definitions:
catcode`E=13def E{1}
defone{E}
one E
the output is 11
. This is because in the first example the E
in the definition was tokenized as a letter (catcode 11) before the catcode change, so it will always be a letter E
. In the second example, however, E
was first made active, and only then one
was defined, and now the definition contains the catcode 13 E
which expands to 1
.
I will, however, overlook this fact and reorder the definitions to have a logical (but not working) order. In the following paragraphs you can assume that the letters B
, C
, D
, and E
are active.
gdefS#1{iftrueBH#1 Q }
(notice there was a small bug in the previous version, it did not contain the final space in the definition above. I only noticed it while writing this. Read on and you'll see why we need that one to properly terminate the macro.)
First we define the user-level macro, S
. This one shouldn't be an active character to have a friendly (?) syntax, so the macro for gwappins eht setterl is S
. The macro starts with an always-true conditional iftrue
(it will soon be clear why), and then calls the B
macro followed by H
(which we defined earlier to be fi
) to match the iftrue
. Then we leave the argument of the macro #1
followed by a space and by the quark Q
. Suppose we use S{hello world}
, then the input stream should look like this: iftrue BHhello world Q␣
(I replaced the last space by a ␣
so that the rendering of the site does not eat it, like I did in the previous version of the code). iftrue
is true, so it expands and we are left with BHhello world Q␣
. TeX does not remove the fi
(H
) after the conditional is evaluated, instead it leaves it there until the fi
is actually expanded. Now the B
macro is expanded:
ABBH#1 {HI#1FC#1|BH}
B
is a delimited macro whose parameter text is H#1␣
, so the argument is whatever is between H
and a space. Continuing the example above the input stream prior to the expansion of B
is BHhello world Q␣
. B
is followed by H
, as it should (otherwise TeX would raise an error), then the next space is between hello
and world
, so #1
is the word hello
. And here we got to split the input text at the spaces. Yay :D The expansion of B
removes everything up to the first space from the input stream and replaces by HI#1FC#1|BH
with #1
being hello
: HIhelloFChello|BHworld Q␣
. Notice that there is a new BH
later in the input stream, to do a tail recursion of B
and process later words. After this word is processed B
processes the next word until the word-to-be-processed is the quark Q
. The last space after Q
is needed because the delimited macro B
requires one at the end of the argument. With the previous version (see edit history) the code would misbehave if you used S{hello world}abc abc
(the space between the abc
s would vanish).
OK, back to the input stream: HIhelloFChello|BHworld Q␣
. First there's the H
(fi
) that completes the initial iftrue
. Now we have this (pseudocoded):
I
hello
F
Chello|B
H
world Q␣
The I...F...H
think is actually a ifx Q...else...fi
structure. The ifx
test checks if the (first token of the) word grabbed is the Q
quark. If it is there is nothing else to do and the execution terminates, otherwise what remains is: Chello|BHworld Q␣
. Now C
is expanded:
ACC#1#2|{D#2Q|#1 }
The first argument of C
is undelimited, so unless braced it will be a single token, The second argument is delimited by |
, so after the expansion of C
(with #1=h
and #2=ello
) the input stream is: DelloQ|h BHworld Q␣
. Notice that another |
is put there, and the h
of hello
is put after that. Half the swapping is done; the first letter is at the end. In TeX it is easy to grab the first token of a token list. A simple macro deffirst#1#2|{#1}
gets the first letter when you use first hello|
. The last one is a problem because TeX always grabs the “smallest, possibly empty” token list as argument, so we need a few work-arounds. Next item in the token list is D
:
ADD#1#2|{I#1FE{}#1#2|H}
This D
macro is one of the work-arounds and it's useful in the sole case where the word has a single letter. Suppose instead of hello
we had x
. In this case the input stream would be DQ|x
, then D
would expand (with #1=Q
, and #2
empty) to: IQFE{}Q|Hx
. This is similar to the I...F...H
(ifx Q...else...fi
) block in B
, which will see that the argument is the quark and will interrupt the execution leaving only x
for typesetting. In other cases (returning to the hello
example), D
would expand (with #1=e
and #2=lloQ
) to: IeFE{}elloQ|Hh BHworld Q␣
. Again, the I...F...H
will check for Q
but will fail and take the else
branch: E{}elloQ|Hh BHworld Q␣
. Now the last piece of this thing, the E
macro would expand:
AEE#1#2#3|{I#3#2#1FE{#1#2}#3|H}
The parameter text here is quite similar to C
and D
; the first and second arguments are undelimited, and the last one is delimited by |
. The input stream looks like this: E{}elloQ|Hh BHworld Q␣
, then E
expands (with #1
empty, #2=e
, and #3=lloQ
): IlloQeFE{e}lloQ|HHh BHworld Q␣
. Another I...F...H
block checks for the quark (which sees l
and returns false
): E{e}lloQ|HHh BHworld Q␣
. Now E
expands again (with #1=e
empty, #2=l
, and #3=loQ
): IloQleFE{el}loQ|HHHh BHworld Q␣
. And again I...F...H
. The macro does a few more iterations until the Q
is finally found and the true
branch is taken: E{el}loQ|HHHh BHworld Q␣
-> IoQlelFE{ell}oQ|HHHHh BHworld Q␣
-> E{ell}oQ|HHHHh BHworld Q␣
-> IQoellFE{ello}Q|HHHHHh BHworld Q␣
. Now the quark is found and the conditional expands to: oellHHHHh BHworld Q␣
. Phew.
Oh, wait, what are these? NORMAL LETTERS? Oh, boy! The letters are finally found and TeX writes down oell
, then a bunch of H
(fi
) are found and expanded (to nothing) leaving the input stream with: oellh BHworld Q␣
. Now the first word has the first and last letters swapped and what TeX finds next is the other B
to repeat the whole process for the next word.
}
Finally we end the group started back there so that all local assignments are undone. The local assignments are the catcode changes of the letters A
, B
, C
, ... which were made macros so that they return to their normal letter meaning and can be safely used in the text. And that's it. Now the S
macro defined back there will trigger the processing of the text as above.
One interesting thing about this code is that it is fully expandable. That is, you can safely use it in moving arguments without worrying that it will explode. You can even use the code to check if the last letter of a word is the same as the second (for whatever reason you would need that) in an if
test:
ifS{here} trueelse falsefi % prints true (plus junk, which you would need to handle)
ifS{test} trueelse falsefi % prints false
Sorry for the (probably far too) wordy explanation. I tried to make it as clear as possible for non TeXies as well :)
Summary for the impatient
The macro S
prepends the input with an active character B
which grabs lists of tokens delimited by a final space and passes them to C
. C
takes the first token in that list and moves it to the end of the token list and expands D
with what remains. D
checks if “what remains” is empty, in which case a single-letter word was found, then do nothing; otherwise expands E
. E
loops through the token list until it finds the last letter in the word, when it is found it leaves that last letter, followed by the middle of the word, which is then followed by the first letter left at the end of the token stream by C
.
$endgroup$
TeX, 216 bytes (4 lines, 54 characters each)
Because it's not about the byte count, it's about the quality of the typeset output :-)
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
Try it Online! (Overleaf; not sure how it works)
Full test file:
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
S{swap the a first and last letters of each word}
pwas eht a tirsf dna tasl setterl fo hace dorw
S{SWAP THE A FIRST AND LAST LETTERS OF EACH WORD}
bye
Output:
For LaTeX you just need the boilerplate:
documentclass{article}
begin{document}
{let~catcode~`A13 defA#1{~`#113gdef}AGG#1{~`#1 13%
globallet}GFFelseGHHfiAQQ{Q}AII{ifxQ}AEE#1#2#3|{%
I#3#2#1FE{#1#2}#3|H}ADD#1#2|{I#1FE{}#1#2|H}ACC#1#2|{D%
#2Q|#1 }ABBH#1 {HI#1FC#1|BH}gdefS#1{iftrueBH#1 Q }}
S{swap the a first and last letters of each word}
pwas eht a tirsf dna tasl setterl fo hace dorw
S{SWAP THE A FIRST AND LAST LETTERS OF EACH WORD}
end{document}
Explanation
TeX is a strange beast. Reading normal code and understanding it is a feat by itself. Understanding obfuscated TeX code goes a few steps further. I'll try to make this understandable for people who don't know TeX as well, so before we start here's a few concepts about TeX to make things easier to follow:
For (not so) absolute TeX beginners
First, and most important item in this list: the code does not have to be in rectangle shape, even though pop culture might lead you to think so.
TeX is a macro expansion language. You can, as an example, define
defsayhello#1{Hello, #1!}
and then writesayhello{Code Golfists}
to get TeX to printHello, Code Golfists!
. This is called an “undelimited macro”, and to feed it the first (and only, in this case) parameter you enclose it in braces. TeX removes those braces when the macro grabs the argument. You can use up to 9 parameters:defsay#1#2{#1, #2!}
thensay{Good news}{everyone}
.The counterpart of undelimited macros are, unsurprisingly, delimited ones :) You could make the previous definition a tad more semantical:
defsay #1 to #2.{#1, #2!}
. In this case the parameters are followed by so-called parameter text. Such parameter text delimits the argument of the macro (#1
is delimited by␣to␣
, spaces included, and#2
is delimited by.
). After that definition you can writesay Good news to everyone.
, which will expand toGood news, everyone!
. Nice, isn't it? :) However a delimited argument is (quoting the TeXbook) “the shortest (possibly empty) sequence of tokens with properly nested{...}
groups that is followed in the input by this particular list of non-parameter tokens”. This means that the expansion ofsay Let's go to the mall to Martin
will produce a weird sentence. In this case you'd need to “hide” the first␣to␣
with{...}
:say {Let's go to the mall} to Martin
.
So far so good. Now things start to get weird. When TeX reads a character (which is defined by a “character code”), it assigns that character a “category code” (catcode, for friends :) which defines what that character will mean. This combination of character and category code makes a token (more on that here, for example). The ones which are of interest for us here are basically:
catcode 11, which define tokens which can make up a control sequence (a posh name for a macro). By default all letters [a-zA-Z] are catcode 11, so I can write
hello
, which is one single control sequence, whilehe11o
is the control sequencehe
followed by two characters1
, followed by the lettero
, because1
is not catcode 11. If I didcatcode`1=11
, from that point onhe11o
would be one control sequence. One important thing is that catcodes are set when TeX first sees the character at hand, and such catcode is frozen... FOREVER! (terms and conditions may apply)catcode 12, which are most of other characters, such as
0"!@*(?,.-+/
and so forth. They are the least special type of catcode as they serve only for writing stuff on the paper. But hey, who uses TeX for writing?!? (again, terms and conditions may apply)catcode 13, which is hell :) Really. Stop reading and go do something out of your life. You don't want to know what catcode 13 is. Ever heard of Friday, 13th? Guess where it got its name from! Continue at your own risk! A catcode 13 character, also called an “active” character, is not just a character anymore, it is a macro itself! You can define it to have parameters and expand to something like we saw above. After you do
catcode`e=13
you think you can dodef e{I am the letter e!}
, BUT. YOU. CANNOT!e
is not a letter anymore, sodef
is not thedef
you know, it isd e f
! Oh, choose another letter you say? Okay!catcode`R=13 def R{I am an ARRR!}
. Very well, Jimmy, try it! I dare you do that and write anR
in your code! That's what a catcode 13 is. I AM CALM! Let's move on.
Okay, now to grouping. This is fairly straightforward. Whatever assignments (
def
is an assignment operation,let
(we'll get into it) is another) done in a group are restored to what they were before that group started unless that assignment is global. There are several ways to start groups, one of them is with catcode 1 and 2 characters (oh, catcodes again). By default{
is catcode 1, or begin-group, and}
is catcode 2, or end-group. An example:defa{1} a{defa{2} a} a
This prints1 2 1
. Outside the groupa
was1
, then inside it was redefined to2
, and when the group ended, it was restored to1
.The
let
operation is another assignment operation likedef
, but rather different. Withdef
you define macros which will expand to stuff, withlet
you create copies of already existing things. Afterletblub=def
(the=
is optional) you can change the start of thee
example from the catcode 13 item above toblub e{...
and have fun with that one. Or better, instead of breaking stuff you can fix (would you look at that!) theR
example:letnewr=R catcode`R=13 def R{I am an Anewrnewrnewr!}
. Quick question: could you rename tonewR
?Finally, the so-called “spurious spaces”. This is kind of a taboo topic because there are people who claim that reputation earned in the TeX - LaTeX Stack Exchange by answering “spurious spaces” questions should not be considered, while others wholeheartedly disagree. Whom do you agree with? Place your bets! Meanwhile: TeX understands a line break as a space. Try to write several words with a line break (not an empty line) between them. Now add a
%
at the end of these lines. It's like you were “commenting out” these end-of-line spaces. That's it :)
(Sort of) ungolfing the code
Let's make that rectangle into something (arguably) easier to follow:
{
let~catcode
~`A13
defA#1{~`#113gdef}
AGG#1{~`#113globallet}
GFFelse
GHHfi
AQQ{Q}
AII{ifxQ}
AEE#1#2#3|{I#3#2#1FE{#1#2}#3|H}
ADD#1#2#3|{I#2FE{#1}#2#3|H}
ACC#1#2|{D{}#2Q|#1 }
ABBH#1 {HI#1FC#1|BH}
gdefS#1{iftrueBH#1 Q }
}
Explanation of each step
each line contains one single instruction. Let's go one by one, dissecting them:
{
First we start a group to keep some changes (namely catcode changes) local so that they don't mess up the input text.
let~catcode
Basically all TeX obfuscation codes start with this instruction. By default, both in plain TeX and LaTeX, the ~
character is the one active character which can be made into a macro for further use. And the best tool for weirdifying TeX code are catcode changes, so this is generally the best choice. Now instead of catcode`A=13
we can write ~`A13
(the =
is optional):
~`A13
Now the letter A
is an active character, and we can define it to do something:
defA#1{~`#113gdef}
A
is now a macro that takes one argument (which should be another character). First the catcode of the argument is changed to 13 to make it active: ~`#113
(replace the ~
by catcode
and add an =
and you have: catcode`#1=13
). Finally it leaves a gdef
(global def
) in the input stream. In short, A
makes another character active and start its definition. Let's try it:
AGG#1{~`#113globallet}
AG
first “activates” G
and does gdef
, which followed by the next G
starts the definition. The definition of G
is very similar to that of A
, except that instead of gdef
it does a globallet
(there isn't a glet
like the gdef
). In short, G
activates a character and makes it be something else. Let's make shortcuts for two commands we'll use later:
GFFelse
GHHfi
Now instead of else
and fi
we can simply use F
and H
. Much shorter :)
AQQ{Q}
Now we use A
again to define another macro, Q
. The above statement basically does (in a less obfuscated language) defQ{Q}
. This isn't a terribly interesting definition, but it has an interesting feature. Unless you do want to break some code, the only macro that expands to Q
is Q
itself, so it acts like a unique marker (it's called a quark). You can use the ifx
conditional to test if the argument of a macro is such quark with ifx Q#1
:
AII{ifxQ}
so you can be pretty sure that you found such a marker. Notice that in this definition I removed the space between ifx
and Q
. Usually this would lead to an error (note that the syntax highlight thinks that ifxQ
is one thing), but since now Q
is catcode 13 it cannot form a control sequence. Be careful, however, not to expand this quark or you'll get stuck in an infinite loop because Q
expands to Q
which expands to Q
which...
Now that the preliminaries are done, we can go to the proper algorithm to pwas eht setterl. Due to TeX's tokenization the algorithm has to be written backwards. This is because at the time you do a definition TeX will tokenize (assign catcodes) to the characters in the definition using the current settings so, for example, if I do:
defone{E}
catcode`E=13def E{1}
one E
the output is E1
, whereas if I change the order of the definitions:
catcode`E=13def E{1}
defone{E}
one E
the output is 11
. This is because in the first example the E
in the definition was tokenized as a letter (catcode 11) before the catcode change, so it will always be a letter E
. In the second example, however, E
was first made active, and only then one
was defined, and now the definition contains the catcode 13 E
which expands to 1
.
I will, however, overlook this fact and reorder the definitions to have a logical (but not working) order. In the following paragraphs you can assume that the letters B
, C
, D
, and E
are active.
gdefS#1{iftrueBH#1 Q }
(notice there was a small bug in the previous version, it did not contain the final space in the definition above. I only noticed it while writing this. Read on and you'll see why we need that one to properly terminate the macro.)
First we define the user-level macro, S
. This one shouldn't be an active character to have a friendly (?) syntax, so the macro for gwappins eht setterl is S
. The macro starts with an always-true conditional iftrue
(it will soon be clear why), and then calls the B
macro followed by H
(which we defined earlier to be fi
) to match the iftrue
. Then we leave the argument of the macro #1
followed by a space and by the quark Q
. Suppose we use S{hello world}
, then the input stream should look like this: iftrue BHhello world Q␣
(I replaced the last space by a ␣
so that the rendering of the site does not eat it, like I did in the previous version of the code). iftrue
is true, so it expands and we are left with BHhello world Q␣
. TeX does not remove the fi
(H
) after the conditional is evaluated, instead it leaves it there until the fi
is actually expanded. Now the B
macro is expanded:
ABBH#1 {HI#1FC#1|BH}
B
is a delimited macro whose parameter text is H#1␣
, so the argument is whatever is between H
and a space. Continuing the example above the input stream prior to the expansion of B
is BHhello world Q␣
. B
is followed by H
, as it should (otherwise TeX would raise an error), then the next space is between hello
and world
, so #1
is the word hello
. And here we got to split the input text at the spaces. Yay :D The expansion of B
removes everything up to the first space from the input stream and replaces by HI#1FC#1|BH
with #1
being hello
: HIhelloFChello|BHworld Q␣
. Notice that there is a new BH
later in the input stream, to do a tail recursion of B
and process later words. After this word is processed B
processes the next word until the word-to-be-processed is the quark Q
. The last space after Q
is needed because the delimited macro B
requires one at the end of the argument. With the previous version (see edit history) the code would misbehave if you used S{hello world}abc abc
(the space between the abc
s would vanish).
OK, back to the input stream: HIhelloFChello|BHworld Q␣
. First there's the H
(fi
) that completes the initial iftrue
. Now we have this (pseudocoded):
I
hello
F
Chello|B
H
world Q␣
The I...F...H
think is actually a ifx Q...else...fi
structure. The ifx
test checks if the (first token of the) word grabbed is the Q
quark. If it is there is nothing else to do and the execution terminates, otherwise what remains is: Chello|BHworld Q␣
. Now C
is expanded:
ACC#1#2|{D#2Q|#1 }
The first argument of C
is undelimited, so unless braced it will be a single token, The second argument is delimited by |
, so after the expansion of C
(with #1=h
and #2=ello
) the input stream is: DelloQ|h BHworld Q␣
. Notice that another |
is put there, and the h
of hello
is put after that. Half the swapping is done; the first letter is at the end. In TeX it is easy to grab the first token of a token list. A simple macro deffirst#1#2|{#1}
gets the first letter when you use first hello|
. The last one is a problem because TeX always grabs the “smallest, possibly empty” token list as argument, so we need a few work-arounds. Next item in the token list is D
:
ADD#1#2|{I#1FE{}#1#2|H}
This D
macro is one of the work-arounds and it's useful in the sole case where the word has a single letter. Suppose instead of hello
we had x
. In this case the input stream would be DQ|x
, then D
would expand (with #1=Q
, and #2
empty) to: IQFE{}Q|Hx
. This is similar to the I...F...H
(ifx Q...else...fi
) block in B
, which will see that the argument is the quark and will interrupt the execution leaving only x
for typesetting. In other cases (returning to the hello
example), D
would expand (with #1=e
and #2=lloQ
) to: IeFE{}elloQ|Hh BHworld Q␣
. Again, the I...F...H
will check for Q
but will fail and take the else
branch: E{}elloQ|Hh BHworld Q␣
. Now the last piece of this thing, the E
macro would expand:
AEE#1#2#3|{I#3#2#1FE{#1#2}#3|H}
The parameter text here is quite similar to C
and D
; the first and second arguments are undelimited, and the last one is delimited by |
. The input stream looks like this: E{}elloQ|Hh BHworld Q␣
, then E
expands (with #1
empty, #2=e
, and #3=lloQ
): IlloQeFE{e}lloQ|HHh BHworld Q␣
. Another I...F...H
block checks for the quark (which sees l
and returns false
): E{e}lloQ|HHh BHworld Q␣
. Now E
expands again (with #1=e
empty, #2=l
, and #3=loQ
): IloQleFE{el}loQ|HHHh BHworld Q␣
. And again I...F...H
. The macro does a few more iterations until the Q
is finally found and the true
branch is taken: E{el}loQ|HHHh BHworld Q␣
-> IoQlelFE{ell}oQ|HHHHh BHworld Q␣
-> E{ell}oQ|HHHHh BHworld Q␣
-> IQoellFE{ello}Q|HHHHHh BHworld Q␣
. Now the quark is found and the conditional expands to: oellHHHHh BHworld Q␣
. Phew.
Oh, wait, what are these? NORMAL LETTERS? Oh, boy! The letters are finally found and TeX writes down oell
, then a bunch of H
(fi
) are found and expanded (to nothing) leaving the input stream with: oellh BHworld Q␣
. Now the first word has the first and last letters swapped and what TeX finds next is the other B
to repeat the whole process for the next word.
}
Finally we end the group started back there so that all local assignments are undone. The local assignments are the catcode changes of the letters A
, B
, C
, ... which were made macros so that they return to their normal letter meaning and can be safely used in the text. And that's it. Now the S
macro defined back there will trigger the processing of the text as above.
One interesting thing about this code is that it is fully expandable. That is, you can safely use it in moving arguments without worrying that it will explode. You can even use the code to check if the last letter of a word is the same as the second (for whatever reason you would need that) in an if
test:
ifS{here} trueelse falsefi % prints true (plus junk, which you would need to handle)
ifS{test} trueelse falsefi % prints false
Sorry for the (probably far too) wordy explanation. I tried to make it as clear as possible for non TeXies as well :)
Summary for the impatient
The macro S
prepends the input with an active character B
which grabs lists of tokens delimited by a final space and passes them to C
. C
takes the first token in that list and moves it to the end of the token list and expands D
with what remains. D
checks if “what remains” is empty, in which case a single-letter word was found, then do nothing; otherwise expands E
. E
loops through the token list until it finds the last letter in the word, when it is found it leaves that last letter, followed by the middle of the word, which is then followed by the first letter left at the end of the token stream by C
.
edited May 20 at 20:24
answered May 16 at 20:24
Phelype OleinikPhelype Oleinik
895318
895318
2
$begingroup$
I would love a full explanation of this one. I'm very curious as to how it works!
$endgroup$
– LambdaBeta
May 18 at 19:55
1
$begingroup$
@LambdaBeta I can do it, but not right now. Hold on and I'll ping you when I do it :)
$endgroup$
– Phelype Oleinik
May 18 at 20:38
1
$begingroup$
@LambdaBeta Done! Sorry, I'm too wordy sometimes :-)
$endgroup$
– Phelype Oleinik
May 20 at 20:25
add a comment |
2
$begingroup$
I would love a full explanation of this one. I'm very curious as to how it works!
$endgroup$
– LambdaBeta
May 18 at 19:55
1
$begingroup$
@LambdaBeta I can do it, but not right now. Hold on and I'll ping you when I do it :)
$endgroup$
– Phelype Oleinik
May 18 at 20:38
1
$begingroup$
@LambdaBeta Done! Sorry, I'm too wordy sometimes :-)
$endgroup$
– Phelype Oleinik
May 20 at 20:25
2
2
$begingroup$
I would love a full explanation of this one. I'm very curious as to how it works!
$endgroup$
– LambdaBeta
May 18 at 19:55
$begingroup$
I would love a full explanation of this one. I'm very curious as to how it works!
$endgroup$
– LambdaBeta
May 18 at 19:55
1
1
$begingroup$
@LambdaBeta I can do it, but not right now. Hold on and I'll ping you when I do it :)
$endgroup$
– Phelype Oleinik
May 18 at 20:38
$begingroup$
@LambdaBeta I can do it, but not right now. Hold on and I'll ping you when I do it :)
$endgroup$
– Phelype Oleinik
May 18 at 20:38
1
1
$begingroup$
@LambdaBeta Done! Sorry, I'm too wordy sometimes :-)
$endgroup$
– Phelype Oleinik
May 20 at 20:25
$begingroup$
@LambdaBeta Done! Sorry, I'm too wordy sometimes :-)
$endgroup$
– Phelype Oleinik
May 20 at 20:25
add a comment |
$begingroup$
JavaScript (ES6), 39 36 bytes
Saved 3 bytes thanks to @FryAmTheEggman
Uses a linefeed as separator.
s=>s.replace(/(.)(.*)(.)/g,'$3$2$1')
Try it online!
$endgroup$
5
$begingroup$
(.)(.*)(.)
is that the Total Recall emoticon?
$endgroup$
– MikeTheLiar
May 17 at 15:34
1
$begingroup$
@MikeTheLiar Kind of, I guess. :D
$endgroup$
– Arnauld
May 17 at 16:12
$begingroup$
The assignment specifies the string contains the separator.
$endgroup$
– Cees Timmerman
May 18 at 21:12
$begingroup$
@CeesTimmerman I'm not sure what you mean. This code expects a linefeed as separator and therefore takes strings with linefeeds as input. (The footer of the TIO link converts spaces to linefeeds and then back to spaces for readability.)
$endgroup$
– Arnauld
May 18 at 21:29
$begingroup$
"given a string of alphabetical ASCII characters as well as one other character to use as a delimiter (to separate each word)" - Nm, i thought it was a separate parameter.
$endgroup$
– Cees Timmerman
May 18 at 21:32
add a comment |
$begingroup$
JavaScript (ES6), 39 36 bytes
Saved 3 bytes thanks to @FryAmTheEggman
Uses a linefeed as separator.
s=>s.replace(/(.)(.*)(.)/g,'$3$2$1')
Try it online!
$endgroup$
5
$begingroup$
(.)(.*)(.)
is that the Total Recall emoticon?
$endgroup$
– MikeTheLiar
May 17 at 15:34
1
$begingroup$
@MikeTheLiar Kind of, I guess. :D
$endgroup$
– Arnauld
May 17 at 16:12
$begingroup$
The assignment specifies the string contains the separator.
$endgroup$
– Cees Timmerman
May 18 at 21:12
$begingroup$
@CeesTimmerman I'm not sure what you mean. This code expects a linefeed as separator and therefore takes strings with linefeeds as input. (The footer of the TIO link converts spaces to linefeeds and then back to spaces for readability.)
$endgroup$
– Arnauld
May 18 at 21:29
$begingroup$
"given a string of alphabetical ASCII characters as well as one other character to use as a delimiter (to separate each word)" - Nm, i thought it was a separate parameter.
$endgroup$
– Cees Timmerman
May 18 at 21:32
add a comment |
$begingroup$
JavaScript (ES6), 39 36 bytes
Saved 3 bytes thanks to @FryAmTheEggman
Uses a linefeed as separator.
s=>s.replace(/(.)(.*)(.)/g,'$3$2$1')
Try it online!
$endgroup$
JavaScript (ES6), 39 36 bytes
Saved 3 bytes thanks to @FryAmTheEggman
Uses a linefeed as separator.
s=>s.replace(/(.)(.*)(.)/g,'$3$2$1')
Try it online!
edited May 16 at 20:44
answered May 16 at 19:41
ArnauldArnauld
86.1k7101352
86.1k7101352
5
$begingroup$
(.)(.*)(.)
is that the Total Recall emoticon?
$endgroup$
– MikeTheLiar
May 17 at 15:34
1
$begingroup$
@MikeTheLiar Kind of, I guess. :D
$endgroup$
– Arnauld
May 17 at 16:12
$begingroup$
The assignment specifies the string contains the separator.
$endgroup$
– Cees Timmerman
May 18 at 21:12
$begingroup$
@CeesTimmerman I'm not sure what you mean. This code expects a linefeed as separator and therefore takes strings with linefeeds as input. (The footer of the TIO link converts spaces to linefeeds and then back to spaces for readability.)
$endgroup$
– Arnauld
May 18 at 21:29
$begingroup$
"given a string of alphabetical ASCII characters as well as one other character to use as a delimiter (to separate each word)" - Nm, i thought it was a separate parameter.
$endgroup$
– Cees Timmerman
May 18 at 21:32
add a comment |
5
$begingroup$
(.)(.*)(.)
is that the Total Recall emoticon?
$endgroup$
– MikeTheLiar
May 17 at 15:34
1
$begingroup$
@MikeTheLiar Kind of, I guess. :D
$endgroup$
– Arnauld
May 17 at 16:12
$begingroup$
The assignment specifies the string contains the separator.
$endgroup$
– Cees Timmerman
May 18 at 21:12
$begingroup$
@CeesTimmerman I'm not sure what you mean. This code expects a linefeed as separator and therefore takes strings with linefeeds as input. (The footer of the TIO link converts spaces to linefeeds and then back to spaces for readability.)
$endgroup$
– Arnauld
May 18 at 21:29
$begingroup$
"given a string of alphabetical ASCII characters as well as one other character to use as a delimiter (to separate each word)" - Nm, i thought it was a separate parameter.
$endgroup$
– Cees Timmerman
May 18 at 21:32
5
5
$begingroup$
(.)(.*)(.)
is that the Total Recall emoticon?$endgroup$
– MikeTheLiar
May 17 at 15:34
$begingroup$
(.)(.*)(.)
is that the Total Recall emoticon?$endgroup$
– MikeTheLiar
May 17 at 15:34
1
1
$begingroup$
@MikeTheLiar Kind of, I guess. :D
$endgroup$
– Arnauld
May 17 at 16:12
$begingroup$
@MikeTheLiar Kind of, I guess. :D
$endgroup$
– Arnauld
May 17 at 16:12
$begingroup$
The assignment specifies the string contains the separator.
$endgroup$
– Cees Timmerman
May 18 at 21:12
$begingroup$
The assignment specifies the string contains the separator.
$endgroup$
– Cees Timmerman
May 18 at 21:12
$begingroup$
@CeesTimmerman I'm not sure what you mean. This code expects a linefeed as separator and therefore takes strings with linefeeds as input. (The footer of the TIO link converts spaces to linefeeds and then back to spaces for readability.)
$endgroup$
– Arnauld
May 18 at 21:29
$begingroup$
@CeesTimmerman I'm not sure what you mean. This code expects a linefeed as separator and therefore takes strings with linefeeds as input. (The footer of the TIO link converts spaces to linefeeds and then back to spaces for readability.)
$endgroup$
– Arnauld
May 18 at 21:29
$begingroup$
"given a string of alphabetical ASCII characters as well as one other character to use as a delimiter (to separate each word)" - Nm, i thought it was a separate parameter.
$endgroup$
– Cees Timmerman
May 18 at 21:32
$begingroup$
"given a string of alphabetical ASCII characters as well as one other character to use as a delimiter (to separate each word)" - Nm, i thought it was a separate parameter.
$endgroup$
– Cees Timmerman
May 18 at 21:32
add a comment |
$begingroup$
Retina, 8 5 bytes
,V,,`
Try it online!
Saved 3 bytes thanks to Kevin Cruijssen!
Uses a newline as the separator. We make use of Retina's reverse stage and some limits. The first limit is which matches to apply the reversal to, so we pick all of them with ,
. Then we want the first and last letter of each match to be swapped, so we take each letter in the range ,,
which translates to a range from the beginning to the end with step size zero.
$endgroup$
$begingroup$
Dangit, I was just searching through the docs for something like this to update my answer, but you beat me to it. I knew aboutV
, but didn't knew it could be used with the indices1,-2
like that. Nice one!
$endgroup$
– Kevin Cruijssen
May 16 at 20:23
1
$begingroup$
@KevinCruijssen I cheated a little and reviewed how limit ranges worked while this was in the sandbox :) I still feel like there should be a better way than inverting a range but I haven't been able to find anything shorter.
$endgroup$
– FryAmTheEggman
May 16 at 20:28
2
$begingroup$
You're indeed right that it can be shorter without a limit-range, because it seems this 5-byter works (given as example at the bottom of the Step Limits in the docs).
$endgroup$
– Kevin Cruijssen
May 16 at 20:41
$begingroup$
@KevinCruijssen Nice! Can't believe I missed that.
$endgroup$
– FryAmTheEggman
May 16 at 20:51
3
$begingroup$
So, 5 bytes and only 3 different characters? That's minimalist.
$endgroup$
– Cœur
May 17 at 5:37
add a comment |
$begingroup$
Retina, 8 5 bytes
,V,,`
Try it online!
Saved 3 bytes thanks to Kevin Cruijssen!
Uses a newline as the separator. We make use of Retina's reverse stage and some limits. The first limit is which matches to apply the reversal to, so we pick all of them with ,
. Then we want the first and last letter of each match to be swapped, so we take each letter in the range ,,
which translates to a range from the beginning to the end with step size zero.
$endgroup$
$begingroup$
Dangit, I was just searching through the docs for something like this to update my answer, but you beat me to it. I knew aboutV
, but didn't knew it could be used with the indices1,-2
like that. Nice one!
$endgroup$
– Kevin Cruijssen
May 16 at 20:23
1
$begingroup$
@KevinCruijssen I cheated a little and reviewed how limit ranges worked while this was in the sandbox :) I still feel like there should be a better way than inverting a range but I haven't been able to find anything shorter.
$endgroup$
– FryAmTheEggman
May 16 at 20:28
2
$begingroup$
You're indeed right that it can be shorter without a limit-range, because it seems this 5-byter works (given as example at the bottom of the Step Limits in the docs).
$endgroup$
– Kevin Cruijssen
May 16 at 20:41
$begingroup$
@KevinCruijssen Nice! Can't believe I missed that.
$endgroup$
– FryAmTheEggman
May 16 at 20:51
3
$begingroup$
So, 5 bytes and only 3 different characters? That's minimalist.
$endgroup$
– Cœur
May 17 at 5:37
add a comment |
$begingroup$
Retina, 8 5 bytes
,V,,`
Try it online!
Saved 3 bytes thanks to Kevin Cruijssen!
Uses a newline as the separator. We make use of Retina's reverse stage and some limits. The first limit is which matches to apply the reversal to, so we pick all of them with ,
. Then we want the first and last letter of each match to be swapped, so we take each letter in the range ,,
which translates to a range from the beginning to the end with step size zero.
$endgroup$
Retina, 8 5 bytes
,V,,`
Try it online!
Saved 3 bytes thanks to Kevin Cruijssen!
Uses a newline as the separator. We make use of Retina's reverse stage and some limits. The first limit is which matches to apply the reversal to, so we pick all of them with ,
. Then we want the first and last letter of each match to be swapped, so we take each letter in the range ,,
which translates to a range from the beginning to the end with step size zero.
edited May 16 at 20:49
answered May 16 at 20:18
FryAmTheEggmanFryAmTheEggman
15.1k32585
15.1k32585
$begingroup$
Dangit, I was just searching through the docs for something like this to update my answer, but you beat me to it. I knew aboutV
, but didn't knew it could be used with the indices1,-2
like that. Nice one!
$endgroup$
– Kevin Cruijssen
May 16 at 20:23
1
$begingroup$
@KevinCruijssen I cheated a little and reviewed how limit ranges worked while this was in the sandbox :) I still feel like there should be a better way than inverting a range but I haven't been able to find anything shorter.
$endgroup$
– FryAmTheEggman
May 16 at 20:28
2
$begingroup$
You're indeed right that it can be shorter without a limit-range, because it seems this 5-byter works (given as example at the bottom of the Step Limits in the docs).
$endgroup$
– Kevin Cruijssen
May 16 at 20:41
$begingroup$
@KevinCruijssen Nice! Can't believe I missed that.
$endgroup$
– FryAmTheEggman
May 16 at 20:51
3
$begingroup$
So, 5 bytes and only 3 different characters? That's minimalist.
$endgroup$
– Cœur
May 17 at 5:37
add a comment |
$begingroup$
Dangit, I was just searching through the docs for something like this to update my answer, but you beat me to it. I knew aboutV
, but didn't knew it could be used with the indices1,-2
like that. Nice one!
$endgroup$
– Kevin Cruijssen
May 16 at 20:23
1
$begingroup$
@KevinCruijssen I cheated a little and reviewed how limit ranges worked while this was in the sandbox :) I still feel like there should be a better way than inverting a range but I haven't been able to find anything shorter.
$endgroup$
– FryAmTheEggman
May 16 at 20:28
2
$begingroup$
You're indeed right that it can be shorter without a limit-range, because it seems this 5-byter works (given as example at the bottom of the Step Limits in the docs).
$endgroup$
– Kevin Cruijssen
May 16 at 20:41
$begingroup$
@KevinCruijssen Nice! Can't believe I missed that.
$endgroup$
– FryAmTheEggman
May 16 at 20:51
3
$begingroup$
So, 5 bytes and only 3 different characters? That's minimalist.
$endgroup$
– Cœur
May 17 at 5:37
$begingroup$
Dangit, I was just searching through the docs for something like this to update my answer, but you beat me to it. I knew about
V
, but didn't knew it could be used with the indices 1,-2
like that. Nice one!$endgroup$
– Kevin Cruijssen
May 16 at 20:23
$begingroup$
Dangit, I was just searching through the docs for something like this to update my answer, but you beat me to it. I knew about
V
, but didn't knew it could be used with the indices 1,-2
like that. Nice one!$endgroup$
– Kevin Cruijssen
May 16 at 20:23
1
1
$begingroup$
@KevinCruijssen I cheated a little and reviewed how limit ranges worked while this was in the sandbox :) I still feel like there should be a better way than inverting a range but I haven't been able to find anything shorter.
$endgroup$
– FryAmTheEggman
May 16 at 20:28
$begingroup$
@KevinCruijssen I cheated a little and reviewed how limit ranges worked while this was in the sandbox :) I still feel like there should be a better way than inverting a range but I haven't been able to find anything shorter.
$endgroup$
– FryAmTheEggman
May 16 at 20:28
2
2
$begingroup$
You're indeed right that it can be shorter without a limit-range, because it seems this 5-byter works (given as example at the bottom of the Step Limits in the docs).
$endgroup$
– Kevin Cruijssen
May 16 at 20:41
$begingroup$
You're indeed right that it can be shorter without a limit-range, because it seems this 5-byter works (given as example at the bottom of the Step Limits in the docs).
$endgroup$
– Kevin Cruijssen
May 16 at 20:41
$begingroup$
@KevinCruijssen Nice! Can't believe I missed that.
$endgroup$
– FryAmTheEggman
May 16 at 20:51
$begingroup$
@KevinCruijssen Nice! Can't believe I missed that.
$endgroup$
– FryAmTheEggman
May 16 at 20:51
3
3
$begingroup$
So, 5 bytes and only 3 different characters? That's minimalist.
$endgroup$
– Cœur
May 17 at 5:37
$begingroup$
So, 5 bytes and only 3 different characters? That's minimalist.
$endgroup$
– Cœur
May 17 at 5:37
add a comment |
$begingroup$
Pepe, 107 105 bytes
REEeREeeEeeeeerEEreREEEeREEEEEEeREEEErEEREEEEEEEreererEEEeererEEEerEEeERrEEEeerEEeerereeerEEEEeEEEReEeree
Try it online!
Explanation:
Notation on comments: command-explanation -> (stack) // explanation
REEe # input -> (R)
REeeEeeeee # push space to last -> (R) // this prevents an infinite loop
rEE # create loop labeled 0 and automatically push 0
re # pop 0 -> (r)
REEEe # go to last item -> (R)
REEEEEEe # ...then copy the char to other stack
REEEE # go to first item -> (R)
rEE # create loop labeled 32 // detect space
REEEEEEE # move item to other stack (R)
ree # do this while char != 32
re # pop 32 -> (r)
rEEEee # push item (dup to end) -> (r)
re # ...then pop -> (r)
rEEEe rEEeE # go to 2nd to last item -> (r)
RrEEEee # push the item (R flag: dup to first) -> (r)
rEEee # go to next -> (r) //
re # ...then pop -> (r)
reee rEEEEeEEE # out all as char then clear -> (r)
ReEe # out 32 as char -> (R)
ree # do this while stack != 0
$endgroup$
$begingroup$
How does this work?
$endgroup$
– lirtosiast
May 29 at 10:05
$begingroup$
@lirtosiast explanation added
$endgroup$
– u_ndefined
May 29 at 10:33
add a comment |
$begingroup$
Pepe, 107 105 bytes
REEeREeeEeeeeerEEreREEEeREEEEEEeREEEErEEREEEEEEEreererEEEeererEEEerEEeERrEEEeerEEeerereeerEEEEeEEEReEeree
Try it online!
Explanation:
Notation on comments: command-explanation -> (stack) // explanation
REEe # input -> (R)
REeeEeeeee # push space to last -> (R) // this prevents an infinite loop
rEE # create loop labeled 0 and automatically push 0
re # pop 0 -> (r)
REEEe # go to last item -> (R)
REEEEEEe # ...then copy the char to other stack
REEEE # go to first item -> (R)
rEE # create loop labeled 32 // detect space
REEEEEEE # move item to other stack (R)
ree # do this while char != 32
re # pop 32 -> (r)
rEEEee # push item (dup to end) -> (r)
re # ...then pop -> (r)
rEEEe rEEeE # go to 2nd to last item -> (r)
RrEEEee # push the item (R flag: dup to first) -> (r)
rEEee # go to next -> (r) //
re # ...then pop -> (r)
reee rEEEEeEEE # out all as char then clear -> (r)
ReEe # out 32 as char -> (R)
ree # do this while stack != 0
$endgroup$
$begingroup$
How does this work?
$endgroup$
– lirtosiast
May 29 at 10:05
$begingroup$
@lirtosiast explanation added
$endgroup$
– u_ndefined
May 29 at 10:33
add a comment |
$begingroup$
Pepe, 107 105 bytes
REEeREeeEeeeeerEEreREEEeREEEEEEeREEEErEEREEEEEEEreererEEEeererEEEerEEeERrEEEeerEEeerereeerEEEEeEEEReEeree
Try it online!
Explanation:
Notation on comments: command-explanation -> (stack) // explanation
REEe # input -> (R)
REeeEeeeee # push space to last -> (R) // this prevents an infinite loop
rEE # create loop labeled 0 and automatically push 0
re # pop 0 -> (r)
REEEe # go to last item -> (R)
REEEEEEe # ...then copy the char to other stack
REEEE # go to first item -> (R)
rEE # create loop labeled 32 // detect space
REEEEEEE # move item to other stack (R)
ree # do this while char != 32
re # pop 32 -> (r)
rEEEee # push item (dup to end) -> (r)
re # ...then pop -> (r)
rEEEe rEEeE # go to 2nd to last item -> (r)
RrEEEee # push the item (R flag: dup to first) -> (r)
rEEee # go to next -> (r) //
re # ...then pop -> (r)
reee rEEEEeEEE # out all as char then clear -> (r)
ReEe # out 32 as char -> (R)
ree # do this while stack != 0
$endgroup$
Pepe, 107 105 bytes
REEeREeeEeeeeerEEreREEEeREEEEEEeREEEErEEREEEEEEEreererEEEeererEEEerEEeERrEEEeerEEeerereeerEEEEeEEEReEeree
Try it online!
Explanation:
Notation on comments: command-explanation -> (stack) // explanation
REEe # input -> (R)
REeeEeeeee # push space to last -> (R) // this prevents an infinite loop
rEE # create loop labeled 0 and automatically push 0
re # pop 0 -> (r)
REEEe # go to last item -> (R)
REEEEEEe # ...then copy the char to other stack
REEEE # go to first item -> (R)
rEE # create loop labeled 32 // detect space
REEEEEEE # move item to other stack (R)
ree # do this while char != 32
re # pop 32 -> (r)
rEEEee # push item (dup to end) -> (r)
re # ...then pop -> (r)
rEEEe rEEeE # go to 2nd to last item -> (r)
RrEEEee # push the item (R flag: dup to first) -> (r)
rEEee # go to next -> (r) //
re # ...then pop -> (r)
reee rEEEEeEEE # out all as char then clear -> (r)
ReEe # out 32 as char -> (R)
ree # do this while stack != 0
edited May 29 at 10:38
answered May 17 at 10:09
u_ndefinedu_ndefined
848214
848214
$begingroup$
How does this work?
$endgroup$
– lirtosiast
May 29 at 10:05
$begingroup$
@lirtosiast explanation added
$endgroup$
– u_ndefined
May 29 at 10:33
add a comment |
$begingroup$
How does this work?
$endgroup$
– lirtosiast
May 29 at 10:05
$begingroup$
@lirtosiast explanation added
$endgroup$
– u_ndefined
May 29 at 10:33
$begingroup$
How does this work?
$endgroup$
– lirtosiast
May 29 at 10:05
$begingroup$
How does this work?
$endgroup$
– lirtosiast
May 29 at 10:05
$begingroup$
@lirtosiast explanation added
$endgroup$
– u_ndefined
May 29 at 10:33
$begingroup$
@lirtosiast explanation added
$endgroup$
– u_ndefined
May 29 at 10:33
add a comment |
$begingroup$
Python 3, 72 58 bytes
print(*[x[-1]+x[1:-1]+x[:x>x[0]]for x in input().split()])
Try it online!
$endgroup$
$begingroup$
Doesn't work for one letter words (ega
)
$endgroup$
– TFeld
May 16 at 19:37
$begingroup$
@TFeld, fixed..
$endgroup$
– alexz02
May 16 at 19:55
add a comment |
$begingroup$
Python 3, 72 58 bytes
print(*[x[-1]+x[1:-1]+x[:x>x[0]]for x in input().split()])
Try it online!
$endgroup$
$begingroup$
Doesn't work for one letter words (ega
)
$endgroup$
– TFeld
May 16 at 19:37
$begingroup$
@TFeld, fixed..
$endgroup$
– alexz02
May 16 at 19:55
add a comment |
$begingroup$
Python 3, 72 58 bytes
print(*[x[-1]+x[1:-1]+x[:x>x[0]]for x in input().split()])
Try it online!
$endgroup$
Python 3, 72 58 bytes
print(*[x[-1]+x[1:-1]+x[:x>x[0]]for x in input().split()])
Try it online!
edited May 16 at 19:54
answered May 16 at 19:31
alexz02alexz02
914
914
$begingroup$
Doesn't work for one letter words (ega
)
$endgroup$
– TFeld
May 16 at 19:37
$begingroup$
@TFeld, fixed..
$endgroup$
– alexz02
May 16 at 19:55
add a comment |
$begingroup$
Doesn't work for one letter words (ega
)
$endgroup$
– TFeld
May 16 at 19:37
$begingroup$
@TFeld, fixed..
$endgroup$
– alexz02
May 16 at 19:55
$begingroup$
Doesn't work for one letter words (eg
a
)$endgroup$
– TFeld
May 16 at 19:37
$begingroup$
Doesn't work for one letter words (eg
a
)$endgroup$
– TFeld
May 16 at 19:37
$begingroup$
@TFeld, fixed..
$endgroup$
– alexz02
May 16 at 19:55
$begingroup$
@TFeld, fixed..
$endgroup$
– alexz02
May 16 at 19:55
add a comment |
$begingroup$
laskelH, 71 bytes
h=reverse
s(x:y:o)=a:h(x:r)where(a:r)=h$y:o
s o=o
f=unwords.map s.words
Try it online!
Example in/output:
Swap the first and last letter in each word
This also works with single letter words like a
It is basically just a straight up implementation in which
I for words consisting of two or more letters cons the head
of the reversed tail on the reverse of the original head consed
on the reversed tail
Note that the rules say that we only have to support one kind
of separator - I am choosing spaces Technically it works with
other whitespace as well, but it will turn everything into spaces
in the end Line endings in this example usage are handled separately
to make the example output look nicer
pwaS eht tirsf dna tasl rettel ni hace dorw
shiT olsa sorkw hitw eingls rettel sordw eikl a
tI si yasicallb tusj a ttraighs pu nmplementatioi ni hhicw
I rof sordw gonsistinc fo owt ro eorm setterl sonc eht deah
fo eht deverser lait no eht eeversr fo eht lriginao deah donsec
no eht deverser lait
eotN that eht suler yas that ew ynlo eavh ot tuppors eno dink
fo reparatos - I ma ghoosinc spaces yechnicallT ti sorkw hitw
rtheo ehitespacw sa ,ellw tub ti lilw nurt gverythine onti spaces
ni eht dne einL sndinge ni shit example esagu era dandleh yeparatels
ot eakm eht example tutpuo kool ricen
```
$endgroup$
1
$begingroup$
The assignment in thewhere
clause can be moved to a binding in guard to save 5 bytes: Try it online!
$endgroup$
– Laikoni
May 19 at 13:16
1
$begingroup$
I see what you did there with the name "Haskell" in the title. I did the same thing on my PHP answer.
$endgroup$
– gwaugh
May 19 at 15:20
add a comment |
$begingroup$
laskelH, 71 bytes
h=reverse
s(x:y:o)=a:h(x:r)where(a:r)=h$y:o
s o=o
f=unwords.map s.words
Try it online!
Example in/output:
Swap the first and last letter in each word
This also works with single letter words like a
It is basically just a straight up implementation in which
I for words consisting of two or more letters cons the head
of the reversed tail on the reverse of the original head consed
on the reversed tail
Note that the rules say that we only have to support one kind
of separator - I am choosing spaces Technically it works with
other whitespace as well, but it will turn everything into spaces
in the end Line endings in this example usage are handled separately
to make the example output look nicer
pwaS eht tirsf dna tasl rettel ni hace dorw
shiT olsa sorkw hitw eingls rettel sordw eikl a
tI si yasicallb tusj a ttraighs pu nmplementatioi ni hhicw
I rof sordw gonsistinc fo owt ro eorm setterl sonc eht deah
fo eht deverser lait no eht eeversr fo eht lriginao deah donsec
no eht deverser lait
eotN that eht suler yas that ew ynlo eavh ot tuppors eno dink
fo reparatos - I ma ghoosinc spaces yechnicallT ti sorkw hitw
rtheo ehitespacw sa ,ellw tub ti lilw nurt gverythine onti spaces
ni eht dne einL sndinge ni shit example esagu era dandleh yeparatels
ot eakm eht example tutpuo kool ricen
```
$endgroup$
1
$begingroup$
The assignment in thewhere
clause can be moved to a binding in guard to save 5 bytes: Try it online!
$endgroup$
– Laikoni
May 19 at 13:16
1
$begingroup$
I see what you did there with the name "Haskell" in the title. I did the same thing on my PHP answer.
$endgroup$
– gwaugh
May 19 at 15:20
add a comment |
$begingroup$
laskelH, 71 bytes
h=reverse
s(x:y:o)=a:h(x:r)where(a:r)=h$y:o
s o=o
f=unwords.map s.words
Try it online!
Example in/output:
Swap the first and last letter in each word
This also works with single letter words like a
It is basically just a straight up implementation in which
I for words consisting of two or more letters cons the head
of the reversed tail on the reverse of the original head consed
on the reversed tail
Note that the rules say that we only have to support one kind
of separator - I am choosing spaces Technically it works with
other whitespace as well, but it will turn everything into spaces
in the end Line endings in this example usage are handled separately
to make the example output look nicer
pwaS eht tirsf dna tasl rettel ni hace dorw
shiT olsa sorkw hitw eingls rettel sordw eikl a
tI si yasicallb tusj a ttraighs pu nmplementatioi ni hhicw
I rof sordw gonsistinc fo owt ro eorm setterl sonc eht deah
fo eht deverser lait no eht eeversr fo eht lriginao deah donsec
no eht deverser lait
eotN that eht suler yas that ew ynlo eavh ot tuppors eno dink
fo reparatos - I ma ghoosinc spaces yechnicallT ti sorkw hitw
rtheo ehitespacw sa ,ellw tub ti lilw nurt gverythine onti spaces
ni eht dne einL sndinge ni shit example esagu era dandleh yeparatels
ot eakm eht example tutpuo kool ricen
```
$endgroup$
laskelH, 71 bytes
h=reverse
s(x:y:o)=a:h(x:r)where(a:r)=h$y:o
s o=o
f=unwords.map s.words
Try it online!
Example in/output:
Swap the first and last letter in each word
This also works with single letter words like a
It is basically just a straight up implementation in which
I for words consisting of two or more letters cons the head
of the reversed tail on the reverse of the original head consed
on the reversed tail
Note that the rules say that we only have to support one kind
of separator - I am choosing spaces Technically it works with
other whitespace as well, but it will turn everything into spaces
in the end Line endings in this example usage are handled separately
to make the example output look nicer
pwaS eht tirsf dna tasl rettel ni hace dorw
shiT olsa sorkw hitw eingls rettel sordw eikl a
tI si yasicallb tusj a ttraighs pu nmplementatioi ni hhicw
I rof sordw gonsistinc fo owt ro eorm setterl sonc eht deah
fo eht deverser lait no eht eeversr fo eht lriginao deah donsec
no eht deverser lait
eotN that eht suler yas that ew ynlo eavh ot tuppors eno dink
fo reparatos - I ma ghoosinc spaces yechnicallT ti sorkw hitw
rtheo ehitespacw sa ,ellw tub ti lilw nurt gverythine onti spaces
ni eht dne einL sndinge ni shit example esagu era dandleh yeparatels
ot eakm eht example tutpuo kool ricen
```
edited May 17 at 10:25
answered May 17 at 10:15
CubicCubic
41125
41125
1
$begingroup$
The assignment in thewhere
clause can be moved to a binding in guard to save 5 bytes: Try it online!
$endgroup$
– Laikoni
May 19 at 13:16
1
$begingroup$
I see what you did there with the name "Haskell" in the title. I did the same thing on my PHP answer.
$endgroup$
– gwaugh
May 19 at 15:20
add a comment |
1
$begingroup$
The assignment in thewhere
clause can be moved to a binding in guard to save 5 bytes: Try it online!
$endgroup$
– Laikoni
May 19 at 13:16
1
$begingroup$
I see what you did there with the name "Haskell" in the title. I did the same thing on my PHP answer.
$endgroup$
– gwaugh
May 19 at 15:20
1
1
$begingroup$
The assignment in the
where
clause can be moved to a binding in guard to save 5 bytes: Try it online!$endgroup$
– Laikoni
May 19 at 13:16
$begingroup$
The assignment in the
where
clause can be moved to a binding in guard to save 5 bytes: Try it online!$endgroup$
– Laikoni
May 19 at 13:16
1
1
$begingroup$
I see what you did there with the name "Haskell" in the title. I did the same thing on my PHP answer.
$endgroup$
– gwaugh
May 19 at 15:20
$begingroup$
I see what you did there with the name "Haskell" in the title. I did the same thing on my PHP answer.
$endgroup$
– gwaugh
May 19 at 15:20
add a comment |
$begingroup$
05AB1E, 10 bytes
#vyRćsRćðJ
Try it online!
-3 Thanks to @Kevin Cruijssen.
# | Split into words.
vy | For each word...
RćsRć | Reverse, split head, swap, reverse, split tail
ðJ | Join by spaces.
$endgroup$
1
$begingroup$
10 bytes
$endgroup$
– Kevin Cruijssen
May 16 at 19:58
1
$begingroup$
@KevinCruijssen I honestly want to delete it and give it to you, that was 99% your brainpower on the ordering of the arguments haha.
$endgroup$
– Magic Octopus Urn
May 16 at 20:29
1
$begingroup$
Found a 9-byter, but it only works in the legacy version:|ʒRćsRćJ,
$endgroup$
– Kevin Cruijssen
May 16 at 21:10
1
$begingroup$
Too bad we don't have aloop_as_long_as_there_are_inputs
, then I would have known an 8-byter:[RćsRćJ,
This 8-byter using[
never outputs in theory however, only when you're out of memory or time out like on TIO (and it requires a trailing newline in the input, otherwise it will keep using the last word)..
$endgroup$
– Kevin Cruijssen
May 16 at 21:15
1
$begingroup$
Unfortunately you needð¡
as single word input is possible, butð¡εćsÁì}ðý
also works at 10 bytes.
$endgroup$
– Emigna
May 17 at 6:06
|
show 5 more comments
$begingroup$
05AB1E, 10 bytes
#vyRćsRćðJ
Try it online!
-3 Thanks to @Kevin Cruijssen.
# | Split into words.
vy | For each word...
RćsRć | Reverse, split head, swap, reverse, split tail
ðJ | Join by spaces.
$endgroup$
1
$begingroup$
10 bytes
$endgroup$
– Kevin Cruijssen
May 16 at 19:58
1
$begingroup$
@KevinCruijssen I honestly want to delete it and give it to you, that was 99% your brainpower on the ordering of the arguments haha.
$endgroup$
– Magic Octopus Urn
May 16 at 20:29
1
$begingroup$
Found a 9-byter, but it only works in the legacy version:|ʒRćsRćJ,
$endgroup$
– Kevin Cruijssen
May 16 at 21:10
1
$begingroup$
Too bad we don't have aloop_as_long_as_there_are_inputs
, then I would have known an 8-byter:[RćsRćJ,
This 8-byter using[
never outputs in theory however, only when you're out of memory or time out like on TIO (and it requires a trailing newline in the input, otherwise it will keep using the last word)..
$endgroup$
– Kevin Cruijssen
May 16 at 21:15
1
$begingroup$
Unfortunately you needð¡
as single word input is possible, butð¡εćsÁì}ðý
also works at 10 bytes.
$endgroup$
– Emigna
May 17 at 6:06
|
show 5 more comments
$begingroup$
05AB1E, 10 bytes
#vyRćsRćðJ
Try it online!
-3 Thanks to @Kevin Cruijssen.
# | Split into words.
vy | For each word...
RćsRć | Reverse, split head, swap, reverse, split tail
ðJ | Join by spaces.
$endgroup$
05AB1E, 10 bytes
#vyRćsRćðJ
Try it online!
-3 Thanks to @Kevin Cruijssen.
# | Split into words.
vy | For each word...
RćsRć | Reverse, split head, swap, reverse, split tail
ðJ | Join by spaces.
edited May 16 at 23:21
answered May 16 at 19:30
Magic Octopus UrnMagic Octopus Urn
13.2k445127
13.2k445127
1
$begingroup$
10 bytes
$endgroup$
– Kevin Cruijssen
May 16 at 19:58
1
$begingroup$
@KevinCruijssen I honestly want to delete it and give it to you, that was 99% your brainpower on the ordering of the arguments haha.
$endgroup$
– Magic Octopus Urn
May 16 at 20:29
1
$begingroup$
Found a 9-byter, but it only works in the legacy version:|ʒRćsRćJ,
$endgroup$
– Kevin Cruijssen
May 16 at 21:10
1
$begingroup$
Too bad we don't have aloop_as_long_as_there_are_inputs
, then I would have known an 8-byter:[RćsRćJ,
This 8-byter using[
never outputs in theory however, only when you're out of memory or time out like on TIO (and it requires a trailing newline in the input, otherwise it will keep using the last word)..
$endgroup$
– Kevin Cruijssen
May 16 at 21:15
1
$begingroup$
Unfortunately you needð¡
as single word input is possible, butð¡εćsÁì}ðý
also works at 10 bytes.
$endgroup$
– Emigna
May 17 at 6:06
|
show 5 more comments
1
$begingroup$
10 bytes
$endgroup$
– Kevin Cruijssen
May 16 at 19:58
1
$begingroup$
@KevinCruijssen I honestly want to delete it and give it to you, that was 99% your brainpower on the ordering of the arguments haha.
$endgroup$
– Magic Octopus Urn
May 16 at 20:29
1
$begingroup$
Found a 9-byter, but it only works in the legacy version:|ʒRćsRćJ,
$endgroup$
– Kevin Cruijssen
May 16 at 21:10
1
$begingroup$
Too bad we don't have aloop_as_long_as_there_are_inputs
, then I would have known an 8-byter:[RćsRćJ,
This 8-byter using[
never outputs in theory however, only when you're out of memory or time out like on TIO (and it requires a trailing newline in the input, otherwise it will keep using the last word)..
$endgroup$
– Kevin Cruijssen
May 16 at 21:15
1
$begingroup$
Unfortunately you needð¡
as single word input is possible, butð¡εćsÁì}ðý
also works at 10 bytes.
$endgroup$
– Emigna
May 17 at 6:06
1
1
$begingroup$
10 bytes
$endgroup$
– Kevin Cruijssen
May 16 at 19:58
$begingroup$
10 bytes
$endgroup$
– Kevin Cruijssen
May 16 at 19:58
1
1
$begingroup$
@KevinCruijssen I honestly want to delete it and give it to you, that was 99% your brainpower on the ordering of the arguments haha.
$endgroup$
– Magic Octopus Urn
May 16 at 20:29
$begingroup$
@KevinCruijssen I honestly want to delete it and give it to you, that was 99% your brainpower on the ordering of the arguments haha.
$endgroup$
– Magic Octopus Urn
May 16 at 20:29
1
1
$begingroup$
Found a 9-byter, but it only works in the legacy version:
|ʒRćsRćJ,
$endgroup$
– Kevin Cruijssen
May 16 at 21:10
$begingroup$
Found a 9-byter, but it only works in the legacy version:
|ʒRćsRćJ,
$endgroup$
– Kevin Cruijssen
May 16 at 21:10
1
1
$begingroup$
Too bad we don't have a
loop_as_long_as_there_are_inputs
, then I would have known an 8-byter: [RćsRćJ,
This 8-byter using [
never outputs in theory however, only when you're out of memory or time out like on TIO (and it requires a trailing newline in the input, otherwise it will keep using the last word)..$endgroup$
– Kevin Cruijssen
May 16 at 21:15
$begingroup$
Too bad we don't have a
loop_as_long_as_there_are_inputs
, then I would have known an 8-byter: [RćsRćJ,
This 8-byter using [
never outputs in theory however, only when you're out of memory or time out like on TIO (and it requires a trailing newline in the input, otherwise it will keep using the last word)..$endgroup$
– Kevin Cruijssen
May 16 at 21:15
1
1
$begingroup$
Unfortunately you need
ð¡
as single word input is possible, but ð¡εćsÁì}ðý
also works at 10 bytes.$endgroup$
– Emigna
May 17 at 6:06
$begingroup$
Unfortunately you need
ð¡
as single word input is possible, but ð¡εćsÁì}ðý
also works at 10 bytes.$endgroup$
– Emigna
May 17 at 6:06
|
show 5 more comments
$begingroup$
J, 23 17 bytes
({:,1|.}:)&.>&.;:
Try it online!
$endgroup$
$begingroup$
Very nice trick to swap the first/last letters by rotating and applying1 A.
!
$endgroup$
– Galen Ivanov
May 17 at 8:55
1
$begingroup$
1&A.&.(1&|.)
->({:,1|.}:)
and then you can remove the::]
$endgroup$
– ngn
May 17 at 21:04
$begingroup$
Amazing, thank you
$endgroup$
– FrownyFrog
May 17 at 21:09
$begingroup$
Really amazing! Once again I'm amazed how simple and elegant can the solution be, but only after I see it done by someone else.
$endgroup$
– Galen Ivanov
May 18 at 6:43
add a comment |
$begingroup$
J, 23 17 bytes
({:,1|.}:)&.>&.;:
Try it online!
$endgroup$
$begingroup$
Very nice trick to swap the first/last letters by rotating and applying1 A.
!
$endgroup$
– Galen Ivanov
May 17 at 8:55
1
$begingroup$
1&A.&.(1&|.)
->({:,1|.}:)
and then you can remove the::]
$endgroup$
– ngn
May 17 at 21:04
$begingroup$
Amazing, thank you
$endgroup$
– FrownyFrog
May 17 at 21:09
$begingroup$
Really amazing! Once again I'm amazed how simple and elegant can the solution be, but only after I see it done by someone else.
$endgroup$
– Galen Ivanov
May 18 at 6:43
add a comment |
$begingroup$
J, 23 17 bytes
({:,1|.}:)&.>&.;:
Try it online!
$endgroup$
J, 23 17 bytes
({:,1|.}:)&.>&.;:
Try it online!
edited May 17 at 21:23
answered May 17 at 8:23
FrownyFrogFrownyFrog
2,6771618
2,6771618
$begingroup$
Very nice trick to swap the first/last letters by rotating and applying1 A.
!
$endgroup$
– Galen Ivanov
May 17 at 8:55
1
$begingroup$
1&A.&.(1&|.)
->({:,1|.}:)
and then you can remove the::]
$endgroup$
– ngn
May 17 at 21:04
$begingroup$
Amazing, thank you
$endgroup$
– FrownyFrog
May 17 at 21:09
$begingroup$
Really amazing! Once again I'm amazed how simple and elegant can the solution be, but only after I see it done by someone else.
$endgroup$
– Galen Ivanov
May 18 at 6:43
add a comment |
$begingroup$
Very nice trick to swap the first/last letters by rotating and applying1 A.
!
$endgroup$
– Galen Ivanov
May 17 at 8:55
1
$begingroup$
1&A.&.(1&|.)
->({:,1|.}:)
and then you can remove the::]
$endgroup$
– ngn
May 17 at 21:04
$begingroup$
Amazing, thank you
$endgroup$
– FrownyFrog
May 17 at 21:09
$begingroup$
Really amazing! Once again I'm amazed how simple and elegant can the solution be, but only after I see it done by someone else.
$endgroup$
– Galen Ivanov
May 18 at 6:43
$begingroup$
Very nice trick to swap the first/last letters by rotating and applying
1 A.
!$endgroup$
– Galen Ivanov
May 17 at 8:55
$begingroup$
Very nice trick to swap the first/last letters by rotating and applying
1 A.
!$endgroup$
– Galen Ivanov
May 17 at 8:55
1
1
$begingroup$
1&A.&.(1&|.)
-> ({:,1|.}:)
and then you can remove the ::]
$endgroup$
– ngn
May 17 at 21:04
$begingroup$
1&A.&.(1&|.)
-> ({:,1|.}:)
and then you can remove the ::]
$endgroup$
– ngn
May 17 at 21:04
$begingroup$
Amazing, thank you
$endgroup$
– FrownyFrog
May 17 at 21:09
$begingroup$
Amazing, thank you
$endgroup$
– FrownyFrog
May 17 at 21:09
$begingroup$
Really amazing! Once again I'm amazed how simple and elegant can the solution be, but only after I see it done by someone else.
$endgroup$
– Galen Ivanov
May 18 at 6:43
$begingroup$
Really amazing! Once again I'm amazed how simple and elegant can the solution be, but only after I see it done by someone else.
$endgroup$
– Galen Ivanov
May 18 at 6:43
add a comment |
$begingroup$
Ruby with -p
, 42 41 29 bytes
gsub /(w)(w*)(w)/,'321'
Try it online!
$endgroup$
1
$begingroup$
We no longer include flags in byte counts
$endgroup$
– Shaggy
May 16 at 21:50
$begingroup$
@Shaggy thanks for the heads up. If you look at my post history it shows I was away for 8 months without any answers, so I've likely missed a few memos during that time, haha
$endgroup$
– Value Ink
May 16 at 21:57
$begingroup$
Pretty sure the consensus was changed more than 8 months ago but just in case you missed it: "non-competing" is also no longer a thing.
$endgroup$
– Shaggy
May 16 at 21:58
$begingroup$
Nicely done. I think under the rules you can use newlines as your delimiter and replace thew
s with.
s.
$endgroup$
– histocrat
May 17 at 18:23
add a comment |
$begingroup$
Ruby with -p
, 42 41 29 bytes
gsub /(w)(w*)(w)/,'321'
Try it online!
$endgroup$
1
$begingroup$
We no longer include flags in byte counts
$endgroup$
– Shaggy
May 16 at 21:50
$begingroup$
@Shaggy thanks for the heads up. If you look at my post history it shows I was away for 8 months without any answers, so I've likely missed a few memos during that time, haha
$endgroup$
– Value Ink
May 16 at 21:57
$begingroup$
Pretty sure the consensus was changed more than 8 months ago but just in case you missed it: "non-competing" is also no longer a thing.
$endgroup$
– Shaggy
May 16 at 21:58
$begingroup$
Nicely done. I think under the rules you can use newlines as your delimiter and replace thew
s with.
s.
$endgroup$
– histocrat
May 17 at 18:23
add a comment |
$begingroup$
Ruby with -p
, 42 41 29 bytes
gsub /(w)(w*)(w)/,'321'
Try it online!
$endgroup$
Ruby with -p
, 42 41 29 bytes
gsub /(w)(w*)(w)/,'321'
Try it online!
edited May 16 at 21:54
answered May 16 at 19:41
Value InkValue Ink
8,405732
8,405732
1
$begingroup$
We no longer include flags in byte counts
$endgroup$
– Shaggy
May 16 at 21:50
$begingroup$
@Shaggy thanks for the heads up. If you look at my post history it shows I was away for 8 months without any answers, so I've likely missed a few memos during that time, haha
$endgroup$
– Value Ink
May 16 at 21:57
$begingroup$
Pretty sure the consensus was changed more than 8 months ago but just in case you missed it: "non-competing" is also no longer a thing.
$endgroup$
– Shaggy
May 16 at 21:58
$begingroup$
Nicely done. I think under the rules you can use newlines as your delimiter and replace thew
s with.
s.
$endgroup$
– histocrat
May 17 at 18:23
add a comment |
1
$begingroup$
We no longer include flags in byte counts
$endgroup$
– Shaggy
May 16 at 21:50
$begingroup$
@Shaggy thanks for the heads up. If you look at my post history it shows I was away for 8 months without any answers, so I've likely missed a few memos during that time, haha
$endgroup$
– Value Ink
May 16 at 21:57
$begingroup$
Pretty sure the consensus was changed more than 8 months ago but just in case you missed it: "non-competing" is also no longer a thing.
$endgroup$
– Shaggy
May 16 at 21:58
$begingroup$
Nicely done. I think under the rules you can use newlines as your delimiter and replace thew
s with.
s.
$endgroup$
– histocrat
May 17 at 18:23
1
1
$begingroup$
We no longer include flags in byte counts
$endgroup$
– Shaggy
May 16 at 21:50
$begingroup$
We no longer include flags in byte counts
$endgroup$
– Shaggy
May 16 at 21:50
$begingroup$
@Shaggy thanks for the heads up. If you look at my post history it shows I was away for 8 months without any answers, so I've likely missed a few memos during that time, haha
$endgroup$
– Value Ink
May 16 at 21:57
$begingroup$
@Shaggy thanks for the heads up. If you look at my post history it shows I was away for 8 months without any answers, so I've likely missed a few memos during that time, haha
$endgroup$
– Value Ink
May 16 at 21:57
$begingroup$
Pretty sure the consensus was changed more than 8 months ago but just in case you missed it: "non-competing" is also no longer a thing.
$endgroup$
– Shaggy
May 16 at 21:58
$begingroup$
Pretty sure the consensus was changed more than 8 months ago but just in case you missed it: "non-competing" is also no longer a thing.
$endgroup$
– Shaggy
May 16 at 21:58
$begingroup$
Nicely done. I think under the rules you can use newlines as your delimiter and replace the
w
s with .
s.$endgroup$
– histocrat
May 17 at 18:23
$begingroup$
Nicely done. I think under the rules you can use newlines as your delimiter and replace the
w
s with .
s.$endgroup$
– histocrat
May 17 at 18:23
add a comment |
$begingroup$
Haskell, 54 bytes
unwords.map f.words
f[a]=[a]
f(a:b)=last b:init b++[a]
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 54 bytes
unwords.map f.words
f[a]=[a]
f(a:b)=last b:init b++[a]
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 54 bytes
unwords.map f.words
f[a]=[a]
f(a:b)=last b:init b++[a]
Try it online!
$endgroup$
Haskell, 54 bytes
unwords.map f.words
f[a]=[a]
f(a:b)=last b:init b++[a]
Try it online!
answered May 17 at 14:04
niminimi
33.3k32491
33.3k32491
add a comment |
add a comment |
$begingroup$
Japt -S
, 7 bytes
¸®ÎiZÅé
Try it
$endgroup$
1
$begingroup$
Nice! I knew there'd be a shorter way.
$endgroup$
– Shaggy
May 17 at 17:44
1
$begingroup$
I am impressed. Well done
$endgroup$
– Embodiment of Ignorance
May 18 at 1:21
add a comment |
$begingroup$
Japt -S
, 7 bytes
¸®ÎiZÅé
Try it
$endgroup$
1
$begingroup$
Nice! I knew there'd be a shorter way.
$endgroup$
– Shaggy
May 17 at 17:44
1
$begingroup$
I am impressed. Well done
$endgroup$
– Embodiment of Ignorance
May 18 at 1:21
add a comment |
$begingroup$
Japt -S
, 7 bytes
¸®ÎiZÅé
Try it
$endgroup$
Japt -S
, 7 bytes
¸®ÎiZÅé
Try it
answered May 17 at 17:20
OliverOliver
5,5251833
5,5251833
1
$begingroup$
Nice! I knew there'd be a shorter way.
$endgroup$
– Shaggy
May 17 at 17:44
1
$begingroup$
I am impressed. Well done
$endgroup$
– Embodiment of Ignorance
May 18 at 1:21
add a comment |
1
$begingroup$
Nice! I knew there'd be a shorter way.
$endgroup$
– Shaggy
May 17 at 17:44
1
$begingroup$
I am impressed. Well done
$endgroup$
– Embodiment of Ignorance
May 18 at 1:21
1
1
$begingroup$
Nice! I knew there'd be a shorter way.
$endgroup$
– Shaggy
May 17 at 17:44
$begingroup$
Nice! I knew there'd be a shorter way.
$endgroup$
– Shaggy
May 17 at 17:44
1
1
$begingroup$
I am impressed. Well done
$endgroup$
– Embodiment of Ignorance
May 18 at 1:21
$begingroup$
I am impressed. Well done
$endgroup$
– Embodiment of Ignorance
May 18 at 1:21
add a comment |
$begingroup$
PowerShell, 37 bytes
$args-replace'(w)(w*)(w)','$3$2$1'
Try it online!
$endgroup$
add a comment |
$begingroup$
PowerShell, 37 bytes
$args-replace'(w)(w*)(w)','$3$2$1'
Try it online!
$endgroup$
add a comment |
$begingroup$
PowerShell, 37 bytes
$args-replace'(w)(w*)(w)','$3$2$1'
Try it online!
$endgroup$
PowerShell, 37 bytes
$args-replace'(w)(w*)(w)','$3$2$1'
Try it online!
answered May 18 at 5:45
mazzymazzy
3,4361419
3,4361419
add a comment |
add a comment |
$begingroup$
Stax, 8 bytes
Σq╞♪áZN¢
Run and debug it
Uses newlines as word separators.
$endgroup$
add a comment |
$begingroup$
Stax, 8 bytes
Σq╞♪áZN¢
Run and debug it
Uses newlines as word separators.
$endgroup$
add a comment |
$begingroup$
Stax, 8 bytes
Σq╞♪áZN¢
Run and debug it
Uses newlines as word separators.
$endgroup$
Stax, 8 bytes
Σq╞♪áZN¢
Run and debug it
Uses newlines as word separators.
edited May 16 at 23:25
answered May 16 at 19:39
recursiverecursive
6,5891326
6,5891326
add a comment |
add a comment |
$begingroup$
Whitespace, 179 bytes
[N
S S S N
_Create_Label_OUTER_LOOP][S S S N
_Push_n=0][N
S S T N
_Create_Label_INNER_LOOP][S N
S _Duplicate_n][S N
S _Duplicate_n][S N
S _Duplicate_n][T N
T S _Read_STDIN_as_character][T T T _Retrieve_input][S S S T S T T N
_Push_11][T S S T _Subtract_t=input-11][N
T T S S N
_If_t<0_jump_to_Label_PRINT][S S S T N
_Push_1][T S S S _Add_n=n+1][N
S N
T N
_Jump_to_Label_INNER_LOOP][N
S S S S N
_Create_Label_PRINT][S S S T N
_Push_1][T S S T _Subtract_n=n-1][S N
S _Duplicate_n][S N
S _Duplicate_n][N
T S N
_If_n==0_jump_to_Label_PRINT_TRAILING][T T T _Retrieve][T N
S S _Print_as_character][S S S N
_Push_s=0][N
S S S T N
_Create_Label_PRINT_LOOP][S S S T N
_Push_1][T S S S _Add_s=s+1][S N
S _Duplicate_s][S T S S T S N
_Copy_0-based_2nd_n][T S S T _Subtract_i=s-n][N
T S N
_If_0_Jump_to_Label_PRINT_TRAILING][S N
S _Duplicate_s][T T T _Retrieve][T N
S S _Print_as_character][N
S T S T N
_Jump_to_Label_PRINT_LOOP][N
S S N
_Create_Label_PRINT_TRAILING][S S S N
_Push_0][T T T _Retrieve][T N
S S _Print_as_character][S S S T S S T N
_Push_9_tab][T N
S S _Print_as_character][N
S N
S N
_Jump_to_Label_OUTER_LOOP]
Letters S
(space), T
(tab), and N
(new-line) added as highlighting only.[..._some_action]
added as explanation only.
Tab as delimiter. Input should contain a trailing newline (or tab), otherwise the program doesn't know when to stop, since taking input in Whitespace can only be done one character at a time.
Try it online (with raw spaces, tabs, and new-lines only).
Explanation in pseudo-code:
Whitespace only has a stack and a heap, where the heap is a map with a key and value (both integers). Inputs can only be read one integer or character at a time, which are always placed in the heap as integers, and can then be received and pushed to the stack with their defined heap-addresses (map-keys). In my approach I store the entire word at the heap-addresses (map-keys) $[0, ..., text{word_length}]$, and then retrieve the characters to print one by one in the order we'd want after a tab (or newline) is encountered as delimiter.
Start OUTER_LOOP:
Integer n = 0
Start INNER_LOOP:
Character c = STDIN as character, saved at heap-address n
If(c == 't' OR c == 'n'):
Jump to PRINT
n = n + 1
Go to next iteration of INNER_LOOP
PRINT:
n = n - 1
If(n == 0): (this means it was a single-letter word)
Jump to PRINT_TRAILING
Character c = get character from heap-address n
Print c as character
Integer s = 0
Start PRINT_LOOP:
s = s + 1
If(s - n == 0):
Jump to PRINT_TRAILING
Character c = get character from heap-address s
Print c as character
Go to next iteration of PRINT_LOOP
PRINT_TRAILING:
Character c = get character from heap-address 0
Print c as character
Print 't'
Go to next iteration of OUTER_LOOP
The program terminates with an error when it tries to read a character when none is given in TIO (or it hangs waiting for an input in some Whitespace compilers like vii5ard).
$endgroup$
add a comment |
$begingroup$
Whitespace, 179 bytes
[N
S S S N
_Create_Label_OUTER_LOOP][S S S N
_Push_n=0][N
S S T N
_Create_Label_INNER_LOOP][S N
S _Duplicate_n][S N
S _Duplicate_n][S N
S _Duplicate_n][T N
T S _Read_STDIN_as_character][T T T _Retrieve_input][S S S T S T T N
_Push_11][T S S T _Subtract_t=input-11][N
T T S S N
_If_t<0_jump_to_Label_PRINT][S S S T N
_Push_1][T S S S _Add_n=n+1][N
S N
T N
_Jump_to_Label_INNER_LOOP][N
S S S S N
_Create_Label_PRINT][S S S T N
_Push_1][T S S T _Subtract_n=n-1][S N
S _Duplicate_n][S N
S _Duplicate_n][N
T S N
_If_n==0_jump_to_Label_PRINT_TRAILING][T T T _Retrieve][T N
S S _Print_as_character][S S S N
_Push_s=0][N
S S S T N
_Create_Label_PRINT_LOOP][S S S T N
_Push_1][T S S S _Add_s=s+1][S N
S _Duplicate_s][S T S S T S N
_Copy_0-based_2nd_n][T S S T _Subtract_i=s-n][N
T S N
_If_0_Jump_to_Label_PRINT_TRAILING][S N
S _Duplicate_s][T T T _Retrieve][T N
S S _Print_as_character][N
S T S T N
_Jump_to_Label_PRINT_LOOP][N
S S N
_Create_Label_PRINT_TRAILING][S S S N
_Push_0][T T T _Retrieve][T N
S S _Print_as_character][S S S T S S T N
_Push_9_tab][T N
S S _Print_as_character][N
S N
S N
_Jump_to_Label_OUTER_LOOP]
Letters S
(space), T
(tab), and N
(new-line) added as highlighting only.[..._some_action]
added as explanation only.
Tab as delimiter. Input should contain a trailing newline (or tab), otherwise the program doesn't know when to stop, since taking input in Whitespace can only be done one character at a time.
Try it online (with raw spaces, tabs, and new-lines only).
Explanation in pseudo-code:
Whitespace only has a stack and a heap, where the heap is a map with a key and value (both integers). Inputs can only be read one integer or character at a time, which are always placed in the heap as integers, and can then be received and pushed to the stack with their defined heap-addresses (map-keys). In my approach I store the entire word at the heap-addresses (map-keys) $[0, ..., text{word_length}]$, and then retrieve the characters to print one by one in the order we'd want after a tab (or newline) is encountered as delimiter.
Start OUTER_LOOP:
Integer n = 0
Start INNER_LOOP:
Character c = STDIN as character, saved at heap-address n
If(c == 't' OR c == 'n'):
Jump to PRINT
n = n + 1
Go to next iteration of INNER_LOOP
PRINT:
n = n - 1
If(n == 0): (this means it was a single-letter word)
Jump to PRINT_TRAILING
Character c = get character from heap-address n
Print c as character
Integer s = 0
Start PRINT_LOOP:
s = s + 1
If(s - n == 0):
Jump to PRINT_TRAILING
Character c = get character from heap-address s
Print c as character
Go to next iteration of PRINT_LOOP
PRINT_TRAILING:
Character c = get character from heap-address 0
Print c as character
Print 't'
Go to next iteration of OUTER_LOOP
The program terminates with an error when it tries to read a character when none is given in TIO (or it hangs waiting for an input in some Whitespace compilers like vii5ard).
$endgroup$
add a comment |
$begingroup$
Whitespace, 179 bytes
[N
S S S N
_Create_Label_OUTER_LOOP][S S S N
_Push_n=0][N
S S T N
_Create_Label_INNER_LOOP][S N
S _Duplicate_n][S N
S _Duplicate_n][S N
S _Duplicate_n][T N
T S _Read_STDIN_as_character][T T T _Retrieve_input][S S S T S T T N
_Push_11][T S S T _Subtract_t=input-11][N
T T S S N
_If_t<0_jump_to_Label_PRINT][S S S T N
_Push_1][T S S S _Add_n=n+1][N
S N
T N
_Jump_to_Label_INNER_LOOP][N
S S S S N
_Create_Label_PRINT][S S S T N
_Push_1][T S S T _Subtract_n=n-1][S N
S _Duplicate_n][S N
S _Duplicate_n][N
T S N
_If_n==0_jump_to_Label_PRINT_TRAILING][T T T _Retrieve][T N
S S _Print_as_character][S S S N
_Push_s=0][N
S S S T N
_Create_Label_PRINT_LOOP][S S S T N
_Push_1][T S S S _Add_s=s+1][S N
S _Duplicate_s][S T S S T S N
_Copy_0-based_2nd_n][T S S T _Subtract_i=s-n][N
T S N
_If_0_Jump_to_Label_PRINT_TRAILING][S N
S _Duplicate_s][T T T _Retrieve][T N
S S _Print_as_character][N
S T S T N
_Jump_to_Label_PRINT_LOOP][N
S S N
_Create_Label_PRINT_TRAILING][S S S N
_Push_0][T T T _Retrieve][T N
S S _Print_as_character][S S S T S S T N
_Push_9_tab][T N
S S _Print_as_character][N
S N
S N
_Jump_to_Label_OUTER_LOOP]
Letters S
(space), T
(tab), and N
(new-line) added as highlighting only.[..._some_action]
added as explanation only.
Tab as delimiter. Input should contain a trailing newline (or tab), otherwise the program doesn't know when to stop, since taking input in Whitespace can only be done one character at a time.
Try it online (with raw spaces, tabs, and new-lines only).
Explanation in pseudo-code:
Whitespace only has a stack and a heap, where the heap is a map with a key and value (both integers). Inputs can only be read one integer or character at a time, which are always placed in the heap as integers, and can then be received and pushed to the stack with their defined heap-addresses (map-keys). In my approach I store the entire word at the heap-addresses (map-keys) $[0, ..., text{word_length}]$, and then retrieve the characters to print one by one in the order we'd want after a tab (or newline) is encountered as delimiter.
Start OUTER_LOOP:
Integer n = 0
Start INNER_LOOP:
Character c = STDIN as character, saved at heap-address n
If(c == 't' OR c == 'n'):
Jump to PRINT
n = n + 1
Go to next iteration of INNER_LOOP
PRINT:
n = n - 1
If(n == 0): (this means it was a single-letter word)
Jump to PRINT_TRAILING
Character c = get character from heap-address n
Print c as character
Integer s = 0
Start PRINT_LOOP:
s = s + 1
If(s - n == 0):
Jump to PRINT_TRAILING
Character c = get character from heap-address s
Print c as character
Go to next iteration of PRINT_LOOP
PRINT_TRAILING:
Character c = get character from heap-address 0
Print c as character
Print 't'
Go to next iteration of OUTER_LOOP
The program terminates with an error when it tries to read a character when none is given in TIO (or it hangs waiting for an input in some Whitespace compilers like vii5ard).
$endgroup$
Whitespace, 179 bytes
[N
S S S N
_Create_Label_OUTER_LOOP][S S S N
_Push_n=0][N
S S T N
_Create_Label_INNER_LOOP][S N
S _Duplicate_n][S N
S _Duplicate_n][S N
S _Duplicate_n][T N
T S _Read_STDIN_as_character][T T T _Retrieve_input][S S S T S T T N
_Push_11][T S S T _Subtract_t=input-11][N
T T S S N
_If_t<0_jump_to_Label_PRINT][S S S T N
_Push_1][T S S S _Add_n=n+1][N
S N
T N
_Jump_to_Label_INNER_LOOP][N
S S S S N
_Create_Label_PRINT][S S S T N
_Push_1][T S S T _Subtract_n=n-1][S N
S _Duplicate_n][S N
S _Duplicate_n][N
T S N
_If_n==0_jump_to_Label_PRINT_TRAILING][T T T _Retrieve][T N
S S _Print_as_character][S S S N
_Push_s=0][N
S S S T N
_Create_Label_PRINT_LOOP][S S S T N
_Push_1][T S S S _Add_s=s+1][S N
S _Duplicate_s][S T S S T S N
_Copy_0-based_2nd_n][T S S T _Subtract_i=s-n][N
T S N
_If_0_Jump_to_Label_PRINT_TRAILING][S N
S _Duplicate_s][T T T _Retrieve][T N
S S _Print_as_character][N
S T S T N
_Jump_to_Label_PRINT_LOOP][N
S S N
_Create_Label_PRINT_TRAILING][S S S N
_Push_0][T T T _Retrieve][T N
S S _Print_as_character][S S S T S S T N
_Push_9_tab][T N
S S _Print_as_character][N
S N
S N
_Jump_to_Label_OUTER_LOOP]
Letters S
(space), T
(tab), and N
(new-line) added as highlighting only.[..._some_action]
added as explanation only.
Tab as delimiter. Input should contain a trailing newline (or tab), otherwise the program doesn't know when to stop, since taking input in Whitespace can only be done one character at a time.
Try it online (with raw spaces, tabs, and new-lines only).
Explanation in pseudo-code:
Whitespace only has a stack and a heap, where the heap is a map with a key and value (both integers). Inputs can only be read one integer or character at a time, which are always placed in the heap as integers, and can then be received and pushed to the stack with their defined heap-addresses (map-keys). In my approach I store the entire word at the heap-addresses (map-keys) $[0, ..., text{word_length}]$, and then retrieve the characters to print one by one in the order we'd want after a tab (or newline) is encountered as delimiter.
Start OUTER_LOOP:
Integer n = 0
Start INNER_LOOP:
Character c = STDIN as character, saved at heap-address n
If(c == 't' OR c == 'n'):
Jump to PRINT
n = n + 1
Go to next iteration of INNER_LOOP
PRINT:
n = n - 1
If(n == 0): (this means it was a single-letter word)
Jump to PRINT_TRAILING
Character c = get character from heap-address n
Print c as character
Integer s = 0
Start PRINT_LOOP:
s = s + 1
If(s - n == 0):
Jump to PRINT_TRAILING
Character c = get character from heap-address s
Print c as character
Go to next iteration of PRINT_LOOP
PRINT_TRAILING:
Character c = get character from heap-address 0
Print c as character
Print 't'
Go to next iteration of OUTER_LOOP
The program terminates with an error when it tries to read a character when none is given in TIO (or it hangs waiting for an input in some Whitespace compilers like vii5ard).
answered May 17 at 10:08
Kevin CruijssenKevin Cruijssen
46k577232
46k577232
add a comment |
add a comment |
$begingroup$
Wolfram Language (Mathematica), 58 bytes
StringReplace[#,a:u~~w:u..~~b:u:>b<>w<>a/.{u->Except@#2}]&
Try it online!
-22 bytes from @attinat
-12 bytes from @M.Stern
$endgroup$
$begingroup$
70 bytes usingStringReplace
withStringExpression
s
$endgroup$
– attinat
May 17 at 7:25
1
$begingroup$
64 bytes usingStringTake
instead ofStringReplace
:StringRiffle[StringSplit@##~StringTake~{{-1},{2,-2},{1}},#2,""]&
$endgroup$
– Roman
May 18 at 17:29
2
$begingroup$
Here is a more direct approach:StringReplace[#, a : u ~~ w : u .. ~~ b : u :> b <> w <> a /. {u -> Except@#2}] &
$endgroup$
– M. Stern
May 19 at 7:44
1
$begingroup$
{ and } are optional :)
$endgroup$
– M. Stern
May 19 at 7:55
1
$begingroup$
55 bytes, also fixes 2-character words
$endgroup$
– attinat
May 19 at 23:59
add a comment |
$begingroup$
Wolfram Language (Mathematica), 58 bytes
StringReplace[#,a:u~~w:u..~~b:u:>b<>w<>a/.{u->Except@#2}]&
Try it online!
-22 bytes from @attinat
-12 bytes from @M.Stern
$endgroup$
$begingroup$
70 bytes usingStringReplace
withStringExpression
s
$endgroup$
– attinat
May 17 at 7:25
1
$begingroup$
64 bytes usingStringTake
instead ofStringReplace
:StringRiffle[StringSplit@##~StringTake~{{-1},{2,-2},{1}},#2,""]&
$endgroup$
– Roman
May 18 at 17:29
2
$begingroup$
Here is a more direct approach:StringReplace[#, a : u ~~ w : u .. ~~ b : u :> b <> w <> a /. {u -> Except@#2}] &
$endgroup$
– M. Stern
May 19 at 7:44
1
$begingroup$
{ and } are optional :)
$endgroup$
– M. Stern
May 19 at 7:55
1
$begingroup$
55 bytes, also fixes 2-character words
$endgroup$
– attinat
May 19 at 23:59
add a comment |
$begingroup$
Wolfram Language (Mathematica), 58 bytes
StringReplace[#,a:u~~w:u..~~b:u:>b<>w<>a/.{u->Except@#2}]&
Try it online!
-22 bytes from @attinat
-12 bytes from @M.Stern
$endgroup$
Wolfram Language (Mathematica), 58 bytes
StringReplace[#,a:u~~w:u..~~b:u:>b<>w<>a/.{u->Except@#2}]&
Try it online!
-22 bytes from @attinat
-12 bytes from @M.Stern
edited May 19 at 11:26
answered May 16 at 20:21
J42161217J42161217
15.1k21457
15.1k21457
$begingroup$
70 bytes usingStringReplace
withStringExpression
s
$endgroup$
– attinat
May 17 at 7:25
1
$begingroup$
64 bytes usingStringTake
instead ofStringReplace
:StringRiffle[StringSplit@##~StringTake~{{-1},{2,-2},{1}},#2,""]&
$endgroup$
– Roman
May 18 at 17:29
2
$begingroup$
Here is a more direct approach:StringReplace[#, a : u ~~ w : u .. ~~ b : u :> b <> w <> a /. {u -> Except@#2}] &
$endgroup$
– M. Stern
May 19 at 7:44
1
$begingroup$
{ and } are optional :)
$endgroup$
– M. Stern
May 19 at 7:55
1
$begingroup$
55 bytes, also fixes 2-character words
$endgroup$
– attinat
May 19 at 23:59
add a comment |
$begingroup$
70 bytes usingStringReplace
withStringExpression
s
$endgroup$
– attinat
May 17 at 7:25
1
$begingroup$
64 bytes usingStringTake
instead ofStringReplace
:StringRiffle[StringSplit@##~StringTake~{{-1},{2,-2},{1}},#2,""]&
$endgroup$
– Roman
May 18 at 17:29
2
$begingroup$
Here is a more direct approach:StringReplace[#, a : u ~~ w : u .. ~~ b : u :> b <> w <> a /. {u -> Except@#2}] &
$endgroup$
– M. Stern
May 19 at 7:44
1
$begingroup$
{ and } are optional :)
$endgroup$
– M. Stern
May 19 at 7:55
1
$begingroup$
55 bytes, also fixes 2-character words
$endgroup$
– attinat
May 19 at 23:59
$begingroup$
70 bytes using
StringReplace
with StringExpression
s$endgroup$
– attinat
May 17 at 7:25
$begingroup$
70 bytes using
StringReplace
with StringExpression
s$endgroup$
– attinat
May 17 at 7:25
1
1
$begingroup$
64 bytes using
StringTake
instead of StringReplace
: StringRiffle[StringSplit@##~StringTake~{{-1},{2,-2},{1}},#2,""]&
$endgroup$
– Roman
May 18 at 17:29
$begingroup$
64 bytes using
StringTake
instead of StringReplace
: StringRiffle[StringSplit@##~StringTake~{{-1},{2,-2},{1}},#2,""]&
$endgroup$
– Roman
May 18 at 17:29
2
2
$begingroup$
Here is a more direct approach:
StringReplace[#, a : u ~~ w : u .. ~~ b : u :> b <> w <> a /. {u -> Except@#2}] &
$endgroup$
– M. Stern
May 19 at 7:44
$begingroup$
Here is a more direct approach:
StringReplace[#, a : u ~~ w : u .. ~~ b : u :> b <> w <> a /. {u -> Except@#2}] &
$endgroup$
– M. Stern
May 19 at 7:44
1
1
$begingroup$
{ and } are optional :)
$endgroup$
– M. Stern
May 19 at 7:55
$begingroup$
{ and } are optional :)
$endgroup$
– M. Stern
May 19 at 7:55
1
1
$begingroup$
55 bytes, also fixes 2-character words
$endgroup$
– attinat
May 19 at 23:59
$begingroup$
55 bytes, also fixes 2-character words
$endgroup$
– attinat
May 19 at 23:59
add a comment |
$begingroup$
QuadR, 20 bytes
(w)(w*)(w)
321
Simply make three capturing groups consisting of 1, 0-or-more, and 1 word-characters, then reverses their order.
Try it online!
$endgroup$
add a comment |
$begingroup$
QuadR, 20 bytes
(w)(w*)(w)
321
Simply make three capturing groups consisting of 1, 0-or-more, and 1 word-characters, then reverses their order.
Try it online!
$endgroup$
add a comment |
$begingroup$
QuadR, 20 bytes
(w)(w*)(w)
321
Simply make three capturing groups consisting of 1, 0-or-more, and 1 word-characters, then reverses their order.
Try it online!
$endgroup$
QuadR, 20 bytes
(w)(w*)(w)
321
Simply make three capturing groups consisting of 1, 0-or-more, and 1 word-characters, then reverses their order.
Try it online!
answered May 16 at 19:45
AdámAdám
27.9k277210
27.9k277210
add a comment |
add a comment |
$begingroup$
APL+WIN, 50 bytes
(∊¯1↑¨s),¨1↓¨(¯1↓¨s),¨↑¨s←((+s=' ')⊂s←' ',⎕)~¨' '
Prompts for string and uses space as the delimiter.
Try it online! Courtesy of Dyalog Classic
$endgroup$
add a comment |
$begingroup$
APL+WIN, 50 bytes
(∊¯1↑¨s),¨1↓¨(¯1↓¨s),¨↑¨s←((+s=' ')⊂s←' ',⎕)~¨' '
Prompts for string and uses space as the delimiter.
Try it online! Courtesy of Dyalog Classic
$endgroup$
add a comment |
$begingroup$
APL+WIN, 50 bytes
(∊¯1↑¨s),¨1↓¨(¯1↓¨s),¨↑¨s←((+s=' ')⊂s←' ',⎕)~¨' '
Prompts for string and uses space as the delimiter.
Try it online! Courtesy of Dyalog Classic
$endgroup$
APL+WIN, 50 bytes
(∊¯1↑¨s),¨1↓¨(¯1↓¨s),¨↑¨s←((+s=' ')⊂s←' ',⎕)~¨' '
Prompts for string and uses space as the delimiter.
Try it online! Courtesy of Dyalog Classic
answered May 17 at 7:02
GrahamGraham
2,77678
2,77678
add a comment |
add a comment |
$begingroup$
Japt -S
, 10 bytes
Convinced there has to be a shorter approach (and I was right) but this'll do for now.
¸ËhJDg)hDÌ
Try it
¸ËhJDg)hDÌ :Implicit input of string
¸ :Split on spaces
Ë :Map each D
h : Set the character at
J : Index -1 to
Dg : The first character in D
) : End set
h : Set the first character to
DÌ : The last character in D
:Implicit output, joined by spaces
$endgroup$
$begingroup$
Much shorter than my 12 byter:¸®Ì+Zs1J +Zg
$endgroup$
– Embodiment of Ignorance
May 17 at 3:07
$begingroup$
@EmbodimentofIgnorance, that's where I started, too, but it would have failed on single character words. You could save a byte on that, though, with¸®ÎiZÌ+Zs1J
.
$endgroup$
– Shaggy
May 17 at 8:01
1
$begingroup$
@EmbodimentofIgnorance Found a 7 byter
$endgroup$
– Oliver
May 17 at 17:21
add a comment |
$begingroup$
Japt -S
, 10 bytes
Convinced there has to be a shorter approach (and I was right) but this'll do for now.
¸ËhJDg)hDÌ
Try it
¸ËhJDg)hDÌ :Implicit input of string
¸ :Split on spaces
Ë :Map each D
h : Set the character at
J : Index -1 to
Dg : The first character in D
) : End set
h : Set the first character to
DÌ : The last character in D
:Implicit output, joined by spaces
$endgroup$
$begingroup$
Much shorter than my 12 byter:¸®Ì+Zs1J +Zg
$endgroup$
– Embodiment of Ignorance
May 17 at 3:07
$begingroup$
@EmbodimentofIgnorance, that's where I started, too, but it would have failed on single character words. You could save a byte on that, though, with¸®ÎiZÌ+Zs1J
.
$endgroup$
– Shaggy
May 17 at 8:01
1
$begingroup$
@EmbodimentofIgnorance Found a 7 byter
$endgroup$
– Oliver
May 17 at 17:21
add a comment |
$begingroup$
Japt -S
, 10 bytes
Convinced there has to be a shorter approach (and I was right) but this'll do for now.
¸ËhJDg)hDÌ
Try it
¸ËhJDg)hDÌ :Implicit input of string
¸ :Split on spaces
Ë :Map each D
h : Set the character at
J : Index -1 to
Dg : The first character in D
) : End set
h : Set the first character to
DÌ : The last character in D
:Implicit output, joined by spaces
$endgroup$
Japt -S
, 10 bytes
Convinced there has to be a shorter approach (and I was right) but this'll do for now.
¸ËhJDg)hDÌ
Try it
¸ËhJDg)hDÌ :Implicit input of string
¸ :Split on spaces
Ë :Map each D
h : Set the character at
J : Index -1 to
Dg : The first character in D
) : End set
h : Set the first character to
DÌ : The last character in D
:Implicit output, joined by spaces
edited May 17 at 18:04
answered May 16 at 21:39
ShaggyShaggy
19.8k31768
19.8k31768
$begingroup$
Much shorter than my 12 byter:¸®Ì+Zs1J +Zg
$endgroup$
– Embodiment of Ignorance
May 17 at 3:07
$begingroup$
@EmbodimentofIgnorance, that's where I started, too, but it would have failed on single character words. You could save a byte on that, though, with¸®ÎiZÌ+Zs1J
.
$endgroup$
– Shaggy
May 17 at 8:01
1
$begingroup$
@EmbodimentofIgnorance Found a 7 byter
$endgroup$
– Oliver
May 17 at 17:21
add a comment |
$begingroup$
Much shorter than my 12 byter:¸®Ì+Zs1J +Zg
$endgroup$
– Embodiment of Ignorance
May 17 at 3:07
$begingroup$
@EmbodimentofIgnorance, that's where I started, too, but it would have failed on single character words. You could save a byte on that, though, with¸®ÎiZÌ+Zs1J
.
$endgroup$
– Shaggy
May 17 at 8:01
1
$begingroup$
@EmbodimentofIgnorance Found a 7 byter
$endgroup$
– Oliver
May 17 at 17:21
$begingroup$
Much shorter than my 12 byter:
¸®Ì+Zs1J +Zg
$endgroup$
– Embodiment of Ignorance
May 17 at 3:07
$begingroup$
Much shorter than my 12 byter:
¸®Ì+Zs1J +Zg
$endgroup$
– Embodiment of Ignorance
May 17 at 3:07
$begingroup$
@EmbodimentofIgnorance, that's where I started, too, but it would have failed on single character words. You could save a byte on that, though, with
¸®ÎiZÌ+Zs1J
.$endgroup$
– Shaggy
May 17 at 8:01
$begingroup$
@EmbodimentofIgnorance, that's where I started, too, but it would have failed on single character words. You could save a byte on that, though, with
¸®ÎiZÌ+Zs1J
.$endgroup$
– Shaggy
May 17 at 8:01
1
1
$begingroup$
@EmbodimentofIgnorance Found a 7 byter
$endgroup$
– Oliver
May 17 at 17:21
$begingroup$
@EmbodimentofIgnorance Found a 7 byter
$endgroup$
– Oliver
May 17 at 17:21
add a comment |
$begingroup$
sed, 64 bytes
sed -E 's/b([[:alpha:]])([[:alpha:]]*)([[:alpha:]])b/321/g'
$endgroup$
$begingroup$
Sure, we could use.
instead of[[:alpha:]]
, but it would actually have to be[^ ]
, which reduces it to 43, but breaks on punctuation and such. Using[a-zA-Z]
brings it up to 55, by which point I'm just hankering after those sweet, sweet human readable entities...
$endgroup$
– Rich
May 18 at 1:08
2
$begingroup$
You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, separated by a delimiter, all of a uniform case.
In other words, you don't need to worry about punctuation "breaking" your code and can just safely go for[^ ]
;)
$endgroup$
– Value Ink
May 18 at 1:15
$begingroup$
@ValueInk Yeah, but then[^ ]
should be[^[:space:]]
which brings it to 67 chars.
$endgroup$
– Rich
May 20 at 19:17
$begingroup$
"a delimiter" means you can ensure that the delimiter is always a regular space. Who uses tabs in a sentence anyways??
$endgroup$
– Value Ink
May 20 at 21:24
$begingroup$
Okay. Seems like "code golf" is a game where you're meant to find ways to get a drone to drop the ball in the hole instead of actually doing the work. Thanks for the shitty welcome.
$endgroup$
– Rich
May 23 at 2:32
|
show 3 more comments
$begingroup$
sed, 64 bytes
sed -E 's/b([[:alpha:]])([[:alpha:]]*)([[:alpha:]])b/321/g'
$endgroup$
$begingroup$
Sure, we could use.
instead of[[:alpha:]]
, but it would actually have to be[^ ]
, which reduces it to 43, but breaks on punctuation and such. Using[a-zA-Z]
brings it up to 55, by which point I'm just hankering after those sweet, sweet human readable entities...
$endgroup$
– Rich
May 18 at 1:08
2
$begingroup$
You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, separated by a delimiter, all of a uniform case.
In other words, you don't need to worry about punctuation "breaking" your code and can just safely go for[^ ]
;)
$endgroup$
– Value Ink
May 18 at 1:15
$begingroup$
@ValueInk Yeah, but then[^ ]
should be[^[:space:]]
which brings it to 67 chars.
$endgroup$
– Rich
May 20 at 19:17
$begingroup$
"a delimiter" means you can ensure that the delimiter is always a regular space. Who uses tabs in a sentence anyways??
$endgroup$
– Value Ink
May 20 at 21:24
$begingroup$
Okay. Seems like "code golf" is a game where you're meant to find ways to get a drone to drop the ball in the hole instead of actually doing the work. Thanks for the shitty welcome.
$endgroup$
– Rich
May 23 at 2:32
|
show 3 more comments
$begingroup$
sed, 64 bytes
sed -E 's/b([[:alpha:]])([[:alpha:]]*)([[:alpha:]])b/321/g'
$endgroup$
sed, 64 bytes
sed -E 's/b([[:alpha:]])([[:alpha:]]*)([[:alpha:]])b/321/g'
answered May 18 at 1:08
RichRich
1614
1614
$begingroup$
Sure, we could use.
instead of[[:alpha:]]
, but it would actually have to be[^ ]
, which reduces it to 43, but breaks on punctuation and such. Using[a-zA-Z]
brings it up to 55, by which point I'm just hankering after those sweet, sweet human readable entities...
$endgroup$
– Rich
May 18 at 1:08
2
$begingroup$
You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, separated by a delimiter, all of a uniform case.
In other words, you don't need to worry about punctuation "breaking" your code and can just safely go for[^ ]
;)
$endgroup$
– Value Ink
May 18 at 1:15
$begingroup$
@ValueInk Yeah, but then[^ ]
should be[^[:space:]]
which brings it to 67 chars.
$endgroup$
– Rich
May 20 at 19:17
$begingroup$
"a delimiter" means you can ensure that the delimiter is always a regular space. Who uses tabs in a sentence anyways??
$endgroup$
– Value Ink
May 20 at 21:24
$begingroup$
Okay. Seems like "code golf" is a game where you're meant to find ways to get a drone to drop the ball in the hole instead of actually doing the work. Thanks for the shitty welcome.
$endgroup$
– Rich
May 23 at 2:32
|
show 3 more comments
$begingroup$
Sure, we could use.
instead of[[:alpha:]]
, but it would actually have to be[^ ]
, which reduces it to 43, but breaks on punctuation and such. Using[a-zA-Z]
brings it up to 55, by which point I'm just hankering after those sweet, sweet human readable entities...
$endgroup$
– Rich
May 18 at 1:08
2
$begingroup$
You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, separated by a delimiter, all of a uniform case.
In other words, you don't need to worry about punctuation "breaking" your code and can just safely go for[^ ]
;)
$endgroup$
– Value Ink
May 18 at 1:15
$begingroup$
@ValueInk Yeah, but then[^ ]
should be[^[:space:]]
which brings it to 67 chars.
$endgroup$
– Rich
May 20 at 19:17
$begingroup$
"a delimiter" means you can ensure that the delimiter is always a regular space. Who uses tabs in a sentence anyways??
$endgroup$
– Value Ink
May 20 at 21:24
$begingroup$
Okay. Seems like "code golf" is a game where you're meant to find ways to get a drone to drop the ball in the hole instead of actually doing the work. Thanks for the shitty welcome.
$endgroup$
– Rich
May 23 at 2:32
$begingroup$
Sure, we could use
.
instead of [[:alpha:]]
, but it would actually have to be [^ ]
, which reduces it to 43, but breaks on punctuation and such. Using [a-zA-Z]
brings it up to 55, by which point I'm just hankering after those sweet, sweet human readable entities...$endgroup$
– Rich
May 18 at 1:08
$begingroup$
Sure, we could use
.
instead of [[:alpha:]]
, but it would actually have to be [^ ]
, which reduces it to 43, but breaks on punctuation and such. Using [a-zA-Z]
brings it up to 55, by which point I'm just hankering after those sweet, sweet human readable entities...$endgroup$
– Rich
May 18 at 1:08
2
2
$begingroup$
You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, separated by a delimiter, all of a uniform case.
In other words, you don't need to worry about punctuation "breaking" your code and can just safely go for [^ ]
;)$endgroup$
– Value Ink
May 18 at 1:15
$begingroup$
You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, separated by a delimiter, all of a uniform case.
In other words, you don't need to worry about punctuation "breaking" your code and can just safely go for [^ ]
;)$endgroup$
– Value Ink
May 18 at 1:15
$begingroup$
@ValueInk Yeah, but then
[^ ]
should be [^[:space:]]
which brings it to 67 chars.$endgroup$
– Rich
May 20 at 19:17
$begingroup$
@ValueInk Yeah, but then
[^ ]
should be [^[:space:]]
which brings it to 67 chars.$endgroup$
– Rich
May 20 at 19:17
$begingroup$
"a delimiter" means you can ensure that the delimiter is always a regular space. Who uses tabs in a sentence anyways??
$endgroup$
– Value Ink
May 20 at 21:24
$begingroup$
"a delimiter" means you can ensure that the delimiter is always a regular space. Who uses tabs in a sentence anyways??
$endgroup$
– Value Ink
May 20 at 21:24
$begingroup$
Okay. Seems like "code golf" is a game where you're meant to find ways to get a drone to drop the ball in the hole instead of actually doing the work. Thanks for the shitty welcome.
$endgroup$
– Rich
May 23 at 2:32
$begingroup$
Okay. Seems like "code golf" is a game where you're meant to find ways to get a drone to drop the ball in the hole instead of actually doing the work. Thanks for the shitty welcome.
$endgroup$
– Rich
May 23 at 2:32
|
show 3 more comments
$begingroup$
sed, 34 bytes
And presumably the pattern idea will work with most RE tools (and I do know there are differences between standard RE and extended RE).
s,b(w)(w*)(w)b,321,g
Try it online!
$endgroup$
1
$begingroup$
Welcome to PPCG! Methinks that the greedy nature of regex matches means you can cut theb
from the match: Try it online!
$endgroup$
– Value Ink
May 20 at 21:29
$begingroup$
Agreed @ValueInk -- but I was being accurate with the match. Removing theb
will lead to 30 bytes.
$endgroup$
– PJF
May 21 at 14:45
$begingroup$
Use -E for advanced regex and you can use unescaped parentheses. Use.
for the swapped chars and you can lose another two chars. This brings yours down to 26 bytes; one of the smallest readable solutions.s,b(.)(w*)(.)b,321,g
$endgroup$
– Rich
May 24 at 0:01
1
$begingroup$
- nope, I'm wrong, you need thew
s at the ends.s,b(w)(w*)(w)b,321,g
28 chars.
$endgroup$
– Rich
May 24 at 0:13
$begingroup$
Nice work @rich, but as I said I know about standard and extended RE. I just chose to make it standard and readable. The anchors would be required which I neglected to mention in my reply to ValueInk.
$endgroup$
– PJF
May 24 at 13:28
|
show 1 more comment
$begingroup$
sed, 34 bytes
And presumably the pattern idea will work with most RE tools (and I do know there are differences between standard RE and extended RE).
s,b(w)(w*)(w)b,321,g
Try it online!
$endgroup$
1
$begingroup$
Welcome to PPCG! Methinks that the greedy nature of regex matches means you can cut theb
from the match: Try it online!
$endgroup$
– Value Ink
May 20 at 21:29
$begingroup$
Agreed @ValueInk -- but I was being accurate with the match. Removing theb
will lead to 30 bytes.
$endgroup$
– PJF
May 21 at 14:45
$begingroup$
Use -E for advanced regex and you can use unescaped parentheses. Use.
for the swapped chars and you can lose another two chars. This brings yours down to 26 bytes; one of the smallest readable solutions.s,b(.)(w*)(.)b,321,g
$endgroup$
– Rich
May 24 at 0:01
1
$begingroup$
- nope, I'm wrong, you need thew
s at the ends.s,b(w)(w*)(w)b,321,g
28 chars.
$endgroup$
– Rich
May 24 at 0:13
$begingroup$
Nice work @rich, but as I said I know about standard and extended RE. I just chose to make it standard and readable. The anchors would be required which I neglected to mention in my reply to ValueInk.
$endgroup$
– PJF
May 24 at 13:28
|
show 1 more comment
$begingroup$
sed, 34 bytes
And presumably the pattern idea will work with most RE tools (and I do know there are differences between standard RE and extended RE).
s,b(w)(w*)(w)b,321,g
Try it online!
$endgroup$
sed, 34 bytes
And presumably the pattern idea will work with most RE tools (and I do know there are differences between standard RE and extended RE).
s,b(w)(w*)(w)b,321,g
Try it online!
answered May 19 at 15:40
PJFPJF
713
713
1
$begingroup$
Welcome to PPCG! Methinks that the greedy nature of regex matches means you can cut theb
from the match: Try it online!
$endgroup$
– Value Ink
May 20 at 21:29
$begingroup$
Agreed @ValueInk -- but I was being accurate with the match. Removing theb
will lead to 30 bytes.
$endgroup$
– PJF
May 21 at 14:45
$begingroup$
Use -E for advanced regex and you can use unescaped parentheses. Use.
for the swapped chars and you can lose another two chars. This brings yours down to 26 bytes; one of the smallest readable solutions.s,b(.)(w*)(.)b,321,g
$endgroup$
– Rich
May 24 at 0:01
1
$begingroup$
- nope, I'm wrong, you need thew
s at the ends.s,b(w)(w*)(w)b,321,g
28 chars.
$endgroup$
– Rich
May 24 at 0:13
$begingroup$
Nice work @rich, but as I said I know about standard and extended RE. I just chose to make it standard and readable. The anchors would be required which I neglected to mention in my reply to ValueInk.
$endgroup$
– PJF
May 24 at 13:28
|
show 1 more comment
1
$begingroup$
Welcome to PPCG! Methinks that the greedy nature of regex matches means you can cut theb
from the match: Try it online!
$endgroup$
– Value Ink
May 20 at 21:29
$begingroup$
Agreed @ValueInk -- but I was being accurate with the match. Removing theb
will lead to 30 bytes.
$endgroup$
– PJF
May 21 at 14:45
$begingroup$
Use -E for advanced regex and you can use unescaped parentheses. Use.
for the swapped chars and you can lose another two chars. This brings yours down to 26 bytes; one of the smallest readable solutions.s,b(.)(w*)(.)b,321,g
$endgroup$
– Rich
May 24 at 0:01
1
$begingroup$
- nope, I'm wrong, you need thew
s at the ends.s,b(w)(w*)(w)b,321,g
28 chars.
$endgroup$
– Rich
May 24 at 0:13
$begingroup$
Nice work @rich, but as I said I know about standard and extended RE. I just chose to make it standard and readable. The anchors would be required which I neglected to mention in my reply to ValueInk.
$endgroup$
– PJF
May 24 at 13:28
1
1
$begingroup$
Welcome to PPCG! Methinks that the greedy nature of regex matches means you can cut the
b
from the match: Try it online!$endgroup$
– Value Ink
May 20 at 21:29
$begingroup$
Welcome to PPCG! Methinks that the greedy nature of regex matches means you can cut the
b
from the match: Try it online!$endgroup$
– Value Ink
May 20 at 21:29
$begingroup$
Agreed @ValueInk -- but I was being accurate with the match. Removing the
b
will lead to 30 bytes.$endgroup$
– PJF
May 21 at 14:45
$begingroup$
Agreed @ValueInk -- but I was being accurate with the match. Removing the
b
will lead to 30 bytes.$endgroup$
– PJF
May 21 at 14:45
$begingroup$
Use -E for advanced regex and you can use unescaped parentheses. Use
.
for the swapped chars and you can lose another two chars. This brings yours down to 26 bytes; one of the smallest readable solutions. s,b(.)(w*)(.)b,321,g
$endgroup$
– Rich
May 24 at 0:01
$begingroup$
Use -E for advanced regex and you can use unescaped parentheses. Use
.
for the swapped chars and you can lose another two chars. This brings yours down to 26 bytes; one of the smallest readable solutions. s,b(.)(w*)(.)b,321,g
$endgroup$
– Rich
May 24 at 0:01
1
1
$begingroup$
- nope, I'm wrong, you need the
w
s at the ends. s,b(w)(w*)(w)b,321,g
28 chars.$endgroup$
– Rich
May 24 at 0:13
$begingroup$
- nope, I'm wrong, you need the
w
s at the ends. s,b(w)(w*)(w)b,321,g
28 chars.$endgroup$
– Rich
May 24 at 0:13
$begingroup$
Nice work @rich, but as I said I know about standard and extended RE. I just chose to make it standard and readable. The anchors would be required which I neglected to mention in my reply to ValueInk.
$endgroup$
– PJF
May 24 at 13:28
$begingroup$
Nice work @rich, but as I said I know about standard and extended RE. I just chose to make it standard and readable. The anchors would be required which I neglected to mention in my reply to ValueInk.
$endgroup$
– PJF
May 24 at 13:28
|
show 1 more comment
$begingroup$
Ruby, 53 bytes
gets.split(" ").map{|z|print z[-1]+z[1..-2]+z[0]," "}
I tried it without regex. The output prints each word on a new line. If that's against the rules, let me know and I'll fix it.
Ungolfed:
gets.split(" ").map {|z|
print z[-1] + z[1..-2] + z[0], " "
}
$endgroup$
$begingroup$
Welcome to PPCG! Printing each word on a new line should be fine, but your old solution ofp
was no good because that added quotes to the output. You could always useputs
instead since that one auto-appends the newline and is shorter thanprint
! Also, if you callsplit
with no arguments it automatically splits on spaces.
$endgroup$
– Value Ink
May 23 at 4:06
add a comment |
$begingroup$
Ruby, 53 bytes
gets.split(" ").map{|z|print z[-1]+z[1..-2]+z[0]," "}
I tried it without regex. The output prints each word on a new line. If that's against the rules, let me know and I'll fix it.
Ungolfed:
gets.split(" ").map {|z|
print z[-1] + z[1..-2] + z[0], " "
}
$endgroup$
$begingroup$
Welcome to PPCG! Printing each word on a new line should be fine, but your old solution ofp
was no good because that added quotes to the output. You could always useputs
instead since that one auto-appends the newline and is shorter thanprint
! Also, if you callsplit
with no arguments it automatically splits on spaces.
$endgroup$
– Value Ink
May 23 at 4:06
add a comment |
$begingroup$
Ruby, 53 bytes
gets.split(" ").map{|z|print z[-1]+z[1..-2]+z[0]," "}
I tried it without regex. The output prints each word on a new line. If that's against the rules, let me know and I'll fix it.
Ungolfed:
gets.split(" ").map {|z|
print z[-1] + z[1..-2] + z[0], " "
}
$endgroup$
Ruby, 53 bytes
gets.split(" ").map{|z|print z[-1]+z[1..-2]+z[0]," "}
I tried it without regex. The output prints each word on a new line. If that's against the rules, let me know and I'll fix it.
Ungolfed:
gets.split(" ").map {|z|
print z[-1] + z[1..-2] + z[0], " "
}
edited May 21 at 18:46
answered May 21 at 3:43
8333883338
212
212
$begingroup$
Welcome to PPCG! Printing each word on a new line should be fine, but your old solution ofp
was no good because that added quotes to the output. You could always useputs
instead since that one auto-appends the newline and is shorter thanprint
! Also, if you callsplit
with no arguments it automatically splits on spaces.
$endgroup$
– Value Ink
May 23 at 4:06
add a comment |
$begingroup$
Welcome to PPCG! Printing each word on a new line should be fine, but your old solution ofp
was no good because that added quotes to the output. You could always useputs
instead since that one auto-appends the newline and is shorter thanprint
! Also, if you callsplit
with no arguments it automatically splits on spaces.
$endgroup$
– Value Ink
May 23 at 4:06
$begingroup$
Welcome to PPCG! Printing each word on a new line should be fine, but your old solution of
p
was no good because that added quotes to the output. You could always use puts
instead since that one auto-appends the newline and is shorter than print
! Also, if you call split
with no arguments it automatically splits on spaces.$endgroup$
– Value Ink
May 23 at 4:06
$begingroup$
Welcome to PPCG! Printing each word on a new line should be fine, but your old solution of
p
was no good because that added quotes to the output. You could always use puts
instead since that one auto-appends the newline and is shorter than print
! Also, if you call split
with no arguments it automatically splits on spaces.$endgroup$
– Value Ink
May 23 at 4:06
add a comment |
$begingroup$
8088 Assembly, IBM PC DOS, 39 38 bytes
$ xxd pwas.com
00000000: d1ee ac8a c8fd 03f1 c604 244e 8bfe ac3c ..........$N...<
00000010: 2075 098a 2586 6402 8825 8bfe e2f0 b409 u..%.d..%......
00000020: ba82 00cd 21c3
Unassembled:
D1 EE SHR SI, 1 ; point SI to DOS PSP (080H)
AC LODSB ; load string length into AL
8A C8 MOV CL, AL ; load string length into CX for loop
FD STD ; set LODSB to decrement
03 F1 ADD SI, CX ; point SI to end of string
C6 04 24 MOV BYTE PTR[SI], '$' ; put a '$' DOS string terminator at end
4E DEC SI ; start at last char of word
8B FE MOV DI, SI ; point DI to last char of word
CHR_LOOP:
AC LODSB ; load next (previous?) char into AL
3C 20 CMP AL, ' ' ; is it a space?
75 0A JNE END_CHR ; if so, continue loop
8A 25 MOV AH, [DI] ; put last char in AH
86 64 02 XCHG AH, [SI][2] ; swap memory contents of first char with last
; (unfortunately XCHG cannot swap mem to mem)
88 25 MOV [DI], AH ; put first char value into last char position
8B FE MOV DI, SI ; point DI last char of word
END_CHR:
E2 EF LOOP CHR_LOOP ; continue loop
B4 09 MOV AH, 9 ; DOS display string function
BA 0082 MOV DX, 082H ; output string is at memory address 82H
CD 21 INT 21H ; display string to screen
C3 RET ; return to DOS
Standalone PC DOS executable. Input via command line args, output to screen.
Download and test PWAS.COM.
$endgroup$
add a comment |
$begingroup$
8088 Assembly, IBM PC DOS, 39 38 bytes
$ xxd pwas.com
00000000: d1ee ac8a c8fd 03f1 c604 244e 8bfe ac3c ..........$N...<
00000010: 2075 098a 2586 6402 8825 8bfe e2f0 b409 u..%.d..%......
00000020: ba82 00cd 21c3
Unassembled:
D1 EE SHR SI, 1 ; point SI to DOS PSP (080H)
AC LODSB ; load string length into AL
8A C8 MOV CL, AL ; load string length into CX for loop
FD STD ; set LODSB to decrement
03 F1 ADD SI, CX ; point SI to end of string
C6 04 24 MOV BYTE PTR[SI], '$' ; put a '$' DOS string terminator at end
4E DEC SI ; start at last char of word
8B FE MOV DI, SI ; point DI to last char of word
CHR_LOOP:
AC LODSB ; load next (previous?) char into AL
3C 20 CMP AL, ' ' ; is it a space?
75 0A JNE END_CHR ; if so, continue loop
8A 25 MOV AH, [DI] ; put last char in AH
86 64 02 XCHG AH, [SI][2] ; swap memory contents of first char with last
; (unfortunately XCHG cannot swap mem to mem)
88 25 MOV [DI], AH ; put first char value into last char position
8B FE MOV DI, SI ; point DI last char of word
END_CHR:
E2 EF LOOP CHR_LOOP ; continue loop
B4 09 MOV AH, 9 ; DOS display string function
BA 0082 MOV DX, 082H ; output string is at memory address 82H
CD 21 INT 21H ; display string to screen
C3 RET ; return to DOS
Standalone PC DOS executable. Input via command line args, output to screen.
Download and test PWAS.COM.
$endgroup$
add a comment |
$begingroup$
8088 Assembly, IBM PC DOS, 39 38 bytes
$ xxd pwas.com
00000000: d1ee ac8a c8fd 03f1 c604 244e 8bfe ac3c ..........$N...<
00000010: 2075 098a 2586 6402 8825 8bfe e2f0 b409 u..%.d..%......
00000020: ba82 00cd 21c3
Unassembled:
D1 EE SHR SI, 1 ; point SI to DOS PSP (080H)
AC LODSB ; load string length into AL
8A C8 MOV CL, AL ; load string length into CX for loop
FD STD ; set LODSB to decrement
03 F1 ADD SI, CX ; point SI to end of string
C6 04 24 MOV BYTE PTR[SI], '$' ; put a '$' DOS string terminator at end
4E DEC SI ; start at last char of word
8B FE MOV DI, SI ; point DI to last char of word
CHR_LOOP:
AC LODSB ; load next (previous?) char into AL
3C 20 CMP AL, ' ' ; is it a space?
75 0A JNE END_CHR ; if so, continue loop
8A 25 MOV AH, [DI] ; put last char in AH
86 64 02 XCHG AH, [SI][2] ; swap memory contents of first char with last
; (unfortunately XCHG cannot swap mem to mem)
88 25 MOV [DI], AH ; put first char value into last char position
8B FE MOV DI, SI ; point DI last char of word
END_CHR:
E2 EF LOOP CHR_LOOP ; continue loop
B4 09 MOV AH, 9 ; DOS display string function
BA 0082 MOV DX, 082H ; output string is at memory address 82H
CD 21 INT 21H ; display string to screen
C3 RET ; return to DOS
Standalone PC DOS executable. Input via command line args, output to screen.
Download and test PWAS.COM.
$endgroup$
8088 Assembly, IBM PC DOS, 39 38 bytes
$ xxd pwas.com
00000000: d1ee ac8a c8fd 03f1 c604 244e 8bfe ac3c ..........$N...<
00000010: 2075 098a 2586 6402 8825 8bfe e2f0 b409 u..%.d..%......
00000020: ba82 00cd 21c3
Unassembled:
D1 EE SHR SI, 1 ; point SI to DOS PSP (080H)
AC LODSB ; load string length into AL
8A C8 MOV CL, AL ; load string length into CX for loop
FD STD ; set LODSB to decrement
03 F1 ADD SI, CX ; point SI to end of string
C6 04 24 MOV BYTE PTR[SI], '$' ; put a '$' DOS string terminator at end
4E DEC SI ; start at last char of word
8B FE MOV DI, SI ; point DI to last char of word
CHR_LOOP:
AC LODSB ; load next (previous?) char into AL
3C 20 CMP AL, ' ' ; is it a space?
75 0A JNE END_CHR ; if so, continue loop
8A 25 MOV AH, [DI] ; put last char in AH
86 64 02 XCHG AH, [SI][2] ; swap memory contents of first char with last
; (unfortunately XCHG cannot swap mem to mem)
88 25 MOV [DI], AH ; put first char value into last char position
8B FE MOV DI, SI ; point DI last char of word
END_CHR:
E2 EF LOOP CHR_LOOP ; continue loop
B4 09 MOV AH, 9 ; DOS display string function
BA 0082 MOV DX, 082H ; output string is at memory address 82H
CD 21 INT 21H ; display string to screen
C3 RET ; return to DOS
Standalone PC DOS executable. Input via command line args, output to screen.
Download and test PWAS.COM.
edited May 24 at 18:42
answered May 24 at 18:05
gwaughgwaugh
3,2161723
3,2161723
add a comment |
add a comment |
$begingroup$
Perl 5 -p
, 24 bytes
s/(w)(w*)(w)/$3$2$1/g
Try it online!
$endgroup$
$begingroup$
You can get it to 21 by using the newline as separator: Try it online!
$endgroup$
– wastl
May 17 at 17:38
add a comment |
$begingroup$
Perl 5 -p
, 24 bytes
s/(w)(w*)(w)/$3$2$1/g
Try it online!
$endgroup$
$begingroup$
You can get it to 21 by using the newline as separator: Try it online!
$endgroup$
– wastl
May 17 at 17:38
add a comment |
$begingroup$
Perl 5 -p
, 24 bytes
s/(w)(w*)(w)/$3$2$1/g
Try it online!
$endgroup$
Perl 5 -p
, 24 bytes
s/(w)(w*)(w)/$3$2$1/g
Try it online!
answered May 16 at 20:29
XcaliXcali
6,058523
6,058523
$begingroup$
You can get it to 21 by using the newline as separator: Try it online!
$endgroup$
– wastl
May 17 at 17:38
add a comment |
$begingroup$
You can get it to 21 by using the newline as separator: Try it online!
$endgroup$
– wastl
May 17 at 17:38
$begingroup$
You can get it to 21 by using the newline as separator: Try it online!
$endgroup$
– wastl
May 17 at 17:38
$begingroup$
You can get it to 21 by using the newline as separator: Try it online!
$endgroup$
– wastl
May 17 at 17:38
add a comment |
$begingroup$
Batch, 141 bytes
@set t=
@for %%w in (%*)do @call:c %%w
@echo%t%
@exit/b
:c
@set s=%1
@if not %s%==%s:~,1% set s=%s:~-1%%s:~1,-1%%s:~,1%
@set t=%t% %s%
Takes input as command-line parameters. String manipulation is dire in Batch at best, and having to special-case single-letter words doesn't help.
$endgroup$
add a comment |
$begingroup$
Batch, 141 bytes
@set t=
@for %%w in (%*)do @call:c %%w
@echo%t%
@exit/b
:c
@set s=%1
@if not %s%==%s:~,1% set s=%s:~-1%%s:~1,-1%%s:~,1%
@set t=%t% %s%
Takes input as command-line parameters. String manipulation is dire in Batch at best, and having to special-case single-letter words doesn't help.
$endgroup$
add a comment |
$begingroup$
Batch, 141 bytes
@set t=
@for %%w in (%*)do @call:c %%w
@echo%t%
@exit/b
:c
@set s=%1
@if not %s%==%s:~,1% set s=%s:~-1%%s:~1,-1%%s:~,1%
@set t=%t% %s%
Takes input as command-line parameters. String manipulation is dire in Batch at best, and having to special-case single-letter words doesn't help.
$endgroup$
Batch, 141 bytes
@set t=
@for %%w in (%*)do @call:c %%w
@echo%t%
@exit/b
:c
@set s=%1
@if not %s%==%s:~,1% set s=%s:~-1%%s:~1,-1%%s:~,1%
@set t=%t% %s%
Takes input as command-line parameters. String manipulation is dire in Batch at best, and having to special-case single-letter words doesn't help.
answered May 17 at 0:20
NeilNeil
85.2k845183
85.2k845183
add a comment |
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 90 bytes
n=>n.Split().Any(x=>WriteLine(x.Length<2?x:x.Last()+x.Substring(1,x.Length-2)+x[0])is int)
Uses newline as delimiter, though really any whitespace can be used.
Try it online!
$endgroup$
$begingroup$
SelectMany (that is, map and flatten) for 84 bytes, but outputs a single trailing space. Try it online!
$endgroup$
– someone
May 17 at 8:17
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 90 bytes
n=>n.Split().Any(x=>WriteLine(x.Length<2?x:x.Last()+x.Substring(1,x.Length-2)+x[0])is int)
Uses newline as delimiter, though really any whitespace can be used.
Try it online!
$endgroup$
$begingroup$
SelectMany (that is, map and flatten) for 84 bytes, but outputs a single trailing space. Try it online!
$endgroup$
– someone
May 17 at 8:17
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 90 bytes
n=>n.Split().Any(x=>WriteLine(x.Length<2?x:x.Last()+x.Substring(1,x.Length-2)+x[0])is int)
Uses newline as delimiter, though really any whitespace can be used.
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 90 bytes
n=>n.Split().Any(x=>WriteLine(x.Length<2?x:x.Last()+x.Substring(1,x.Length-2)+x[0])is int)
Uses newline as delimiter, though really any whitespace can be used.
Try it online!
edited May 17 at 4:53
answered May 17 at 4:47
Embodiment of IgnoranceEmbodiment of Ignorance
4,096128
4,096128
$begingroup$
SelectMany (that is, map and flatten) for 84 bytes, but outputs a single trailing space. Try it online!
$endgroup$
– someone
May 17 at 8:17
add a comment |
$begingroup$
SelectMany (that is, map and flatten) for 84 bytes, but outputs a single trailing space. Try it online!
$endgroup$
– someone
May 17 at 8:17
$begingroup$
SelectMany (that is, map and flatten) for 84 bytes, but outputs a single trailing space. Try it online!
$endgroup$
– someone
May 17 at 8:17
$begingroup$
SelectMany (that is, map and flatten) for 84 bytes, but outputs a single trailing space. Try it online!
$endgroup$
– someone
May 17 at 8:17
add a comment |
$begingroup$
Icon, 76 bytes
link segment
procedure f(s)
w:=!seglist(s,' ')&w[1]:=:w[-1]&writes(w)&x
end
Try it online!
$endgroup$
add a comment |
$begingroup$
Icon, 76 bytes
link segment
procedure f(s)
w:=!seglist(s,' ')&w[1]:=:w[-1]&writes(w)&x
end
Try it online!
$endgroup$
add a comment |
$begingroup$
Icon, 76 bytes
link segment
procedure f(s)
w:=!seglist(s,' ')&w[1]:=:w[-1]&writes(w)&x
end
Try it online!
$endgroup$
Icon, 76 bytes
link segment
procedure f(s)
w:=!seglist(s,' ')&w[1]:=:w[-1]&writes(w)&x
end
Try it online!
answered May 17 at 6:57
Galen IvanovGalen Ivanov
8,51711237
8,51711237
add a comment |
add a comment |
$begingroup$
Java, 110 109 bytes
-1 bytes by using a newline for a delimeter
s->{int l;for(var i:s.split("n"))System.out.println(i.charAt(l=i.length()-1)+i.substring(1,l)+i.charAt(0));}
TIO
$endgroup$
$begingroup$
Does this work for single-letter words?
$endgroup$
– Neil
May 17 at 0:19
$begingroup$
@Neil no because I'm bad. I'll fix later.
$endgroup$
– Benjamin Urquhart
May 17 at 0:20
$begingroup$
109 by using newline as delimiter
$endgroup$
– Embodiment of Ignorance
May 17 at 4:55
add a comment |
$begingroup$
Java, 110 109 bytes
-1 bytes by using a newline for a delimeter
s->{int l;for(var i:s.split("n"))System.out.println(i.charAt(l=i.length()-1)+i.substring(1,l)+i.charAt(0));}
TIO
$endgroup$
$begingroup$
Does this work for single-letter words?
$endgroup$
– Neil
May 17 at 0:19
$begingroup$
@Neil no because I'm bad. I'll fix later.
$endgroup$
– Benjamin Urquhart
May 17 at 0:20
$begingroup$
109 by using newline as delimiter
$endgroup$
– Embodiment of Ignorance
May 17 at 4:55
add a comment |
$begingroup$
Java, 110 109 bytes
-1 bytes by using a newline for a delimeter
s->{int l;for(var i:s.split("n"))System.out.println(i.charAt(l=i.length()-1)+i.substring(1,l)+i.charAt(0));}
TIO
$endgroup$
Java, 110 109 bytes
-1 bytes by using a newline for a delimeter
s->{int l;for(var i:s.split("n"))System.out.println(i.charAt(l=i.length()-1)+i.substring(1,l)+i.charAt(0));}
TIO
edited May 17 at 11:31
answered May 16 at 21:57
Benjamin UrquhartBenjamin Urquhart
1,072121
1,072121
$begingroup$
Does this work for single-letter words?
$endgroup$
– Neil
May 17 at 0:19
$begingroup$
@Neil no because I'm bad. I'll fix later.
$endgroup$
– Benjamin Urquhart
May 17 at 0:20
$begingroup$
109 by using newline as delimiter
$endgroup$
– Embodiment of Ignorance
May 17 at 4:55
add a comment |
$begingroup$
Does this work for single-letter words?
$endgroup$
– Neil
May 17 at 0:19
$begingroup$
@Neil no because I'm bad. I'll fix later.
$endgroup$
– Benjamin Urquhart
May 17 at 0:20
$begingroup$
109 by using newline as delimiter
$endgroup$
– Embodiment of Ignorance
May 17 at 4:55
$begingroup$
Does this work for single-letter words?
$endgroup$
– Neil
May 17 at 0:19
$begingroup$
Does this work for single-letter words?
$endgroup$
– Neil
May 17 at 0:19
$begingroup$
@Neil no because I'm bad. I'll fix later.
$endgroup$
– Benjamin Urquhart
May 17 at 0:20
$begingroup$
@Neil no because I'm bad. I'll fix later.
$endgroup$
– Benjamin Urquhart
May 17 at 0:20
$begingroup$
109 by using newline as delimiter
$endgroup$
– Embodiment of Ignorance
May 17 at 4:55
$begingroup$
109 by using newline as delimiter
$endgroup$
– Embodiment of Ignorance
May 17 at 4:55
add a comment |
$begingroup$
Haskell, 75 74 bytes
Fixed a bug pointed at by Cubic and also golfed down 1 byte.
f=unwords.map(#v).words
x#g=g(r$tail x)++[x!!0]
r=reverse
v=
v x=r$x#r
Try it online!
$endgroup$
$begingroup$
map g
is shorter than(g<$>)
$endgroup$
– Cubic
May 17 at 10:17
1
$begingroup$
Also, if you look at your test case you'll see it doesn't work for one letter words, it turnsa
intoaa
$endgroup$
– Cubic
May 17 at 10:20
add a comment |
$begingroup$
Haskell, 75 74 bytes
Fixed a bug pointed at by Cubic and also golfed down 1 byte.
f=unwords.map(#v).words
x#g=g(r$tail x)++[x!!0]
r=reverse
v=
v x=r$x#r
Try it online!
$endgroup$
$begingroup$
map g
is shorter than(g<$>)
$endgroup$
– Cubic
May 17 at 10:17
1
$begingroup$
Also, if you look at your test case you'll see it doesn't work for one letter words, it turnsa
intoaa
$endgroup$
– Cubic
May 17 at 10:20
add a comment |
$begingroup$
Haskell, 75 74 bytes
Fixed a bug pointed at by Cubic and also golfed down 1 byte.
f=unwords.map(#v).words
x#g=g(r$tail x)++[x!!0]
r=reverse
v=
v x=r$x#r
Try it online!
$endgroup$
Haskell, 75 74 bytes
Fixed a bug pointed at by Cubic and also golfed down 1 byte.
f=unwords.map(#v).words
x#g=g(r$tail x)++[x!!0]
r=reverse
v=
v x=r$x#r
Try it online!
edited May 17 at 13:18
answered May 17 at 8:56
Max YekhlakovMax Yekhlakov
5917
5917
$begingroup$
map g
is shorter than(g<$>)
$endgroup$
– Cubic
May 17 at 10:17
1
$begingroup$
Also, if you look at your test case you'll see it doesn't work for one letter words, it turnsa
intoaa
$endgroup$
– Cubic
May 17 at 10:20
add a comment |
$begingroup$
map g
is shorter than(g<$>)
$endgroup$
– Cubic
May 17 at 10:17
1
$begingroup$
Also, if you look at your test case you'll see it doesn't work for one letter words, it turnsa
intoaa
$endgroup$
– Cubic
May 17 at 10:20
$begingroup$
map g
is shorter than (g<$>)
$endgroup$
– Cubic
May 17 at 10:17
$begingroup$
map g
is shorter than (g<$>)
$endgroup$
– Cubic
May 17 at 10:17
1
1
$begingroup$
Also, if you look at your test case you'll see it doesn't work for one letter words, it turns
a
into aa
$endgroup$
– Cubic
May 17 at 10:20
$begingroup$
Also, if you look at your test case you'll see it doesn't work for one letter words, it turns
a
into aa
$endgroup$
– Cubic
May 17 at 10:20
add a comment |
$begingroup$
Scala, 100 bytes
(b:String,c:String)=>b.split(c)map(f=>f.tail.lastOption++:(f.drop(1).dropRight(1)+f.head))mkString c
$endgroup$
add a comment |
$begingroup$
Scala, 100 bytes
(b:String,c:String)=>b.split(c)map(f=>f.tail.lastOption++:(f.drop(1).dropRight(1)+f.head))mkString c
$endgroup$
add a comment |
$begingroup$
Scala, 100 bytes
(b:String,c:String)=>b.split(c)map(f=>f.tail.lastOption++:(f.drop(1).dropRight(1)+f.head))mkString c
$endgroup$
Scala, 100 bytes
(b:String,c:String)=>b.split(c)map(f=>f.tail.lastOption++:(f.drop(1).dropRight(1)+f.head))mkString c
answered May 17 at 13:18
SoapySoapy
1717
1717
add a comment |
add a comment |
$begingroup$
T-SQL, 126 bytes
SELECT STRING_AGG(STUFF(STUFF(value,1,1,RIGHT(value,1)),LEN(value),1,LEFT(value,1)),' ')
FROM STRING_SPLIT((SELECT*FROM t),' ')
Input is via a pre-existing table t with varchar field v, per our IO standards.
Reading from back to front, STRING_SPLIT
breaks a string into individual rows via a delimiter, STUFF
modifies the characters at the specified positions, then STRING_AGG
mashes them back together again.
$endgroup$
add a comment |
$begingroup$
T-SQL, 126 bytes
SELECT STRING_AGG(STUFF(STUFF(value,1,1,RIGHT(value,1)),LEN(value),1,LEFT(value,1)),' ')
FROM STRING_SPLIT((SELECT*FROM t),' ')
Input is via a pre-existing table t with varchar field v, per our IO standards.
Reading from back to front, STRING_SPLIT
breaks a string into individual rows via a delimiter, STUFF
modifies the characters at the specified positions, then STRING_AGG
mashes them back together again.
$endgroup$
add a comment |
$begingroup$
T-SQL, 126 bytes
SELECT STRING_AGG(STUFF(STUFF(value,1,1,RIGHT(value,1)),LEN(value),1,LEFT(value,1)),' ')
FROM STRING_SPLIT((SELECT*FROM t),' ')
Input is via a pre-existing table t with varchar field v, per our IO standards.
Reading from back to front, STRING_SPLIT
breaks a string into individual rows via a delimiter, STUFF
modifies the characters at the specified positions, then STRING_AGG
mashes them back together again.
$endgroup$
T-SQL, 126 bytes
SELECT STRING_AGG(STUFF(STUFF(value,1,1,RIGHT(value,1)),LEN(value),1,LEFT(value,1)),' ')
FROM STRING_SPLIT((SELECT*FROM t),' ')
Input is via a pre-existing table t with varchar field v, per our IO standards.
Reading from back to front, STRING_SPLIT
breaks a string into individual rows via a delimiter, STUFF
modifies the characters at the specified positions, then STRING_AGG
mashes them back together again.
answered May 17 at 15:23
BradCBradC
4,024623
4,024623
add a comment |
add a comment |
1 2
next
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f185674%2fpwas-eht-tirsf-dna-tasl-setterl-fo-hace-dorw%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
$begingroup$
How should punctuation be treated?
Hello, world!
becomes,elloH !orldw
(swapping punctuation as a letter) oroellH, dorlw!
(keeping punctuation in place)?$endgroup$
– Phelype Oleinik
May 16 at 19:23
3
$begingroup$
@PhelypeOleinik You do not need to handle punctuation; all of the inputs will only consist of the letters a through z, and all a uniform case.
$endgroup$
– Comrade SparklePony
May 16 at 19:24
4
$begingroup$
Second paragraph reads as well as one other character to use as a delimiter while the fourth reads separated by spaces. Which one is it?
$endgroup$
– Adám
May 16 at 19:46
$begingroup$
@Adám Any non-alphabetic character. I’ll edit to clarify.
$endgroup$
– Comrade SparklePony
May 16 at 21:36
1
$begingroup$
@BenjaminUrquhart Yes. You can take input as a function argument if you want as well.
$endgroup$
– Comrade SparklePony
May 16 at 21:44