Print a string with vertical words

12

Your task is to take an input string of ascii characters and output the string as a series of vertical words separated by spaces. An example is shown below:

Given the string:

Hello, World! My name is Foo.

the output should be:

H W M n i F
e o y a s o
l r   m   o
l l   e   .
o d
, !

10 bonus points will be awarded if your program correctly handles strings which need to wrap-around the terminal, which we'll set at 80 characters.

50 points if your program can also do the reverse!

Foo Barrigno

Posted 2014-02-21T12:15:12.337

Reputation: 223

Quite similar to Transpose a page of text.

– manatwork – 2014-02-21T12:29:19.590

3@manatwork: I suppose it is. It's not identical though - and I might argue my problem is a little easier. – Foo Barrigno – 2014-02-21T12:31:44.887

It's not 100% identical, but it's close enough to count as a duplicate: the reduction to make it identical is just replacing each space with two newlines. – Peter Taylor – 2014-02-21T14:04:11.620

1@PeterTaylor: Not quite. My problem has no requirement to respect newlines in the original string. That problem requires that new lines be converted into spaces, and spaces converted into two newlines. It's not quite a trivial reduction. – Foo Barrigno – 2014-02-21T14:09:32.180

Answers

10

J, 15

|:>'\S+| 'rxall

Usage:

   |:>'\S+| 'rxall 'Hello, World! My name is Foo.'
H W M n i F
e o y a s o
l r   m   o
l l   e   .
o d        
, !        

grc

Posted 2014-02-21T12:15:12.337

Reputation: 18 565

5

Javascript - 228 172 145 126

A=prompt().split(" "),B="",N=A;for(y=0;y<N.length;y++){for(i=0;i<N.length;i++){if(A[i][y]){B+=A[i][y];}else{B+=" ";}}B+="\n";}

My first code golf :)

Wolle Vanillebär Lutz

Posted 2014-02-21T12:15:12.337

Reputation: 256

Its great for your first attempt! – Foo Barrigno – 2014-02-21T12:54:31.180

You should attempt to make your code as short as possible, for instance, remove spaces, also "Input ?" doesn't really affect the program's behaviour, remove it too. – mniip – 2014-02-21T12:54:53.370

Fixed Bugs. Should work as expected :) – Wolle Vanillebär Lutz – 2014-02-21T13:11:34.163

1

Works correctly now. But some minor things: no need for variable N, store the array's length instead of asking it twice, some pointless braces, some unnecessary semicolons. A=prompt().split(" "),B="";for(y=0;y<(l=A.length);y++){for(i=0;i<l;i++)if(A[i][y])B+=A[i][y];else B+="_";B+="\n"}alert(B). (In the JavaScript Standards for IO meta question the mostly agreed opinion was that relying on REPL's implicit output should not be considered correct.)

– manatwork – 2014-02-21T13:28:20.873

hello world, gave me hw\neo. – Danny – 2014-02-21T16:10:37.773

Doesn't work for I love JavaScript – Florent – 2014-02-21T16:43:00.747

1(untested) if(A[i][y]){B+=A[i][y];}else{B+=" ";} => B+=(x=A[i][y])?x:" " – daniero – 2014-02-25T16:04:55.063

5

APL, 22

{⍉⊃⍵⊂⍨1+0,+\2≠/⍵=' '}

Explanation

{              ⍵=' '}   A. check which chars are spaces           
            2≠/         B. of that vector, which consecutive pairs are different  
          +\            C. compute the partial sums                           
      1+0,              D. insert 0 at the front and add 1 to every item
   ⍵⊂⍨                     use this vector to split the original string
 ⍉⊃                        disclose into a matrix and transpose

    'W o w   S u c h   D o g e'
A.   0 0 0 1 0 0 0 0 1 0 0 0 0
B.    0 0 1 1 0 0 0 1 1 0 0 0
C.    0 0 1 2 2 2 2 3 4 4 4 4
D.  1 1 1 2 3 3 3 3 4 5 5 5 5

Example

      {⍉⊃⍵⊂⍨1+0,+\2≠/⍵=' '} 'Hello, World! My name is Foo.'
H W M n i F
e o y a s o
l r   m   o
l l   e   .
o d        
, !        

Tobia

Posted 2014-02-21T12:15:12.337

Reputation: 5 455

3

Javascript 184 149 123

var a=s.split(" "),b="",c;for(c in a)b+="<div style='float:left'>"+a[c].split("").join("<br>")+"</div>";document.write(b);

With the example string defined:

var s = "Hello, World! My name is Foo.";
var a=s.split(" "),b="",c;for(c in a)b+="<div style='float:left'>"+a[c].split("").join("<br>")+"</div>";document.write(b);

You can copy the second statement to you browsers console.

Unminified:

var res = "Hello, World! My name is Foo.";
var t=res.split(" ");
var s ="";
for (var word in t){
    s+="<div style='float:left'>" + t[word].split("").join("<br />") + "</div>";
}
document.write(s);

JsFiddle Link: http://jsfiddle.net/FzMvK/

My first code golf post :P

RononDex

Posted 2014-02-21T12:15:12.337

Reputation: 274

Very nicely done – Foo Barrigno – 2014-02-21T13:16:45.860

@FooBarrigno updated answer to support reverse – RononDex – 2014-02-21T13:28:03.480

@FooBarrigno Updated answer, removed reverse support and changed logic completely to reduce byte count – RononDex – 2014-02-21T15:15:33.150

Clever, I like it. Can't you just change to float:right for reverse? – Danny – 2014-02-21T16:26:07.990

2Depends what "reverse" means. If the first letter should be at the bottom then it wont work. If it is simply reversing the words, then it should work yes – RononDex – 2014-02-21T16:57:20.443

@RononDex for some reason I thought reverse meant writing it backwards. but you are probably right about the letters being on the bottom. I feel stupid now... – Danny – 2014-02-21T17:15:50.950

@RononDex I was wondering if you really needed the var declaration ... – Gaurang Tandon – 2014-02-22T05:01:40.253

3

Ruby, 91 87

s=gets.split
puts s.map{|x|x.ljust(s.map(&:size).max,' ').split''}.transpose.map &:join

Hooray, I beat Perl! :D

Ruby, 150 - 50 bonus = 100

s=$<.read
n=s.index"
"
s=s.split n ?'
':' '
o=s.map{|x|x.ljust(s.map(&:size).max,' ').split''}.transpose.map &:join
puts n ?o.map(&:strip).join(' '):o

It just detects for newlines, and applies special handling if they are detected.

Run it like

ruby transposegolf.rb < transposegolfinput.txt

Doorknob

Posted 2014-02-21T12:15:12.337

Reputation: 68 138

2

05AB1E, score 8 -20 -21 (30 29 bytes - 50 bonus):

|Dgi#ζεSðý}»}ë¹ε2ô€¬}ζJεðÜ}ðý

Try it online (regular single-line input).
Try it online (multi-line reversed input).

Explanation:

|                     # Take the input split on newlines:
                      #  i.e. 'This is a test' → ['This is a test']
                      #  i.e. 'T i a t\nh s   e\ni     s\ns     t'
                      #    → ['T i a t','h s   e','i     s','s     t']
 Dg                   #  Duplicate this list, and take the length
                      #   i.e. ['This is a test'] → 1
                      #   i.e. ['T i a t','h s   e','i     s','s     t'] → 4
   i         }        # If the length is exactly 1:
    ¹                 #  Take the input again 
     #                #  Split the input-string by spaces
                      #   i.e. 'This is a test' → ['This','is','a','test']
      ζ               #  Zip with space-filler: Swap all rows and columns
                      #   i.e. ['This','is','a','test'] → ['Tiat','hs e','i  s','s  t']
       ε   }          #  For each item:
        S             #   Convert the item to a list of characters
                      #    i.e. 'Tiat' → ['T','i','a','t']
         ðý           #   Join them by a single space
                      #    i.e. ['T','i','a','t'] → 'T i a t'
            »         #  Join the result by newlines (and output implicitly)
    ë                 # Else:
     ε    }           #  For each item:
      2ô              #   Split it into chunks of two characters
                      #    i.e. 'h s   e' → ['h ','s ','  ','e']
        €¬            #   Take the head (first character) of each:
                      #    i.e. ['h ','s ','  ','e'] → ['h','s',' ','e']
           ζ          #  Zip with space-filler: Swap all rows and columns
                      #   i.e. [['T','i','a','t'],['h','s',' ','e'],['i',' ',' ','s'],['s',' ',' ','t']]
                      #     → [['T','h','i','s'],['i','s',' ',' '],['a',' ',' ',' '],['t','e','s','t']]
            J         #  Join each inner list to a single string
                      #   i.e. [['T','h','i','s'],['i','s',' ',' '],['a',' ',' ',' '],['t','e','s','t']]
                      #     → ['This','is  ','a   ','test']
             ε  }     #  For each item:
              ðÜ      #   Remove any trailing spaces
                      #    i.e. 'is  ' → 'is'
                 ðý   #  Join the items by a single space (and output implicitly)

Original 8-byte answer:

#ζεSðý}»

Try it online.

Explanation:

#           # Split the input-string by spaces
            #  i.e. 'This is a test' → ['This','is','a','test']
 ζ          # Zip with space-filler: Swap all rows and columns
            #  i.e. ['This','is','a','test'] → ['Tiat','hs e','i  s','s  t']
  ε   }     # For each item:
   S        #  Convert the item to a list of characters
            #   i.e. 'Tiat' → ['T','i','a','t']
    ðý      #  Join them by a single space
            #   i.e. ['T','i','a','t'] → 'T i a t'
       »    # Join the result by newlines (and output implicitly)

Kevin Cruijssen

Posted 2014-02-21T12:15:12.337

Reputation: 67 575

2

Perl - 92 97

$_=<>;chop;@x=split$";do{print((substr$_,$q,1or$").$")for@x;$q++,print$/}while/@{['\S'x$q]}/

Does the job in a pretty straightforward way.

mniip

Posted 2014-02-21T12:15:12.337

Reputation: 9 396

No need for parenthesis around statement modifiers' expressions.

– manatwork – 2014-02-21T12:45:13.593

Note that while as used here is also statement modifier. – manatwork – 2014-02-21T12:51:38.457

Is it? Is it not a do{}while() loop? – mniip – 2014-02-21T12:55:52.330

Nope. do itself has nothing else, just a block. The while is a separate thing.

– manatwork – 2014-02-21T13:00:14.397

2

K, 33

{" "/:',:''x@'/:!max@#:'x:" "\:x}

Example input and output:

k){" "/:',:''x@'/:!max@#:'x:" "\:x}"Hello, World! My name is Foo."
"H W M n i F"
"e o y a s o"
"l r   m   o"
"l l   e   ."
"o d        "
", !        "

tmartin

Posted 2014-02-21T12:15:12.337

Reputation: 3 917

Are the " supposed to be there? – Dr. belisarius – 2014-02-21T13:34:05.443

@belisarius It's how strings are represented in k. If you specifically want to write to stdout then you can with {-1@" "/:',:''x@'/:!max@#:'x:" "\:x;} (37 chars), which would produce the output without " – tmartin – 2014-02-21T14:00:39.047

4Well, I think the output should be the required one, notwithstanding the language – Dr. belisarius – 2014-02-21T14:02:03.780

2

Python 2.7 - 137 112 bytes

s=raw_input().split()
for c in range(max(map(len,s))):
 for w in s:
    try:print w[c],
    except:print' ',
 print''

Someone else has already done it better, but I might as well throw this up. Adds spaces to each word in the input until it's the same length as the longest one (to avoid index errors for the next part), then prints the cth letter of every word while c goes from 0 to the length of each string.

I came up with a much better way of doing this and cut out 25 bytes. Rather than padding strings with spaces to avoid the index error, I handle the error directly! Whenever there's nothing to print, I print a single space in its place with try:print w[c],, except:print' ',. I also remembered that I don't need a space between a print statement and a string literal, which saved one byte.

Note that Python 2 allows you to mix tabs and spaces and considers them separate levels of indentation. SE's Markdown interpreter replaces a tab character with four spaces, but every line of this program except the first has exactly one byte of indentation.

Formatting was pretty easy, since print 'something', prints 'something ' rather than 'something\n'. I did that for each character, and used an empty print statement to get the newline where I needed it.

undergroundmonorail

Posted 2014-02-21T12:15:12.337

Reputation: 5 897

2

Python:

One line of code to process the sting:

import sys
m = "Hello, World! My name is Foo."

map(lambda y: sys.stdout.write(' '.join(y)+'\n'), zip(*map(lambda x: x.ljust(max(map(len,m.split()))), m.split())))

Kevin

Posted 2014-02-21T12:15:12.337

Reputation: 3 123

2

Python 2.7, 108 103

I'm sure this can be golfed more, but here's an initial solution in python.

w=raw_input().split();m=max(map(len,w));i=0
while i<m:print" ".join(map(lambda x:x.ljust(m)[i],w));i+=1

Improvements:

  • split(" ") => split()
  • removed some extra spaces

scleaver

Posted 2014-02-21T12:15:12.337

Reputation: 507

Nice job! If you start with i=m and loop down to 0, you can shave another 3 characters for an even 100. – DLosc – 2014-06-09T00:30:24.137

2

F#, 187

let o=Console.ReadLine()
let s=o.Split(' ')
let m=s|>Seq.map String.length|>Seq.max
for l=0 to m-1 do
 for w in s|>Seq.map(fun x->x.PadRight(m,' ').[l])do printf"%c "w
 printfn"%s"""

m0sa

Posted 2014-02-21T12:15:12.337

Reputation: 549

2

Ruby, 63

$F.map(&:size).max.times{|i|puts$F.map{|s|s[i]||' '}.join' '}

The algorithm is very straightforward; only golfed. Code is 61 bytes long, plus 2 bytes for the -na options that it needs to work. From ruby -h:

-n   assume 'while gets(); ... end' loop around your script
-a   autosplit mode with -n or -p (splits $_ into $F)

Sample run:

$ echo 'This sentence is false' | ruby -na cols.rb
T s i f
h e s a
i n   l
s t   s
  e   e
  n    
  c    
  e

epidemian

Posted 2014-02-21T12:15:12.337

Reputation: 531

2

C, 111 110 95 90 bytes

This solution uses VT-100 control codes to move the cursor on the terminal

main(int _,char**v){char*s=*++v;printf("^[7");while(*s)' '==*s?(s++,printf("^[8^[[2C^[7")):printf("%c^[[B^H",*s++);}

the ^[ sequence is a placeholder for the single ASCII ESC character, that can't be displayed here.

  • ^[7 saves the current pointer position
  • ^[8 restores the cursor position to the saved position
  • ^[[2C moves 2 steps right
  • ^[[B moves 1 step down

edit 1: the ^[[D (1 step left) VT-100 code has been replaced by a backspace (shown as ^H here, but is only one ASCII char) ; also forgot the "separated by spaces" instruction, now fixed

edit 2:

7 chars saved by using a for loop instead of while, and 32 instead of ' ':

main(int _,char**v){printf("^[7");for(char*s=*++v;*s;s++)32==*s?printf("^[8^[[2C^[7"):printf("%c^[[B^H",*s);}

8 more chars saved by calling one less printf: the ternary ?: operator is now used in the printf parameters

main(int _,char**v){printf("^[7");for(char*s=*++v;*s;s++)printf(32==*s?"^[8^[[2C^[7":"%c^[[B^H",*s);}

edit 3:

Got rid of the local variable s, working directly with argv aka v. This is totally hideous. But saved 4 chars. Also replaced == with ^ and therefore switched the ?: operands, to save 1 more char.

main(int c,char**v){printf("^[7");for(v++;**v;)printf(32^*(*v)++?"%c^[[B^H":"^[8^[[2C^[7",**v);}

Usage

$ gcc transpose.c -o transpose --std=c99
$ ./transpose 'Hello, World! My name is Foo.'
H W M n i F
e o y a s o
l r   m   o
l l   e   .
o d
, !

Un-golfed version (first version)

main (int _,char**v) {
    char*s=*++v; // init s with the address of the first letter
    printf("^[7"); // save the current cursor position
    while(*s) 
        ' '==*s ? ( /* if current char is a space */
            s++,printf("^[8^[[2C^[7") /* return to the saved location, move right, save position */
        ) : /* kind of else */
            printf("%c^[[B^H",*s++); /* print the current char, move down, move left */
}

Un-golfed version (last version)

main(int c,char**v) {
    printf("^[7");
    for(v++;**v;) /* v++: make v point to argv[1] */
        printf(     
            32^*(*v)++? /* test if **v is different from ' ', and make *v point to
                           the next char */
                "%c^[[B^H":         
                "^[8^[[2C^[7",      
            **v); /* this undefined behaviour (using *v and (*v)++ in the same expression)
                     works as "expected" with gcc 4.7.2 */
} 

Coaumdio

Posted 2014-02-21T12:15:12.337

Reputation: 141

1

That un-golfed version looks suspiciously like Befunge. :)

– DLosc – 2014-06-09T00:20:21.503

2

I answered this question a long time ago. It was one of my first contributions to the site, actually. I recently came across it again and was kind of embarrassed. 112 bytes?! Unacceptable. So I gave it another shot:

Python 3 - 92 bytes

s=input().split()
print('\n'.join(map(' '.join,zip(*[a.ljust(max(map(len,s)))for a in s]))))

In the 109 days since I posted that first answer, I like to think I've come a long way. Even something like using Python 3 over 2.71 wouldn't have occurred to me. With this code golfed down to under 100 bytes, my soul can finally rest and I can proceed to the afterlife.

Explanation

s=input().split()

This gets a line from stdin and creates a list by splitting it at whitespace characters. The only whitespace likely to be in the input is spaces, so this line gets a list of words.

Let's take the second line from the inside out:

                                           max(map(len,s))

map takes a function and an iterable as arguments. It applies the function to each element of the iterable, and returns a new iterable of the results. Here, I create an iterable with the lengths of each input word. max gets the maximum value from an iterable. This gets us the longest word in the input.

                                  [a.ljust(              )for a in s]

A list comprehension is similar to map. It does something to every element of an iterable, and returns a list the results. For every word in the input, I do that_word.ljust(some code). ljust is short for "left justify". It takes an integer as an argument and adds spaces to the string until it's that long.

                             zip(*                                    )

This is a neat trick. In this context, * means "unzip this iterable as multiple arguments". This way, zip can be used to transpose a matrix (e.g. zip(*[(1,2),(3,4)]) -> [(1,3),(2,4)]). The only restriction is that all the rows in the matrix have to be the same length, or elements from all rows but the shortest are cut off to match.

                map(' '.join,                                          )

We already know what map does. The only new thing here is join, which takes an iterable of strings and makes it a single string using the delimiter it's attached to. For example, 'a'.join(['I', 'can', 'play', 'the', 'saxophone'])2 becomes Iacanaplayatheasaxophone.

print('\n'.join(                                                        ))

This join takes a bunch of strings and seperates them by newlines. All that's left is to print to stdout and we're done!

All together now:

print('\n'.join(map(' '.join,zip(*[a.ljust(max(map(len,s)))for a in s]))))

Find the length of the longest word from the input, append spaces to every word until they're the same length, transpose with the zip(*3 trick, use join to seperate each character in a row with spaces, join again to seperate each row with a newline, and print! Not bad for the only non-input-handling line in a 92 byte program.


1. The extra characters spent on print()'s parentheses are outweighed by the 4 characters I drop from raw_input()->input().
2. I can't actually play the saxophone.
3. ). You're welcome.

undergroundmonorail

Posted 2014-02-21T12:15:12.337

Reputation: 5 897

You can change print("\n".join(...)) to *map(print,...),. Try it online!

– Jo King – 2018-10-06T10:05:51.277

2I have no idea why this is CW. I might have hit the button by accident. Oh well. – undergroundmonorail – 2014-06-10T18:03:21.317

1

R, 81 bytes

Save a byte by storing a newline as e, which can be used in both scan calls, the compare call and cat.

w=scan(,e,t=scan(,e<-"
"));while(any((s=substr(w,F<-F+1,F))>e))cat(pmax(" ",s),e)

Try it online!

J.Doe

Posted 2014-02-21T12:15:12.337

Reputation: 2 379

1

K (oK), 26 bytes

Solution:

`0:{" "/'+(|/#:'x)$x}@" "\

Try it online!

Explanation:

`0:{" "/'+(|/#:'x)$x}@" "\ / the solution
                      " "\ / split input on whitespace
   {                }@     / apply (@) lambda
                  $x       / pad ($) input (x)
          (      )         / do this together
             #:'x          / count (#:) each (')
           |/              / max
         +                 / transpose / flip
    " "/'                  / join each with whitespace
`0:                        / print to stdout

streetster

Posted 2014-02-21T12:15:12.337

Reputation: 3 635

1

Mathematica 49

Clearly Mathematica isn't the best for this one:

Grid[PadRight@Characters@StringSplit@s^T]/. 0->" "

Mathematica graphics

Note ^T (transpose) is only one char (I can't find the right char code now)

Dr. belisarius

Posted 2014-02-21T12:15:12.337

Reputation: 5 345

1

Javascript, 141

a=prompt().split(' '),c=0,d=a.map(function(a){b=a.length;c=c<b?b:c});for(j=0;j<c;j++){b='';for(i in a)b+=a[i][j]?a[i][j]:' ';console.log(b);}

Sample

hello, world! this is code golf

hwticg
eohsoo
lri dl
lls ef
od    
,! 

Danny

Posted 2014-02-21T12:15:12.337

Reputation: 1 563

1

GolfScript [41 bytes]

' '%:x{.,x{,}%$-1=-abs' '*+}%zip{' '*}%n*

How it works:

' '%:x          split text into words and store array in 'x'
{               for each word in the array:
    .,              from word's length
    x{,}%$-1=-      substract the length of the longest word in 'x'
    abs             get absolute value (maybe there is a shorter way?)
    ' '*+           add corresponding number of spaces
}%
zip{' '*}%      transpose array of words and add spaces between letters
n*              join words with a new line character

You may see the online demo here.

P.S.: This is my first GolfScript code ever, so don't judge me strictly ;)

VisioN

Posted 2014-02-21T12:15:12.337

Reputation: 4 490

1

R, 116

z=t(plyr::rbind.fill.matrix(lapply(strsplit(scan(,""),""),t)));z[is.na(z)]=" ";invisible(apply(cbind(z,"\n"),1,cat))

Sven Hohenstein

Posted 2014-02-21T12:15:12.337

Reputation: 2 464

1

Bash + coreutils, 54

eval paste `printf " <(fold -w1<<<%s)" $@`|expand -t2

Output:

$ ./transpose.sh Hello, World! My name is Foo.
H W M n i F
e o y a s o
l r   m   o
l l   e   .
o d       
, !       
$ 

Digital Trauma

Posted 2014-02-21T12:15:12.337

Reputation: 64 644

Suggestion for an update: backticks for command substitution is depreciated. Using the $() construct is now the common method for command substitution. – Yokai – 2018-07-31T09:23:30.967

@Yokai - This is code-golf - here we are optimizing for code length and not for standards/best-practices compliance. https://codegolf.stackexchange.com/a/25572/11259

– Digital Trauma – 2018-07-31T16:34:35.930

I figured since the norm had changed for command substitution, I would suggest an update. You don't have to. It was just a suggestion. It would only add a single new character to the count anyways. – Yokai – 2018-08-01T08:27:22.413

1

APL: 18

⍉↑1↓¨a⊂⍨' '=a←' ',

Explanation:

a←' ', put a space in front of the string and assign to a

' '= find spaces, produces a boolean

1↓¨a⊂⍨ make substrings starting where the boolean has 1's and drop first element of each (so the space)

⍉↑ Make matrix out of the resulting substrings, and reverse it along the diagonal

Moris Zucca

Posted 2014-02-21T12:15:12.337

Reputation: 1 519

0

Japt -R, 5 bytes

¸y¬m¸

Try it


Explanation

¸         :Split on spaces
 y        :Transpose
  ¬       :Split columns
   m      :Map rows
    ¸     :  Join with spaces
          :Implicitly join with newlines and output

Shaggy

Posted 2014-02-21T12:15:12.337

Reputation: 24 623

0

Pyth, 8 bytes

jbjL\ Cc

Try it online!

Pretty straightforward. Takes input enclosed in quotes, i.e. "Hello World"

jbjL\ CcQ
---------
       cQ    Chop the input Q on spaces
      C      Matrix transpose
  jL\        Join each element by spaces,
             i.e. interleave spaces between the characters of each element
jb           Join by newlines

RK.

Posted 2014-02-21T12:15:12.337

Reputation: 497

1C truncates to the length of the shortest entry, so this doesn't work. Here's a quick fix which is also 8 bytes. – hakr14 – 2018-08-01T04:49:07.650

0

APL(NARS), 79 chars, 158 bytes

{m←⌈/↑∘⍴¨z←(' '≠⍵)⊂,⍵⋄v←∊m{⍵\⍨∊(k⍴1)(0⍴⍨⍺-k←↑⍴⍵)}¨z⋄((2×↑⍴z)⍴1 0)\[2]⍉(↑⍴z)m⍴v}

test:

  f←{m←⌈/↑∘⍴¨z←(' '≠⍵)⊂,⍵⋄v←∊m{⍵\⍨∊(k⍴1)(0⍴⍨⍺-k←↑⍴⍵)}¨z⋄((2×↑⍴z)⍴1 0)\[2]⍉(↑⍴z)m⍴v}
  f 'Hello, World! My name is Foo.'
H W M n i F 
e o y a s o 
l r   m   o 
l l   e   . 
o d         
, !         

The old function have output not perfect:

  {⍪¨(' '≠⍵)⊂,⍵}'Hello, World! My name is Foo.'
 H  W  M  n  i  F 
 e  o  y  a  s  o 
 l  r     m     o 
 l  l     e     . 
 o  d             
 ,  !

RosLuP

Posted 2014-02-21T12:15:12.337

Reputation: 3 036

0

Julia - 109 bytes

i=split(readline());m=maximum(map(x->length(x),i));for c∈1:m println(join([rpad(w,m)[c] for w∈i]," "))end

EricShermanCS

Posted 2014-02-21T12:15:12.337

Reputation: 121

0

Python 2.7 - 119 106

Take 1 - 166. The reversing of lists was needed to make pop work in the order I wanted, but this seemed wasteful. And when I tried to combine into a single comprehension for fun, the pop screwed things up.

w=raw_input().split(' ');l=max([len(l) for l in w]);
q=[list(p)[::-1]for p in w]+[['\n']*l]
t=[' '+(v.pop() if v else' ')for i in range(l)for v in q]
print ''.join(t)

Take 2 - 119. So I changed to simple list indexing. Still seems clunky though, especially the padding of spaces and new lines.

w=raw_input().split(' ');l=max([len(l)for l in w]);print''.join([' '+(v+' '*l)[i]for i in range(l)for v in w+['\n'*l]])

Take 3 - thanks to @grc

w=raw_input().split();l=max(map(len,w));print''.join(' '+(v+' '*l)[i]for i in range(l)for v in w+['\n'*l])

psion5mx

Posted 2014-02-21T12:15:12.337

Reputation: 121

2[len(l)for l in w] can be shortened to map(len,w), .split(' ') to .split(), and .join([...]) to .join(...). – grc – 2014-02-21T13:57:59.770

I haven't gone over your code in too much detail so this might not work, but: "The reversing of lists was needed to make pop work in the order I wanted" Couldn't you use v.pop(0) to pop the first element instead of the last one? – undergroundmonorail – 2014-02-21T14:12:10.153

0

Python 3, 124

a=input().split()
l=max(map(len,a))
print("\n".join(" ".join(c[i] for c in [i+" "*(l-len(i)) for i in a]) for i in range(l)))

gcq

Posted 2014-02-21T12:15:12.337

Reputation: 251

0

Haskell, 112

Golfed:

import Data.List
r s=unlines$transpose$p$words s
p w=m(\s->s++replicate(maximum(m l w)-l s)' ')w
m=map
l=length

Explained:

import Data.List

-- Break on spaces, then pad, then transpose, then join with newlines
r s=unlines$transpose$p$words s

-- Pads each String in a list of String to have the same length as the longest String
p w=m(\s->s++replicate(maximum(m l w)-l s)' ')w

-- Aliases to save space
m=map
l=length

Example:

*Main Data.List> putStrLn $ r "Hello Doge"
HD
eo
lg
le
o

danmcardle

Posted 2014-02-21T12:15:12.337

Reputation: 695

0

JavaScript, 139 (156 with console.log output)

s=" ",b="",a=prompt().split(s),l=0;for(var i in a){m=a[i].length;l=(l<m)?m:l;}for(i=0;i<l;i++){for(var j in a)b+=((x=a[j][i])?x:s)+s;b+="\n"}console.log(b);

I think this is as golfed as I can get it. I just split, find the largest word and transpose accordingly, adding spaces if the char doesn't exist in the shorter words. More than the previous submitted JavaScript answer, but that answer doesn't seem to work?

Matt

Posted 2014-02-21T12:15:12.337

Reputation: 777