TDD LeetCode:832. Flipping an Image

RojerChen.2018.10.08

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image.
To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0, 1, 1].
To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0].
Example 1:
Input: [[1,1,0],[1,0,1],[0,0,0]]
Output: [[1,0,0],[0,1,0],[1,1,1]]
Explanation: First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].
Then, invert the image: [[1,0,0],[0,1,0],[1,1,1]]
Example 2:
Input: [[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]
Output: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Explanation: First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].
Then invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]
Notes:
  • 1 <= A.length = A[0].length <= 20
  • 0 <= A[i][j] <= 1

※前言

原本我以為可以在很短的時間內把這題給解完,沒想到後來還是花了不少時間處理 (昏),我在寫得時候遇到了幾個問題

1.陣列的基本知識 (太久沒用了,超不熟練)
2.該怎樣寫測試?如果我用一維陣列寫各別功能,要怎樣跟不規則陣列組合起來?

※基本知識
※一維陣列
int[] array = new int[5];
int[] array1 = new int[] { 1, 3, 5, 7, 9 };

陣列資料複製
Array.Copy(sourceArray, destArray, sourceArray.Length);

※多維陣列
int[,] array = new int[4, 2];
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
                                 { { 7, 8, 9 }, { 10, 11, 12 } } };

※不規則陣列 jagged Array
int[][] jaggedArray = new int[3][];

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };

int[][] jaggedArray2 = new int[][] 
{
    new int[] {1,3,5,7,9},
    new int[] {0,2,4,6},
    new int[] {11,22}
};

Step0 題目要求

從題目的說明可以知道我們大概要幾個東西:

  1. 一維陣列、不規則陣列
  2. 一維陣列 reverse
  3. 一維陣列 invert

Step1  準備「一維陣列 reverse」 測試資料

[TestMethod]
public void 一維陣列reverse()
{
    int[] array = new[] { 1, 1, 0 };
    int[] expected = new[] { 0, 1, 1 };

    FlippingAnImage.Algorithm alg = new Algorithm();
    int[] actual = alg.reverse(array);
    CollectionAssert.AreEqual(expected, actual);
}

Step2  撰寫「一維陣列 reverse」 程式

public int[] reverse(int[] source)
{
    int[] dest = new int[source.Length];
    for (int i = 0; i < source.Length; i++)
    {
        dest[i] = source[source.Length - 1 - i];
    }

    return dest;
}

Step3  準備「一維陣列 invert」 測試資料


[TestMethod]
public void 一維陣列invert()
{
    int[] array = new[] { 1, 1, 0 };
    int[] expected = new[] { 0, 0, 1 };

    FlippingAnImage.Algorithm alg = new Algorithm();
    int[] actual = alg.invert(array);
    CollectionAssert.AreEqual(expected, actual);
}

Step4  撰寫「一維陣列 invert」 程式

 public int[] invert(int[] source)
{
    int[] dest = new int[source.Length];
    for (int i = 0; i < source.Length; i++)
    {
        dest[i] = ( source[i] == 1 ) ? 0 : 1;
    }
    return dest;
}

Step5 準備測試資料

[TestMethod]
public void Reverse1_Flip1_TestMethod1()
{
    int[][] array = new int[][]
    {
        new int[] {1,1,0},
        new int[] {1,0,1},
        new int[] {0,0,0}
    };

    int[][] expected = new int[][]
    {
        new int[] {1,0,0},
        new int[] {0,1,0},
        new int[] {1,1,1}
    };

    FlippingAnImage.Algorithm alg = new Algorithm();
    int[][] actual = alg.FlipAndInvertImage(array);

    for (int i = 0; i < actual.GetLength(0); i++)
    {
        CollectionAssert.AreEqual(expected[i], actual[i]);
    }
}

Step6 撰寫功能

我卡最久的地方就是這個地方了,原來可以這樣 source[i] 取得資料 。另外一個原因就是我想說應該可以透過 ArrayCopy 的方式將一維陣列的資料複製到不規則陣列上,只是目前還沒找到可以處理的方式,只好暫且作罷了。

public int[][] FlipAndInvertImage(int[][] source)
{
    int length = source.Length;
    int[][] dest = new int[length][];

    for (int i = 0; i < length; i++)
    {
        int[] result = reverse(invert(source[i]));
        dest[i] = new int[source[i].Length];
        for (int j = 0; j < source[i].Length; j++)
        {
            dest[i][j] = result[j];
        }
    }

    return dest;
}

※後紀

程式寫完之後,就開始查資料看看別人是怎樣寫的,看完之後的想法是,原來還有這樣的解法,只是這樣的解法要怎樣把功能再做分拆進行測試呢?

使用我這種方式撰寫當然就效率來說不會多好,但是根據題目敘述的步驟來一步步撰寫測試,再把所有的功能整合起來再做一次測試,這樣整個功能就完成了。

    Blogger Comment

0 意見: