Code Golf: Forwards sdrawkcaB sdrawkcaB Forwards Forwards sdrawkcaB
TeaScript, 55 bytes 58 60 69 76 78 80 87 89
xO`a-z `?xl(#~-i&2?l:lv(),/ +/):Ld`SÀZn Û § «e Ò5s`
This is extremely short, I'm very happy with it.
The last ~20 characters may seem like gibberish but that's "Sentence must only use letters" encoded. All the characters have char codes below 256 so each are one byte
Explanation
xO`a-z `? // If input contains only a-z and space...
xl(# // Loop through input
~-i&2? // If (index - 1 "unary and"ed with 2) isn't 0...
:l, // Leave alone
lv() // Otherwise, reverse string
/ +/ // Loops on spaces
)
:Ld`SÀZn Û § «e Ò5s` // Otherwise... decompress and print the error string
- Try it online
- Pastebin
Haskell, 141 bytes
r=reverse
f x|all(`elem`(' ':['a'..'z']++['A'..'Z']))x=unwords$zipWith($)(cycle[id,r,r,id])$words x|1<2=error"Sentence must only use letters"
Almost 2/3 of the code is for error checking. Seems to be the first real world challenge.
The work is done by unwords$zipWith($)(cycle[id,reverse,reverse,id])$words x
which splits the input into a list of words, zips it with the cycling list of functions [id,reverse,reverse,id,id,reverse...]
and joins the result with spaces back to a single string.
Thanks to @Christian Irwan for 2 bytes.
JavaScript (ES6) 122
f=s=>/[^a-z ]/i.test(s)?"Sentence must only use letters":s.split(/ +/).map((w,i)=>~-i&2?w:[...w].reverse().join``).join` `
alert(f(prompt('?','She sells Sea shells on the Sea shore')))