Wednesday 29 July 2015

Print array in spiral shape

For given N* N matrix,
1 2 3
8 9 4
7 6 5
Write a program to print 1,2,3,4,5,6,7,8,9.

It is very simple. First print first row (from left to rigth), next last column (from top to bottom), next last row(from right to left) and next first column (from bottom to top). Repeat this procedure until you process all elements.
import java.util.Objects;

public class PrintSpiral {

 private static int firstRow;
 private static int firstColumn;
 private static int lastRow;
 private static int lastColumn;
 private static int arr[][];
 private static StringBuilder builder = new StringBuilder();

 public static String getSpiral(int array[][]) {
  Objects.nonNull(array);
  int rows = array.length;
  if (rows == 0)
   return "";
  int cols = array[0].length;

  if (rows != cols)
   throw new IllegalArgumentException(
     "rows and columns are not equals");

  builder = new StringBuilder();
  firstRow = firstColumn = lastRow = lastColumn = -1;
  arr = array;

  firstRow = 0;
  firstColumn = 0;
  lastRow = arr.length - 1;
  lastColumn = lastRow;

  int length = arr.length / 2;
  if (arr.length % 2 != 0) {
   length++;
  }

  for (int i = 0; i < length; i++) {
   printFirstRow(firstRow, firstColumn, lastColumn);
   printLastColumn(firstRow, lastColumn, lastRow);
   printLastRow(lastColumn, firstColumn, lastRow);
   printFirstColumn(firstColumn, lastRow, firstRow);

   firstRow++;
   lastRow--;
   firstColumn++;
   lastColumn--;
  }
  return builder.toString();
 }

 private static void printFirstRow(int firstRow, int firstColumn,
   int lastColumn) {
  for (int i = firstColumn; i <= lastColumn; i++) {
   builder = builder.append(arr[firstRow][i]).append(" ");
  }
 }

 private static void printLastColumn(int firstRow, int lastColumn,
   int lastRow) {
  for (int i = firstRow + 1; i <= lastRow; i++) {
   builder = builder.append(arr[i][lastColumn]).append(" ");
  }
 }

 private static void printLastRow(int lastColumn, int firstColumn,
   int lastRow) {
  for (int i = lastColumn - 1; i >= firstColumn; i--) {
   builder = builder.append(arr[lastRow][i]).append(" ");
  }
 }

 private static void printFirstColumn(int firstColumn, int lastRow,
   int firstRow) {
  for (int i = lastRow - 1; i > firstRow; i--) {
   builder = builder.append(arr[i][firstColumn]).append(" ");
  }
 }

}


Following is the junit test case for above program.

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class PrintSpiralTest {

 @Test
 public void test1() {
  int arr1[][] = { { 1 } };
  int arr2[][] = { { 1, 2, 3 }, { 8, 9, 4 }, { 7, 6, 5 } };
  int arr3[][] = { { 1, 2, 3, 4 }, { 12, 13, 14, 5 }, { 11, 16, 15, 6 },
    { 10, 9, 8, 7 } };
  int arr4[][] = { { 1, 2, 3, 4, 5 }, { 16, 17, 18, 19, 6 },
    { 15, 24, 25, 20, 7 }, { 14, 23, 22, 21, 8 },
    { 13, 12, 11, 10, 9 } };
  int arr5[][] = { { 1, 2, 3, 4, 5, 6, 7 },
    { 24, 25, 26, 27, 28, 29, 8 }, { 23, 40, 41, 42, 43, 30, 9 },
    { 22, 39, 48, 49, 44, 31, 10 }, { 21, 38, 47, 46, 45, 32, 11 },
    { 20, 37, 36, 35, 34, 33, 12 }, { 19, 18, 17, 16, 15, 14, 13 } };

  int arr6[][] = {};

  String extected1 = "1";
  String extected2 = "1 2 3 4 5 6 7 8 9";
  String extected3 = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16";
  String extected4 = "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";
  String extected5 = "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";
  String expected6 = "";

  assertEquals(PrintSpiral.getSpiral(arr1).trim(), extected1);
  assertEquals(PrintSpiral.getSpiral(arr2).trim(), extected2);
  assertEquals(PrintSpiral.getSpiral(arr3).trim(), extected3);
  assertEquals(PrintSpiral.getSpiral(arr4).trim(), extected4);
  assertEquals(PrintSpiral.getSpiral(arr5).trim(), extected5);
  assertEquals(PrintSpiral.getSpiral(arr6).trim(), expected6);
 }

 @Test(expected = NullPointerException.class)
 public void test2() {
  PrintSpiral.getSpiral(null);
 }

 @Test(expected = IllegalArgumentException.class)
 public void test3() {
  int arr[][] = { { 1, 2 } };
  PrintSpiral.getSpiral(arr);
 }
}


No comments:

Post a Comment