Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Write a program which takes a 3x3 matrix on stdin and prints its transpose along the anti-diagonal to stdout. You may assume that all elements of the matrix will be integers. Columns are space-separated and rows are newline-separated.

Example

Input:

1 2 3
3 4 5
1 2 3

Output:

3 5 3
2 4 2
1 3 1

Input:

1 2 3
4 5 6
7 8 9

Output:

9 6 3
8 5 2
7 4 1
share|improve this question
    
Your example output seems incorrect; it has only been transposed along the second diagonal. –  Ilmari Karonen Mar 26 at 4:34
    
Also, what are the matrix elements? Digits? Positive integers? Any integers? Floats? Any strings? If numbers, is there an upper limit? –  Ilmari Karonen Mar 26 at 4:36
    
@IlmariKaronen edited –  Mukul Kumar Mar 26 at 4:46
1  
So, you're effectively saying that the matrix should be transposed twice along the first diagonal (once in step 1, then again in step 2) as well as along the second diagonal (in step 2)? That's the only way I can make sense of your example output. –  Ilmari Karonen Mar 26 at 4:49
    
@IlmariKaronen well yes –  Mukul Kumar Mar 26 at 4:51
show 1 more comment

7 Answers 7

up vote 4 down vote accepted

APL - 7

⌽⍉⌽3 3⍴

Example input:

⌽⍉⌽3 3⍴1 2 3 3 4 5 1 2 9
> 9 5 3
  2 4 2
  1 3 1

ngn APL demo

share|improve this answer
    
Could the downvoter please explain themself? Thanks. –  mniip Mar 26 at 6:57
    
God love APL. Nice solution! –  alvonellos Mar 28 at 3:49
    
@mniip ANy chance you can explain how this works? It's so... concise! I love it! –  WallyWest Mar 28 at 6:17
    
3 3⍴ converts input to a 3x3 matrix. reverses it along first dimension, transposes it, and then reverses it again. –  mniip Mar 28 at 17:14
add comment

GolfScript, 18 / 15 / 11 chars

~]-1%3/zip{' '*n}%

This is the straightforward implementation, following pretty much the exact steps given in the question. There's a clever arithmetic trick one could use instead, but it turns out to need more characters.

Sample input:

1 2 3
3 4 5
1 2 9

Sample output:

9 5 3
2 4 2
1 3 1

Ps. If I can use the same output format as in ace's answer (i.e. extra with square brackets around each row), I can save three chars for a total of 15 chars:

~]-1%3/zip{`n}%

If a one-line output format like [[9 5 3] [2 4 2] [1 3 1]] is allowed, I can shrink that further to just 11 chars:

~]-1%3/zip`
share|improve this answer
    
If I understand your code correctly, I think you misinterpreted the question. For the matrix [[1,2,3],[4,5,6],[7,8,9]], your program should output [[9,6,3],[8,5,2],[7,4,1]], not [[3,6,9],[2,5,8],[1,4,7]]. –  ace Mar 26 at 5:14
    
@ace: Yeah, I noticed that. Stupid symmetric example. Fixed. –  Ilmari Karonen Mar 26 at 5:15
    
You may save one character in each of the first two cases if you include the newline in the map, e.g. {' '*n}%. –  Howard Mar 26 at 5:54
    
@Howard: Edited, thanks! That'll give me an extra blank line at the end of the output, but I suppose that's acceptable. –  Ilmari Karonen Mar 26 at 5:56
add comment

Mathematica, 22 31 28 characters

Your first example was somewhat ambiguous, so the old solution simply reversed the rows in step two. Here is an updated one that actually does a transpose along the anti-diagonal.

Too bad Mathematica function names are so bulky. This would probably win by number of tokens

f=(r=Reverse)/@Thread[r/@#]&

In Mathematica itself, you actually only need 25 characters, because you can write the transposition (performed above by Thread) as a superscript.

share|improve this answer
    
You can replace the Reverseby r. You can also use <sup>T</sup> for Transpose. –  David Carraher Mar 26 at 17:24
    
@DavidCarraher haha, totally forgot to use that r. Thanks for the Transpose tip, though! The problem is, converted to ASCII (i.e. copy-paste-able) superscript T becomes \[Transpose], which doesn't really help. –  Martin Büttner Mar 26 at 17:26
    
To use the Transpose T, you first paste <sup>T</sup> into a regular text paragraph of your submission (not indented). Then you copy the superscripted T from the rendered text, below. Finally, you paste that superscripted T into your code in your answer. –  David Carraher Mar 26 at 17:45
    
@DavidCarraher a) even here on SE the pasted T is just a regular T, b) even if it was a superscripted T copying and pasting it into Mathematica would not give the correct function. When posting Mathematica answers I tend to count the characters such that they are work when pasted directly into Mathematica. –  Martin Büttner Mar 26 at 17:59
    
I see. Sometimes that happens, for reasons I don't understand. –  David Carraher Mar 26 at 18:51
show 2 more comments

Sage, 39

Runs in the interactive prompt

matrix(input()[::-1]).transpose()[::-1]

Sample input:

[[1,2,3],[3,4,5],[1,2,3]]

Sample output:

[3 5 3]
[2 4 2]
[1 3 1]
share|improve this answer
add comment

J - 16 (?) char

Taking the 3x3 matrix as a grid from stdin, we get the 16 character:

|:&.|.".1!:1]3#1

This can be made shorter if the input is made more flexible, as in the Sage and APL answers:

|:&.|.".1!:1]1   NB. if stdin input form can be  1 2 3, 4 5 6,: 7 8 9
|:&.|.           NB. if used as an expression like the APL answer

The key is in the |:&.|. portion: this is what transposes the matrix. It reads Transpose (|:) Under (&.) Reverse (|.), meaning you reverse the matrix, transpose it, and then undo your initial reverse.

Demo:

   |:&.|.".1!:1]3#1          NB. three lines input, three output
1 2 3
4 5 6
7 8 9
9 6 3
8 5 2
7 4 1

   1 2 3,3 4 5,:1 2 9        NB. a matrix
1 2 3
3 4 5
1 2 9
  |:&.|. 1 2 3,3 4 5,:1 2 9  NB. the logic
9 5 3
2 4 2
1 3 1
share|improve this answer
add comment

R, 78

write.table(matrix(rev(unlist(strsplit(readLines(),' '),' ')),3),qu=F,r=F,c=F)
# Copy and paste the input
# If the prompt is not on a new line, press `enter` after the last line
# Type `Ctrl+D`

I discovered the rev() function on SO, it helped me to understand than the transformation is just reversing the input and putting it in the matrix from top to bottom, and left to right.

I also discovered that the argument row.names=T (T for true) can be shortened to r=T, saving 16 chars.

Explanations:

  • readLines() reads STDIN and return a vector with 3 elements (each one is a string)
  • strsplit() splits the strings in the vector by using space as a separator
  • unlist() makes a flat list from a vector
  • rev() puts the list in reverse order
  • matrix([list], 3) creates a matrix from the list, the argument 3 indicates that there is 3 elements per row
  • write.table([matrix], qu=F, r=F, c=F) prints the matrix without quotes, rows and columns labels
share|improve this answer
add comment

TI-BASIC, 50

Input A:rowSwap(A,1,dim(A
AnsT→A:rowSwap(A,1,dim(A
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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