Convert n strings of m length to m strings of n length

16

Write a program that, given any 'n' number of strings of 'm' length, returns 'm' number of 'n'-length strings, with this condition:

Every new string should contains the letters at the same index of the others strings

For example, the first output string must contain the first letter of all the input strings, the second output string must contain the second letter of all the input strings, and so on.

Examples (the numbers under the letters are the indexes of the strings):

input: "car", "dog", "man", "yay"
        012    012    012    012
output: "cdmy", "aoaa", "rgny"
         0000    1111    2222

input: "money", "taken", "trust"
        01234    01234    01234
output: "mtt", "oar", "nku", "ees", "ynt"
         000    111    222    333    444

Assume that the input is correct everytime

Shortest bytes' code wins!

Edit:

Since there are many programming languages, and there could be many possible solutions for each of them, I will post the shortest solution for each programming language and the user who provided it. Keep coding!

Re-Edit:

Thanks to user Dennis I inserted a snippet for the leaderboards.

var QUESTION_ID=85255,OVERRIDE_USER=56179;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

Input Name Here

Posted 2016-07-12T16:27:25.627

Reputation: 177

2

We have a leaderboard snippet that you can add to your question body. No need to do that by hand.

– Dennis – 2016-07-12T16:49:13.460

Duplicate: http://codegolf.stackexchange.com/q/17305

– msh210 – 2016-07-12T18:06:08.293

I'm sorry, I didn't know it was called "transposing" – Input Name Here – 2016-07-12T19:01:27.160

6I do not believe these challenges are duplicates, because of the differences involving ragged transposition and some weird whitespace handling in the other question. While in some languages the answers might be very similar, I think in most they will be different enough that this shouldn't be a duplicate. – FryAmTheEggman – 2016-07-13T12:40:13.473

Related, Related – FlipTack – 2017-12-04T20:37:04.690

Answers

21

Pyth, 1 byte

C

Try it here

Another transpose builtin

Blue

Posted 2016-07-12T16:27:25.627

Reputation: 26 661

5How is this even possible?! – DavidC – 2016-07-12T19:15:10.617

Surely you're joking! – Vincent – 2016-07-12T19:43:09.157

Pyth is the new Mathematica! – Loovjo – 2016-07-12T20:24:31.377

9

Python, 36 29 bytes

zip(*s) returns a list of tuples of each character, transposed.

lambda s:map(''.join,zip(*s))

Try it online

mbomb007

Posted 2016-07-12T16:27:25.627

Reputation: 21 944

The output says "cdmy", "aoaa", "rgny", which is a list ["cdmy", "aoaa", "rgny"] or a tuple ("cdmy", "aoaa", "rgny") – mbomb007 – 2016-07-12T16:41:02.083

map(''.join,zip(*s)) also works for the string conversion (Python 2 only), and for Python 3, [*map(''.join,zip(*s))] afaik works – Value Ink – 2016-07-12T17:14:35.380

@KevinLau-notKenny map(''.join,zip(*s)) is valid for Python 3 also - we allow iterators/generators in place of lists by default. – Mego – 2016-07-12T21:35:38.267

8

Dennis

Posted 2016-07-12T16:27:25.627

Reputation: 196 637

7

MATL, 1 byte

!

Try it online!

Takes input implicitly, transposes, displays output implicitly.

Luis Mendo

Posted 2016-07-12T16:27:25.627

Reputation: 87 464

7

PowerShell v2+, 66 54 bytes

param($n)for($x=0;$n[0][$x];$x++){-join($n|%{$_[$x]})}

Yo ... no map, no zip, no transpose, etc., so we get to roll our own. Big props to @DarthTwon for the 12-byte golf.

Takes input $n, sets up a for loop. Initialization sets $x to 0, the test is whether we still have letters in our word $n[0][$x], and we increment $x++ each iteration.

Inside the loop, we take our array of strings, pipe it to an inner loop that spits out the appropriate character from each word. That is encapsulated in a -join to form a string, and that string is left on the pipeline. At end of execution, the strings on the pipeline are implicitly printed.

PS C:\Tools\Scripts\golfing> .\convert-n-strings.ps1 "Darth","-Twon","Rocks"
D-R
aTo
rwc
tok
hns

AdmBorkBork

Posted 2016-07-12T16:27:25.627

Reputation: 41 581

1Nice, that's the kind of the answers I wanted to see ;) It's too simple writing , rather than thinking about the answer – Input Name Here – 2016-07-12T17:15:14.720

Using a while loop can further pare that down: param($n)$x=0;while($n[0][$x]){-join($n|%{$_[$x]});$x++}. And no errors here :D – ThePoShWolf – 2016-07-12T18:45:19.213

@DarthTwon Excellent. Or, use a for loop for another two. ;-) – AdmBorkBork – 2016-07-12T19:06:47.130

Hmm... I tried that, but not after really paring down my while loop. Good work sir! – ThePoShWolf – 2016-07-12T20:03:32.873

Can you use Linq in Powershell? – aloisdg moving to codidact.com – 2016-07-20T15:33:56.503

@aloisdg Mmm, kinda. Usually it requires a lengthy call like [System.Linq.<something>] since it's not native like in C#, so it's usability is limited in code-golf. In many cases, there's a roughly-equivalent PowerShell-native method. – AdmBorkBork – 2016-07-20T15:45:01.317

7

Vim, 37 36 keystrokes

All the other answers are boring, and using boring single-byte builtins. Here's a hacky answer that does the entire thing manually, in something that isn't even a programming language:

Gmaqqgg:s/./&<cr>d<C-v>`aGo<esc>pvGgJ@qq@q`adgg

Explanation:

G                                   "move to the end of this line
     ma                             "And leave mark 'a' here
       qq                           "Start recording in register 'q'
         gg                         "Move to the beginning
           :s/./&<cr>               "Assert that there is atleast one character on this line
                      d             "Delete
                       <C-v>        "Blockwise
                            `a      "Until mark 'a'

    G                               "Move to the end of the buffer
     o<esc>                         "Open a newline below us
           p                        "And paste what we deleted
            vG                      "Visually select everything until the end
              gJ                    "And join these without spaces
                @q                  "Call macro 'q'. The first time, this will do nothing,
                                    "The second time, it will cause recursion.
                  q                 "Stop recording
                   @q               "Call macro 'q' to start it all

Now, everything is good, but the buffer has some extra text left over. So we must:

`a                              "Move to mark 'a'
  dgg                           "And delete everything until the first line

James

Posted 2016-07-12T16:27:25.627

Reputation: 54 537

5

CJam, 6 5 bytes

Saved 1 byte thanks to Luis Mendo.

qS%zp

Try it online!

q      e# Get all input
 S%    e# Split it on spaces
   z   e# Take the transpose
    p  e# Print it as an array

Business Cat

Posted 2016-07-12T16:27:25.627

Reputation: 8 927

4

Pyke, 1 byte

,

Try it here!

Transpose.

Blue

Posted 2016-07-12T16:27:25.627

Reputation: 26 661

1Oh... Didn't think it would have been so simple :/ – Input Name Here – 2016-07-12T16:32:35.873

1Yeah, I can't seem to find a duplicate by looking for 60 secs though – Blue – 2016-07-12T16:34:16.877

4

Retina, 45 43 bytes

Byte count assumes ISO 8859-1 encoding.

O$#`.(?<=(.+))|¶
$.1
!`(?<=(¶)+.*)(?<-1>.)+

The leading linefeed is significant. Input and output are linefeed-terminated lists of printable ASCII strings (note that both have a single trailing linefeed).

Try it online!

I knew for a while that transposing rectangular blocks would be a pain in Retina (whereas transposing squares isn't too bad), but never actually tried. The first solution was indeed a whopping 110 bytes long, but after several substantial changes in the approach, the resulting 45 bytes are by far not as bad as I suspected (but still...). Explanation will follow tomorrow.

Explanation

Stage 1: Sort

O$#`.(?<=(.+))|¶
$.1

This does the main work of reordering the characters in the input, but it ends up messing up the separation into lines. Interestingly, if we remove the , we get the code required to transpose a square input.

Sort stages (denoted by the O) work like this: they find all matches of the given regex (the thing after the `), and then sort those matches and reinsert them into the places where the matches were found. As it happens, this regex matches every single character: non-linefeeds via the .(?<=(.*)) alternative and linefeeds via the . Hence, it sorts all characters in the input. The more interesting part is what they are sorted by.

The $ option activates a "sort-by" mode, where each match is replaced with the substitution pattern on the second line, which is then used for comparing the matches. Furthermore, the # tells Retina to convert the result of the substitution to an integer, and compare those integers (instead of treating them as strings).

So finally, we need to look at the regex and the substitution. If the first alternative matches (i.e. we have matched any character within one of the lines), then the (?<=(.*)) captures everything up to that character on that line into group 1. The $.1 in the substitution pattern replaces this with the length of group 1. Hence, the first character in each string becomes 1, the second becomes 2, the third becomes 3 and so on. It should be clear now how this transposes a square input: all the first characters of the lines come first and all end up in the top-most line, then all the second characters end up in the second line and so on. But for these rectangular inputs, we're also matching the linefeeds. Since group 1 is unused in this case, the substitution is empty, but for the purposes of the # option, this is considered 0. That means, all the linefeeds are sorted to the front.

So now we do have the characters in the first order (first character of each string, second character of each string, etc.) and all the linefeeds at the start.

Stage 2: Match

!`(?<=(¶)+.*)(?<-1>.)+

We now need to split up the characters into lines of the correct length. This length corresponds to the number of lines in the original input, which is corresponds to the number of linefeeds we have at the beginning of the string.

The splitting is done here with the help of a match stage, which simply finds all matches of the given regex and uses the ! option to print those matches (the default would be to count them instead). So the goal of the regex is to match one line at a time.

We start by "counting" the number with the lookbehind (?<=(¶)*.*). It generates one capture in group 1 for every linefeed at the front.

Then, for each of those captures, we match a single character with (?<-1>.)+.

Martin Ender

Posted 2016-07-12T16:27:25.627

Reputation: 184 808

4

x86 machine code, 19 bytes

In hex:

fc89d35651a401dee2fb91aa595e464a75f1c3

Input: ECX: # of strings (n), EDX: individual string length (m), ESI: array of input strings, EDI: output buffer receiving array of strings. Array is presumed to be defined as char src[n][m+1] for input and char dst[m][n+1] for output, and all strings are NULL-terminated.

0:  fc                  cld
1:  89 d3               mov ebx,edx   ;EDX is the counter for the outer loop
_outer:
3:  56                  push esi
4:  51                  push ecx
_inner:
5:  a4                  movsb         ;[EDI++]=[ESI++]
6:  01 de               add esi,ebx   ;Same char, next string
8:  e2 fb               loop _inner   ;--ECX==0 => break
a:  91                  xchg eax,ecx  ;EAX=0
b:  aa                  stosb         ;NULL-terminate just completed string
c:  59                  pop ecx
d:  5e                  pop esi       ;First string,
e:  46                  inc esi       ;...next char
f:  4a                  dec edx
10: 75 f1               jnz _outer
12: c3                  ret

meden

Posted 2016-07-12T16:27:25.627

Reputation: 711

3

Brachylog, 5 bytes

z:ca.

Expects a list of strings as input, e.g. run_from_atom('z:ca.',["money":"taken":"trust"],Output).

Explanation

z       Zip the strings in the Input list
 :ca.   output is the application of concatenation to each element of the zip

Fatalize

Posted 2016-07-12T16:27:25.627

Reputation: 32 976

3

05AB1E, 3 bytes

ø€J

Explained

     # implicit input, eg: ['car', 'dog', 'man', 'yay']
ø    # zip, producing [['c', 'd', 'm', 'y'], ['a', 'o', 'a', 'a'], ['r', 'g', 'n', 'y']]
 €J  # map join, resulting in ['cdmy', 'aoaa', 'rgny']

Try it online

Emigna

Posted 2016-07-12T16:27:25.627

Reputation: 50 798

2

JavaScript ES6, 48 46 bytes

a=>[...a[0]].map((n,x)=>a.map(a=>a[x]).join``)

I feel left out, we have no built in zip function. Thanks nicael for pointing out my type error.

Usage

(a=>[...a[0]].map((n,x)=>a.map(a=>a[x])))(["asd","dsa"]); //or whatever is above, might change due to edits

returns

["ad","ss","da"]

charredgrass

Posted 2016-07-12T16:27:25.627

Reputation: 935

Exception: TypeError: a[0].map is not a function – edc65 – 2016-07-12T16:48:23.723

1You need [...a[0]].map, since a[0] isn't an array. – nicael – 2016-07-12T16:49:35.417

@nicael thanks. messed up the types. this proves why I should not golf this early in the morning. – charredgrass – 2016-07-12T16:50:26.187

@nicael I .joined them to fix that problem. – charredgrass – 2016-07-12T16:56:23.823

Yep, does work now. @edc please retract your downvote, the answer has been fixed. – nicael – 2016-07-12T16:57:44.853

1join`` instead of join('') to save 2 bytes. Downvote retracted – edc65 – 2016-07-12T17:39:22.203

2

CJam, 3 bytes

{z}

This is a code block (equivalent to a function; allowed by default) that expects the input on the stack and leaves the output on the stack.

Try it online!

Luis Mendo

Posted 2016-07-12T16:27:25.627

Reputation: 87 464

Sure about that? – James – 2016-07-12T19:00:39.467

1@Drgreeneggsandironman Well, the answer with more votes says The default should be "programs or functions" – Luis Mendo – 2016-07-12T19:34:58.777

2

Bash + BSD utilities, 27

sed s/./\&:/g|rs -c: -g0 -T

I/O via STDIN/STDOUT newline-separated strings.

You may need to install rs with sudo apt install rs or similar. Works out of the box on OS X.

The -T option to rs does the heavy lifting of the transposition. The rest is just formatting:

  • The sed command simply inserts : after every character
  • -c: specifies input columns are : separated
  • -g0 specifies output columns have zero width separation

If my reading of the rs manpage is correct, then the following ought to work for a score of 12, but unfortunately it doesn't work - see note below:

rs -E -g0 -T

Example output:

$ printf "%s\n" car dog man yay |sed s/./\&:/g|rs -c: -g0 -T
cdmy
aoaa
rgny
$

If input is expected to be all printable ASCII, then the : may be replaced with some unprintable character, e.g. 0x7 BEL.


Note

I wanted to understand why I couldn't get the -E option to work and get rid of the sed preprocessing. I found the rs source code here. As you can see, giving this option sets the ONEPERCHAR flag. However, there is nothing in the code that actually checks the state of this flag. So I think we can say that despite the fact that is option is documented, it is not implemented.

In fact, this option is documented in the Linux rs manpage:

-E Consider each character of input as an array entry.

but not the OS X version.

Digital Trauma

Posted 2016-07-12T16:27:25.627

Reputation: 64 644

2

PHP, 82 bytes

function f($a){foreach($a as$s)foreach(str_split($s)as$i=>$c)$y[$i].=$c;return$y;}

takes and returns an array of strings

break down

function f($a)
{
    foreach($a as$s)                    // loop through array $a
        foreach(str_split($s)as$i=>$c)  // split string to array, loop through characters
            $y[$i].=$c;                 // append character to $i-th result string
    return$y;
}

examples

$samples=[
    ["asd","dsa"], ["ad","ss","da"],
    ["car", "dog", "man", "yay"], ["cdmy", "aoaa", "rgny"],
    ["money", "taken", "trust"], ["mtt", "oar", "nku", "ees", "ynt"]
];
echo '<pre>';
while ($samples)
{
    echo '<b>in:</b> ';         print_r($x=array_shift($samples));
    echo '<b>out:</b> ';        print_r(f($x));
    echo '<b>expected:</b> ';   print_r(array_shift($samples));
    echo '<hr>';
}

Titus

Posted 2016-07-12T16:27:25.627

Reputation: 13 814

2

APL, 3 bytes

↓⍉↑

takes the input strings and turns them into a char matrix. transposes the matrix. splits back the rows of the resulting matrix into strings.

lstefano

Posted 2016-07-12T16:27:25.627

Reputation: 850

2

K, 1 byte

+

Try it here!

Transpose

Adám

Posted 2016-07-12T16:27:25.627

Reputation: 37 779

1

MATLAB / Octave, 4 bytes

@(x)x'

This defines an anonymous function. Input format is ['car'; 'dog'; 'man'; 'yay'].

Try it here.

Luis Mendo

Posted 2016-07-12T16:27:25.627

Reputation: 87 464

What format is the input for this solution? I don't think it will work with the way Matlab handles strings if you try to input them in a Matlabby way, e.g. f = @(x)x', f([{'abcd'},{'abcd'},{'abcd'}]) outputs

ans =

'abcd'
'abcd'
'abcd'
 – sintax  – 2016-07-13T20:36:11.587

@sintax Input format should be a 2D char array, as in the example linked in the answer. You are using a cell array of strings (obtained by concatenating singleton cell arrays). It should be: f(['abcd';'abcd';'abcd']). I've edited the answer to specify the input format – Luis Mendo – 2016-07-13T21:42:17.173

1

Mathematica, 26 bytes

""<>#&/@(Characters@#)&

Anonymous function. Takes a string list as input and returns a string list as output. The Unicode character is U+F3C7, representing \[Transpose]. Works by converting to a character matrix, transposing, and converting back to a string list. If the input/output format was stretched, then a simple 5-byte transpose would work:

#&

LegionMammal978

Posted 2016-07-12T16:27:25.627

Reputation: 15 731

+1 you beat me to the punch! (But I wasn't able to use the Transpose symbol.) By the way, why doesn't the T show up on screen? (It is there when I copy your code to Mathematica). – DavidC – 2016-07-12T19:27:32.133

@DavidC The Unicode character is in the Private Use Area, which is marked as a section for proprietary characters and is therefore neither officially assigned a glyph nor rendered at all in most fonts. Mathematica's font just happens to render this character as a supserscript T and interpret it as a \[Transpose]. – LegionMammal978 – 2016-07-12T19:52:33.190

@DavidC Why don't they use ? – Adám – 2016-07-20T15:13:33.250

@Adám IDK, you'd have to ask Wolfram Research about that, not me – LegionMammal978 – 2016-07-20T15:17:27.057

@LegionMammal978 Nah, I'm happy as long as I have APL's obvious . – Adám – 2016-07-20T15:43:23.637

Adám, I was puzzled myself. From LegionMammal978's answer, I gather that he wanted code that would run unaltered in Mathematica. I think the superscripted T that you showed will not run when copied and pasted from Code Golf to Mathematica. – DavidC – 2016-07-20T20:22:19.573

1

Haskell, 41 bytes

f l=zipWith($)[map(!!n)|n<-[0..]]$l<$l!!0

Or with a bit more air:

f l = zipWith ($) [map (!! n) | n <- [0..]] $
                  l <$ (l !! 0)

The second list is the list of words repeated “the length of a word” time. On each list of words, we select the n-th letter of each word, giving the result.

villou24

Posted 2016-07-12T16:27:25.627

Reputation: 161

1

There's no need for a full program, because "program" means "program or function", so a function is enough. You can pass and return a list of strings and therefore omit wordsand unwords. Further, map(\_->l)(l!!0) is l<*l!!0, so it boils down to \l->zipWith($)[map(!!n)|n<-[0..]]$l<$l!!0.

– nimi – 2016-07-13T22:15:52.970

Oh, good thanks :) I'm updating. – villou24 – 2016-07-13T22:25:52.237

Oops, typo: it's l<$l!!0 (first time wrong, second time right), but you got it right anyway. – nimi – 2016-07-13T22:34:47.893

1

Common Lisp, 62 bytes

(lambda(s)(apply'map'list(lambda(&rest x)(coerce x'string))s))

The map function takes a result type (here list), a function f to apply and one or more sequences s1, ..., sn. All sequences are iterated in parallel. For each tuple of elements (e1,...,en) taken from those sequences, we call (f e1 ... en) and the result is accumulated in a sequence of the desired type.

We take a list of strings, say ("car" "dog" "man" "yay"), and use apply to call map. We have to do this so that the input list is used as more arguments to map. More precisely, this:

(apply #'map 'list fn '("car" "dog" "man" "yay"))

... is equivalent to:

(map 'list f "car" "dog" "man" "yay")

And since strings are sequences, we iterate in parallel over all first characters, then all second characters, etc... For example, the first iteration calls f as follows:

(f #\c #\d #\m #\y)

The anonymous lambda takes the list of arguments given to it and coerces it back to a string.

coredump

Posted 2016-07-12T16:27:25.627

Reputation: 6 292

1

Perl, 91 bytes

So lengthy one..

$l=<>;$p=index($l," ")+1;@i=split//,$l;for$a(0..$p-1){print$i[$a+$_*$p]for(0..$p);print" "}

Try it here!

Swadhikar C

Posted 2016-07-12T16:27:25.627

Reputation: 141

0

Ruby, 46 bytes

Probably the first time that "Ruby strings aren't Enumerables" is actually biting me hard since I had to map the strings into arrays before processing. (Normally having to use String#chars isn't enough of a byte loss to matter, but since I need to map them, it stings a lot more)

->s{s.map!(&:chars).shift.zip(*s).map &:join}

Value Ink

Posted 2016-07-12T16:27:25.627

Reputation: 10 608

0

Clojure, 68 bytes

#(map(fn[g](reduce(fn[a b](str a(nth b g)))""%))(range(count(% 0))))

Maps a function which is just reduce (goes on elements of the list one by one and joins nth character of the string) on the range from 0 to length of the first string.

See it online: https://ideone.com/pwhZ8e

cliffroot

Posted 2016-07-12T16:27:25.627

Reputation: 1 080

0

C#, 53 bytes

t=>t[0].Select((_,i)=>t.Aggregate("",(a,b)=>a+b[i]));

C# lambda (Func) where the output is IList<string> and the output is IEnumerable<string>. I dont know how work zip in other language but I can't find a way to use the C#'s one here. Aggregate fit the need well. No transpose in C#, there is one in Excel but I wont use it.

Try it online!

aloisdg moving to codidact.com

Posted 2016-07-12T16:27:25.627

Reputation: 1 767