# Tag Info

31

vim, 43 36 35 bytes VGrx:sor G:let &tw=col("$") uu:%ce Too good not to post. Note the trailing newline; it is significant. Thanks to @Marth for saving a character! vim-friendly format: VGrx:sor<cr>G:let &tw=col("$")<cr>uu:%ce<cr> Explanation: VGrx replace every character with an "x" :sor<cr> ...

23

Mathematica, 96 bytes StringRiffle[#~StringPadLeft~Floor[Max@(l=StringLength)@a/2+l@#/2]&/@(a=#~StringSplit~" ")," "]& Don't ask me how it worked, I just fiddled with it until it produced the correct output.

22

APL (Dyalog Classic), 4 bytes ⊢∘⌽\ The input is a vector of character vectors. ⌽ is a function that reverses a vector (when ⌽ is applied monadically). ⊢ is "dex" - a function that returns its right argument. When composed (∘) with another function f it forces the latter to be monadic as A ⊢∘f B is equivalent to A ⊢ (f B) and therefore f B. \ is the scan ...

15

Pyth, 19 17 bytes 2 bytes thanks to Jakube V.ztr+1.[l.T.zNd6 Demonstration I think this is the first time the center-pad function of .[ has been useful. The length of the longest line is found using non-truncating transpose (.T). Trailing spaces are removed by adding a non-space character to the front, stripping spaces, then removing the added character....

13

Haskell, 26 bytes zipWith($)l l=id:reverse:l Try it online! Usage example: zipWith($)l ["abc","def","ghi"] yields ["abc","fed","ghi"]. Explanation: l is an infinite list of functions alternating between the identity function and the reverse function. The main function zips l and the input list with the function application $, that is for an input ["abc",... 12 Ruby 162 160 158 152 160 157 (demo) i=gets+' ' (69..s=r=89).map{|c|w=i.scan(/(.{1,#{c}}\S) /).flatten m=(0..c).map{|i|w.map{|l|l[i]}+[?x]}.join.scan(/ +/).map(&:size).max m<s&&(s=m;r=w)} puts r The un-golfed version: input = gets+' ' result = '' (69..smallest_max=89).each{|w| #split text into words of at most w characters wrap = (input+... 12 Husk, 4 bytes z*İ_ Takes and returns a list of strings (the interpreter implicitly joins the result by newlines before printing). The first string is reversed. Try it online! Explanation z*İ_ Implicit input. İ_ The infinite list [-1,1,-1,1,-1,1.. z Zip with input * using multiplication. In Husk, multiplying a string with a number repeats it ... 11 Funciton, non-competitive This challenge has highlighted the sore lack of a “maximum value” (and minimum value) function for lazy sequences, so... I added them to the core library (they’re called ⊤ and ⊥, respectively). Therefore I didn’t bother to submit this as a golfed answer (it would have to include the ⊤ function declaration to be valid), so here’s ... 10 Octave, 233 225 216 213 bytes o=@ones;l=z=o(5,1);for k=input('')-47;l=[l,reshape(dec2bin([448,22558,8514,10560,3936,2376,328,15840,320,2368](k),15),5,[])-48,z];end;L=~o(size(l)+2);L(2:6,2:end-1)=l;O=o(3);O(5)=-8;M=~conv2(kron(L,o(25)),O);imshow(M) Here the first test case (from a resized screen capture, s.t. it fits my monitor=): o=@ones; l=z=o(5,1); ... 9 Retina, 58 bytes (?<!\n)\n(?!\n) <space> - (?! |[A-Z][a-z])| (?= )|(?<=-) (?=[A-Z]) <empty> <space> represents a single space on its own line and <empty> represents an empty trailing line. For counting purposes, each line goes into a separate file and the \n are replaced with actual linefeed characters. For convenience you can ... 9 Retina, 54 52 bytes +m^(.)+$(?<=(?=[^\t]*^..(?<-1>.)+(?(1)^))[^\t]*) $0 The \ts can replaced with actual tabs, but I've used \t here, because otherwise SE will convert the tabs to spaces. Note that there is a leading space on the second line. Try it online. Explanation The basic idea is to match a line which is at least two characters ... 9 GNU sed -r, 621 bytes Takes input as two lines: The width as a unary number first and the string second. I'm certain this could be golfed much more but I've already dumped way too much time into it. x;N G s/\n/!@/ : /@\n/bZ s/-!(.*)@ /\1 !@/ s/!(.*[- ])(@.*1)$/\1!\2/ s/@(.)(.*)1$/\1@\2/ s/-!(.*-)(@.*)\n$/\1!\2\n1/ s/(\n!@) /\1/ s/-!(.* )(@.*)\n$/\1!\2\n1/ ... 8 APL (105) {∊{1↓∊⍵,3⊃⎕TC}¨⊃G/⍨V=⌊/V←{⌈/≢¨⊂⍨¨↓⍉2≠⌿+\↑≢¨¨⍵}¨G←(K⊂⍨' '=K←' ',⍵)∘{×⍴⍺:(⊂z/⍺),⍵∇⍨⍺/⍨~z←⍵>+\≢¨⍺⋄⍺}¨70+⍳21} Explanation: (K⊂⍨' '=K←' ',⍵): Add a space in front of ⍵, then split ⍵ on the spaces. Each word retains the space it begins with. ∘{...}¨70+⍳21: with that value, for each number in the range [71, 91]: (Because of the way the words are ... 8 Bash+coreutils, 236 157 bytes Edited with a different approach - quite a bit shorter than before: a=(for i in {71..91};{ for((b=1;b++<i;));{ fold -s$i<<<$@|cut -b$b|uniq -c|sort -nr|grep -m1 "[0-9] " }|sort -nr|sed q }|nl -v71|sort -nk2) fold -s$a<<<$@ Reads the input string from the command line. With 3 nested sorts, I ...

8

JavaScript (ES6), 218 bytes w=>s=>s.map((c,i)=>c.map((p,j)=>(k+p)[l="length"]-w-(b=!i|j>0)+(j<c[l]-1)<0?k+=b?p:" "+p:(Array(w-k[l]-b).fill(h=k.split ).map((_,i)=>h[i%(h[l]-1)]+=" "),o.push(h.join +(b?"-":"")),k=p)),o=[],k="")&&o.join + +k Takes arguments in currying syntax (f(width)(text)) and the text input is in ...

8

JavaScript (ES6), 147 bytes Takes input as (width)(text). w=>F=(s,p=S=' ')=>(g=([c,...b],o='',h=c=='-')=>c?o[w-1]?c==S&&o+ +F(b):o[w+~h]?o+c+ +F(b):c>S?g(b,h?o:o+c):g(b,o+p)||g(b,o+p+c):o)(s)||F(s,p+S) Try it online! Commented w => // w = requested width F = ( // F is a ...

7

CJam, 116 106 99 84 77 72 bytes l:X;93,72>{:D;OOXS/{S+_2$+,D<{+}{@@);a+\}?}/a+}%{z'K*S/:!0a/1fb$W=}$0=N* Takes the single line input and prints the correct output to STDOUT. UPDATE : Improved a lot and removed redundant loops by doing all calculations in the sorting loop itself. Also fixed a bug in river length calculation. Explanation soon (after I ... 7 CJam, 26 23 19 bytes qN/_z,f{1$,m2/S*\N} My first time using CJam! Four bytes saved thanks to Martin Büttner. Try it online. Explanation qN/ e# Read input and split each line _z, e# Transpose a copy and get its length to find the longest line f{ e# For each line... 1,- e# Subtract its length from the longest length 2/ e# Divide by two to ... 7 Jolf, 3 bytes Noncompeting, update postdates question. Try it here!. pci pc center i string input ¯\_(ツ)_/¯ I thought it'd be a useful function. 7 JavaScript (ES6), 93 91 bytes s=>(m=l=s.split ).map(x=>(v=x.length/2)<m?v:m=v).map((x,i)=>" ".repeat(m-x)+l[i]).join  2 bytes saved thanks to @edc65! Explanation s=>( m= // m = max line length (divided by 2) l=s.split ) // l = array of lines .map(x=> // for each line (v=x.... 7 Octave, 168 bytes for i=1:(n=nnz(s='CATION MEANS FASTER CODING MEANS FEWER CODERS MEANS EASIER COMMUNI')),text(sin(i/n*2*pi),cos(i/n*2*pi),s(i),'rotation',-i/n*360);axis([-2,2,-2,2]);end Try it here 7 Mathematica, 153 Bytes x=Pi/33;Graphics@Map[StringSplit["CATION MEANS FASTER CODING MEANS FEWER CODERS MEANS EASIER COMMUNI",""][[#/x]]~Text~{Sin@#,Cos@#}~Rotate~-#&,Range@66x] Increasing it to 178 bytes lets it rotate: x=Pi/33;Graphics@Map[StringSplit["CATION MEANS FASTER CODING MEANS FEWER CODERS MEANS EASIER COMMUNI",""][[#/x-a]]~Text~{Sin@#,Cos@#}~... 7 JavaScript (ES6), Firefox, 43 bytes This version abuses the sort algorithm of Firefox. It generates garbage on Chrome and doesn't alter the strings at all on Edge. a=>a.map((s,i)=>[...s].sort(_=>i&1).join) Test cases let f = a=>a.map((s,i)=>[...s].sort(_=>i&1).join`) console.log(f(["Here are some lines","of text for ... 6 Python 2, 83 81 bytes def f(s): t=s.split('\n') for y in t:print(max(len(f)for f in t)-len(y))/2*' '+y Thanks to @xnor for saving 2 chars example input: f("""Programming Puzzles & Code Golf""") example output: Programming Puzzles & Code Golf And ending in second place with 84 bytes using str.center() ... 6 LabVIEW, 3 or 35 LabVIEW Primitives Finds lines until none are left, then calculates how many spaces to add and puts everything together. Alternatively you could use the built-in center alignment on string indicators, it kinda does feel like cheating though. 6 Html+JavaScript ES6, 352 Test running the snippet below <canvas id=C></canvas><script>s=prompt(),C.width=-~s.length*80,c=C.getContext("2d"),[...s].map(d=>[30,d*=3,++d,++d].map(w=a=>{for(a=parseInt("vhvivgtlnllv74vnltvlt11vvlvnlv0"[a],36)*2+1,p=1,y=100,i=64;i>>=1;p=b,y-=20)c.moveTo(x+20,y),b=a&i?1:0,c[b-p?'lineTo':'... 6 HTML and JS, 13 + 170 = 284 227 192 186 183 bytes 41 bytes saved thanks to @Shaggy (x=c.getContext("2d")).translate(r=c.height/2,r) for(i in t="CATION MEANS FASTER CODING MEANS FEWER CODERS MEANS EASIER COMMUNI"){x.rotate(2/21);x.fillText(t[i],0,-r*.8)} <canvas id=c height=250 width=250> The width and height set to 250 to allow better ... 6 Haskell, 27 bytes foldr((.map reverse).(:))[] Try it online! Haskell, 30 bytes f(a:b:c)=a:reverse b:f c f a=a Try it online! Recursion FTW. 6 Perl 5, 17 + 2 (-pl) = 19 bytes odd lines reversed_=reverse if$.%2 even lines reversed$_=reverse if\$|-- After @Martin's comment : input needs to have a trailing linefeed. try it online

6

APL (Dyalog Unicode), 10 bytes ⌽¨@{2|⍳≢⍵} Works both ways: Try it online! with ⎕IO←1 Try it online! with ⎕IO←0 How it works: ⌽¨@{2|⍳≢⍵} ⍝ tacit prefix fn { ≢⍵} ⍝ Length of the input ⍳ ⍝ generate indexes from 1 (or 0 with ⎕IO←0) 2| ⍝ mod 2; this generates a boolean vector of 0s (falsy) and 1s (truthy) @ ⍝ apply to the truthy ...

Only top voted, non community-wiki answers of a minimum length are eligible