Twisting Sentences
Jelly, 15 14 8 bytes
A whopping 6 byte save from Dennis (by moving the flatten and mould inside link 1 there is no need to split into two and head, and a flatten is there already so they become one!)
żṚFṁ
Ç€Ç
(from the two lines: żṚFœs2Ḣ
, and Ç€ÇFṁ⁸
)
Try it online!
Takes an array of words and returns an array of new words. (The Footer at TIO calls this and joins the array with spaces so it prints out nicely.)
Note - handling a single string, splitting on tabs spaces and newlines, then reassembling was actually proving rather tricky; once I saw that a list of words was an option things got a lot easier!
How?
Ç€Ç - Main link: list of words
Ç€ - call the last link (1) as a monad for €ach word
Ç - call the last link (1) as a monad for the result
żṚFṁ - Link 1: Do a twist: list (here, a list of words or characters)
e.g. input = [A,B,C,D,E,F,G]
ż - zip the list with [A, B, C, D, E, F, G]
Ṛ - the reverse of the list [G, F, E, D, C, B, A]
[[A,G],[B,F],[C,E],[D,D],[E,C],[F,B],[G,A]]
F - flatten into a single list [A,G, B,F, C,E, D,D, E,C, F,B, G,A]
[A,G,B,F,C,E,D,D,E,C,F,B,G,A]
ṁ - mould like the input list [A,G,B,F,C,E,D]
JavaScript (ES6), 89 bytes
Takes and outputs an array of words.
a=>a.map(w=>(F=([a,...b])=>a?a+(b.pop()||'')+F(b):'')(a.map(F)).slice(p,p+=w.length),p=0)
Test
let f =
a=>a.map(w=>(F=([a,...b])=>a?a+(b.pop()||'')+F(b):'')(a.map(F)).slice(p,p+=w.length),p=0)
console.log(
JSON.stringify(
f(["The","quick","brown","fox","jumps","over","the","lazy","dog."])
)
);
String version, 112 bytes
Takes and outputs a string.
s=>s.replace(/\S+/g,w=>(F=([a,...b])=>a?a+(b.pop()||'')+F(b):'')(s.split(/\s/).map(F)).slice(p,p+=w.length),p=0)
Test
let f =
s=>s.replace(/\S+/g,w=>(F=([a,...b])=>a?a+(b.pop()||'')+F(b):'')(s.split(/\s/).map(F)).slice(p,p+=w.length),p=0)
console.log(f(`The quick brown fox jumps over the lazy dog.`))
console.log(f(`The quick brown fox jumps
over the lazy dog.`))
console.log(f(`Aflack`))
Perl, 77 bytes
74 bytes of code + 3 bytes for -0pa
flags.
map{s/.\K/chop/ge,s/../chop/reg}@F;s/\S/($r||=$F[$i++].pop@F)=~s%.%%,$&/ge
Try it online!
Saved 8 bytes thanks to an old @Ton Hospel's post where I "stole" s/../chop/reg
. (I previously had $c=y///c/2,s/.{$c}$//
)