Background

I wanted to make a pretty word cloud, like this:

these    are
    words   

  floating  

I computed the (x,y)-coordinates of the first letter of each word, plugged them into my word cloud generator, and let it do its job. However, I accidentally used (y,x)-coordinates, so the result looks like this:

these      

   floating

 words     



are        

Since I'm too lazy to re-compute the coordinates, I need you to transpose the word cloud for me.

Input

Your input is a rectangular grid of lowercase ASCII letters and spaces. This means that every row is padded with spaces to have the same length. The input can be taken as a multi-line string or an array of strings.

A word is a horizontal segment of letters, and its position is the (x,y)-coordinates of its leftmost letter, the upper left corner of the grid being (0,0). There will always be at least one word, and there are no trailing rows or columns of spaces. However, leading rows or columns of spaces may be present.

Output

Your output is another rectangular grid of characters, obtained by moving every word with position (x,y) to position (y,x). Your output must not contain extra trailing rows or columns of spaces. Leading rows and columns must be preserved, and the output must be rectangular.

You may assume that this transformation does not create overlapping words, and does not merge several words into one. This means that running the program on the output should produce the original input.

Rules and scoring

You can write a full program or a function. The lowest byte count wins, and standard loopholes are disallowed.

Test cases

For clarity (and since Stack Exchange dislikes space-only lines), every row ends in a pipe character |. These are not part of the actual input or output, and you should remove them. Note again that running the program on each output should also produce the corresponding input.

Input:
oneword|
Output:
oneword|

Input:
  spaces|
Output:
      |
      |
spaces|

Input:
   |
row|
Output:
 row|

Input:
these    are|
    words   |
            |
  floating  |
Output:
these      |
           |
   floating|
           |
 words     |
           |
           |
           |
are        |

Input:
   same|
 the   |
       |
same   |
Output:
   same|
 the   |
       |
same   |

Input:
  some |
words k|
       |
       |
       |
  still|
Output:
 words    |
          |
some still|
          |
          |
          |
 k        |

Input:
   hello   |
  world hey|
what   up  |
Output:
  what|
      |
 world|
hello |
      |
      |
      |
  up  |
 hey  |

Input:
  a b  a d cc|
 g h  huh nng|
  ye dunnn   |
    dud  yo  |
 wha   g     |
  huh heh hah|
Output:
        |
 g  wha |
a ye huh|
 h      |
b  dud  |
  dunnn |
 huh heh|
a   g   |
        |
d  yo   |
 nng hah|
cc      |

Perl, 58 bytes

Added +2 for -lp

Give input on STDIN, run as perl -lp wordcloud.pl

wordcloud.pl:

s/\w+/$l|=$a[pos]|=$"x~-$..$&/eg}for(@a){$_|=$l=~y// /cr

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.