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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 '14 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 '14 at 4:36
    
@IlmariKaronen edited – Mukul Kumar Mar 26 '14 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 '14 at 4:49
    
@IlmariKaronen well yes – Mukul Kumar Mar 26 '14 at 4:51

10 Answers 10

up vote 7 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 '14 at 6:57
    
God love APL. Nice solution! – alvonellos Mar 28 '14 at 3:49
    
@mniip ANy chance you can explain how this works? It's so... concise! I love it! – WallyWest Mar 28 '14 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 '14 at 17:14

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 '14 at 5:14
    
@ace: Yeah, I noticed that. Stupid symmetric example. Fixed. – Ilmari Karonen Mar 26 '14 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 '14 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 '14 at 5:56

Mathematica, 31 28 20 bytes

(r=Reverse)[r@#]&

The is Mathematica's transpose operator (which is displayed as a superscript T in Mathematica).

share|improve this answer

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

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

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

TI-BASIC, 50

Input A:rowSwap(A,1,dim(A
AnsT→A:rowSwap(A,1,dim(A
share|improve this answer
    
This will throw an error when you try to store a matrix to the real variable A. – Thomas Kwa May 17 '15 at 4:37
    
In addition, it doesn't take input in the right format, which should be space-separated numbers. – Thomas Kwa May 17 '15 at 4:44

MATLAB, 15 bytes

Rotate it 90 degrees, and transpose. This takes the input where columns are space separated and rows are newline separated. Outputs on the same format.

@(A)rot90(A,2)'

ans([1 2 3
4 5 6
7 8 9])

ans =

     9     6     3
     8     5     2
     7     4     1
share|improve this answer

CJam, 13 bytes

q~]W%3/zSf*N*

Test it here.

CJam is newer than this challenge. This solution is very similar to the GolfScript one.

Explanation

q~   e# Read input and evaluate, pushing all 9 numbers on the stack.
]    e# Wrap them in an array.
W%   e# Reverse it - this performs a 180° rotation.
3/   e# Split into rows of length 3.
z    e# Transpose.
Sf*  e# Join integers in each row with spaces.
N*   e# Join the rows with linefeeds.

If there was no constraint on 3x3 inputs, we could either compute the line width with a square root:

q~]W%_,mQ/zSf*N*

Or we could perform the anti-diagonal transpose as vertical flip, transpose, vertical flip (like my Mathematica answer does):

qN/Sf/W%zW%Sf*N*

In either case, we'd have 16 bytes.

share|improve this answer

K, 3/7/20(?) bytes

K is similar enough to APL that a character-for-character transliteration of @mniip's solution works:

|+|3 3#

In action:

  |+|3 3#1 2 3 3 4 5 1 2 9
(9 5 3
 2 4 2
 1 3 1)

This behaves identically, modulo the way output is prettyprinted. However, I should note that neither this solution nor the solution it is based on actually operate on stdin/stdout. To implement this as per a stricter interpretation of the spec in Kona-compatible K3 it's necessary to use 0: and jump through some hoops:

`0:,/'2$|+|3 3#. 0:`

Write to stdout (`0:) the join over each (,/') of the two-wide string format (2$) of the anti-diagonal transpose (|+|) of the 3x3 reshape (3 3#) of the eval (.) of stdin (0:`).

In action:

indigo:kona je$ ./k antidiag.k 
K Console - Enter \ for help

  1 2 3 3 4 5 1 2 9
 9 5 3
 2 4 2
 1 3 1

There's a pretty good reason that APL-family programmers tend to avoid problems that force the use of stdin/stdout. Arguably, with flexible IO requirements, this could be solved with simply |+|:

  |+|(1 2 3;3 4 5;1 2 9)
(9 5 3
 2 4 2
 1 3 1)
share|improve this answer

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.