The problem
On this problem your mission is to rotate matrix counter-clockwise N
instances.
So, you should have 2 inputs:
1) matrix
2) a quantity, what number of instances to show it
And the output is turned matrix.
Instance:
matrix = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
times_to_turn = 1
It ought to return this:
[[4, 8, 12, 16],
[3, 7, 11, 15],
[2, 6, 10, 14],
[1, 5, 9, 13]])
Be aware: all matrixes will likely be sq.. Additionally, random checks can have large numbers within the enter (instances to show)
The answer in Java code
Possibility 1:
public class Answer {
public static int[][] rotateCounterclockwise(int[][] matrix, int instances) {
int matlen = matrix.size;
for (int n = 0; n < instances % 4; n++) {
int[][] aspect = new int[matlen][matlen];
for (int i = 0; i < matlen; i++) {
for (int j = 0; j < matlen; j++) {
aspect[i][j] = matrix[j][matlen - 1 - i];
}
}
matrix = aspect;
}
return matrix;
}
}
Possibility 2:
public class Answer {
public static int[][] rotateCounterclockwise(int[][] matrix, int instances) {
int measurement = matrix.size;
int mat[][] = new int[size][size];
int temp[][];
instances = instances % 4;
if (instances==0) return matrix;
whereas(times-->0) {
for(int i=0,y=0;i<measurement;i++,y++)
for(int j=0,x=size-1;j<measurement;j++,x--)
mat[x][y]=matrix[i][j];
if(instances>0){
temp=matrix;
matrix=mat;
mat=temp;
}
}
return mat;
}
}
Possibility 3:
public class Answer {
public static int[][] rotateCounterclockwise(int[][] matrix, int instances) {
instances = instances % 4;
whereas(instances > 0) {
matrix = rotateOnce(matrix);
times--;
}
return matrix;
}
non-public static int[][] rotateOnce(int[][] matrix) {
int[][] r = new int[matrix.length][matrix.length];
for (int i = r.length-1; i >=0; i--) {
for (int j = 0; j < r.size; j++) {
r[r.length-1-i][j] = matrix[j][i];
}
}
return r;
}
}
Take a look at circumstances to validate our answer
import org.junit.Take a look at;
import java.util.Random;
import java.util.perform.BiFunction;
import java.util.perform.IntFunction;
import java.util.perform.IntSupplier;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.Assert;
public class SolutionTest {
@Take a look at
public void exampleTest() {
Assert.assertArrayEquals(new int[][] {
{2, 4},
{1, 3}},
Answer.rotateCounterclockwise(new int[][] {
{1, 2},
{3, 4}}, 1));
Assert.assertArrayEquals(new int[][] {
{4, 8, 12, 16},
{3, 7, 11, 15},
{2, 6, 10, 14},
{1, 5, 9, 13}},
Answer.rotateCounterclockwise(new int[][] {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}}, 1));
Assert.assertArrayEquals(new int[][] {
{16, 15, 14, 13},
{12, 11, 10, 9},
{8, 7, 6, 5},
{4, 3, 2, 1}},
Answer.rotateCounterclockwise(new int[][] {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}}, 2));
Assert.assertArrayEquals(new int[][] {
{57, 49, 41, 33, 25, 17, 9, 1},
{58, 50, 42, 34, 26, 18, 10, 2},
{59, 51, 43, 35, 27, 19, 11, 3},
{60, 52, 44, 36, 28, 20, 12, 4},
{61, 53, 45, 37, 29, 21, 13, 5},
{62, 54, 46, 38, 30, 22, 14, 6},
{63, 55, 47, 39, 31, 23, 15, 7},
{64, 56, 48, 40, 32, 24, 16, 8}},
Answer.rotateCounterclockwise(new int[][] {
{1, 2, 3, 4, 5, 6, 7, 8},
{9, 10, 11, 12, 13, 14, 15, 16},
{17, 18, 19, 20, 21, 22, 23, 24},
{25, 26, 27, 28, 29, 30, 31, 32},
{33, 34, 35, 36, 37, 38, 39, 40},
{41, 42, 43, 44, 45, 46, 47, 48},
{49, 50, 51, 52, 53, 54, 55, 56},
{57, 58, 59, 60, 61, 62, 63, 64}}, 3));
}
}