Created
September 3, 2013 23:47
-
-
Save ylegall/6431103 to your computer and use it in GitHub Desktop.
90 degree clockwise matrix rotation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
auto rotate(T)(T[][] array) { | |
foreach(i; 0 .. array.length/2) { | |
foreach(j; i .. array.length-i-1) { | |
T temp = array[i][j]; | |
array[i][j] = array[array.length-j-1][i]; | |
array[array.length-j-1][i] = array[array.length-i-1][array.length-j-1]; | |
array[array.length-i-1][array.length-j-1] = array[j][array.length-i-1]; | |
array[j][array.length-i-1] = temp; | |
} | |
} | |
} | |
void printArray(T)(T[][] array) { | |
foreach (subarray; array) { | |
writeln(subarray); | |
} | |
} | |
void main() { | |
int[][] array = [ | |
[1,2,3,4,5], | |
[2,4,6,8,10], | |
[1,3,5,7,9], | |
[2,3,5,7,11], | |
[9,8,7,6,5] | |
]; | |
rotate(array); | |
printArray(array); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment