Skip to content

Instantly share code, notes, and snippets.

@btforsythe
Created October 26, 2017 01:11
Show Gist options
  • Save btforsythe/694b5b4b4406fed6be551d1fcc9ef030 to your computer and use it in GitHub Desktop.
Save btforsythe/694b5b4b4406fed6be551d1fcc9ef030 to your computer and use it in GitHub Desktop.
Rotate an array in place
package codefights;
public class RotateInPlace {
private int[][] a;
public RotateInPlace(int[][] a) {
this.a = a;
}
public void rotate() {
for (int initRow = 0; initRow < halfMatrixSizeRoundedDown(); initRow++) {
for (int initCol = 0; initCol < halfMatrixSizeRoundedUp(); initCol++) {
Coordinate[] coordinates = generateCoordinates(initRow, initCol);
int valueToMove = coordinates[3].value();
for(Coordinate coord: coordinates) {
valueToMove = coord.replaceValue(valueToMove);
}
}
}
}
private int matrixSize() {
return a.length;
}
private int halfMatrixSizeRoundedDown() {
return matrixSize() / 2;
}
private int halfMatrixSizeRoundedUp() {
return (matrixSize() + 1) / 2;
}
private Coordinate[] generateCoordinates(int initRow, int initCol) {
int distanceFromLeft = initCol + 1;
int distanceFromTop = initRow + 1;
return new Coordinate[] { //
new Coordinate(distanceFromTop - 1, distanceFromLeft - 1), ///
new Coordinate(distanceFromLeft - 1, matrixSize() - distanceFromTop), //
new Coordinate(matrixSize() - distanceFromTop, matrixSize() - distanceFromLeft), //
new Coordinate(matrixSize() - distanceFromLeft, distanceFromTop - 1) };
}
class Coordinate {
int row;
int col;
Coordinate(int row, int col) {
this.row = row;
this.col = col;
}
int value() {
return a[row][col];
}
int replaceValue(int newValue) {
int replaced = value();
a[row][col] = newValue;
return replaced;
}
}
}
package codefights;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
public class RotateInPlaceTest {
@Test
public void shouldRotate1x1() {
int[][] toRotate = { { 42 } };
RotateInPlace underTest = new RotateInPlace(toRotate);
underTest.rotate();
int[][] expected = { { 42 } };
assertThat(toRotate, is(expected));
}
@Test
public void shouldRotate2x2() {
int[][] toRotate = { //
{ 23, 42 }, //
{ 86, 12 } ///
};
RotateInPlace underTest = new RotateInPlace(toRotate);
underTest.rotate();
int[][] expected = { //
{ 86, 23 }, //
{ 12, 42 } };
assertThat(toRotate, is(expected));
}
@Test
public void shouldRotate3x3() {
int[][] toRotate = { //
{ 1, 2, 3 }, ///
{ 4, 5, 6 }, //
{ 7, 8, 9 } };
RotateInPlace underTest = new RotateInPlace(toRotate);
underTest.rotate();
int[][] expected = { //
{ 7, 4, 1 }, //
{ 8, 5, 2 }, //
{ 9, 6, 3 } };
assertThat(toRotate, is(expected));
}
@Test
public void shouldRotate5x5() {
int[][] toRotate = { //
{ 10, 9, 6, 3, 7 }, //
{ 6, 10, 2, 9, 7 }, //
{ 7, 6, 3, 8, 2 }, //
{ 8, 9, 7, 9, 9 }, //
{ 6, 8, 6, 8, 2 } };
RotateInPlace underTest = new RotateInPlace(toRotate);
underTest.rotate();
int[][] expected = { //
{ 6, 8, 7, 6, 10 }, //
{ 8, 9, 6, 10, 9 }, //
{ 6, 7, 3, 2, 6 }, //
{ 8, 9, 8, 9, 3 }, //
{ 2, 9, 2, 7, 7 } };
assertThat(toRotate, is(expected));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment