TDD LeetCode:804. Unique Morse Code Words

RojerChen.2018.09.17

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a"maps to ".-""b" maps to "-...""c" maps to "-.-.", and so on.
For convenience, the full table for the 26 letters of the English alphabet is given below:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-.-....-", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.
Return the number of different transformations among all words we have.
Example:
Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
Explanation: 
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."

There are 2 different transformations, "--...-." and "--...--.".
Note:
  • The length of words will be at most 100.
  • Each words[i] will have length in range [1, 12].
  • words[i] will only consist of lowercase letters.

Step0 題目說明

1.有一個文字編碼表,「a」轉成「.-」,將「b」轉成「-...」
2.有一個字串,「gin」 轉變成 「--...-.」
3.有一個字串陣列,把這些文字編碼,編碼後看看不重複的數量有多少

Step1建立密碼對照表測試案例

從說明中,我們知道要把「a」轉成「.-」,將「b」轉成「-...」,將「c」轉成「-.-.」,所以我們先撰寫以下測試案例
[TestMethod]
public void A804_TestMethod1()
{
    string input = "a";
    string expected = ".-";
    Algorithm alg = new Algorithm();
    string actual = alg.morseCodeMapping(input);
    Assert.AreEqual(expected,actual);
}

[TestMethod]
public void A804_TestMethod2()
{
    string input = "b";
    string expected = "-...";
    Algorithm alg = new Algorithm();
    string actual = alg.morseCodeMapping(input);
    Assert.AreEqual(expected, actual);
}

[TestMethod]
public void A804_TestMethod3()
{
    string character = "c";
    string expected = "-.-.";
    Algorithm alg = new Algorithm();
    string actual = alg.morseCodeMapping(character);
    Assert.AreEqual(expected, actual);
}

Step2 撰寫字串編碼對照表

測試案例寫完之後開始寫對應程式,由於資料轉換是從 a 開始,而 a 的 ASCII 為 97,所以程式碼大概如下
public string morseCodeMapping(string input)
{
    String[] array = new[]
    {
        ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
        ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
    };

    int value = (int)(Convert.ToChar(input));
    return array[value - 97];
}

Step3 字串編碼測試程式

單一文字編碼無誤後,開始寫字串編碼的測試程式
[TestMethod]
public void A804_Encode1()
{
    string input = "gin";
    string expected = "--...-.";
    Algorithm alg = new Algorithm();
    string actual = alg.encode(input);
    Assert.AreEqual(expected, actual);
}

[TestMethod]
public void A804_Encode2()
{
    string input = "zen";
    string expected = "--...-.";
    Algorithm alg = new Algorithm();
    string actual = alg.encode(input);
    Assert.AreEqual(expected, actual);
}

[TestMethod]
public void A804_Encode3()
{
    string input = "gig";
    string expected = "--...--.";
    Algorithm alg = new Algorithm();
    string actual = alg.encode(input);
    Assert.AreEqual(expected, actual);
}

[TestMethod]
public void A804_Encode4()
{
    string input = "msg";
    string expected = "--...--.";
    Algorithm alg = new Algorithm();
    string actual = alg.encode(input);
    Assert.AreEqual(expected, actual);
}

Step4 編碼程式

public string decode(string input)
{
    string output = string.Empty;

    for (int i = 0; i < input.Length; i++)
    {
        output += morseCodeMapping(input.Substring(i, 1));
    }

    return output;
}

Step5 輸入程式說明測試範例 cab

[TestMethod]
public void A804_Encode5()
{
    string input = "cab";
    string expected = " -.-.-....- ";
    Algorithm alg = new Algorithm();
    string actual = alg.encode(input);
    Assert.AreEqual(expected, actual);
}
測試失敗Assert.AreEqual 失敗。 預期: < -.-.-....- >。實際: <-.-..--...>
會發生測試失敗的原因是因為題目的說明範例寫錯了,所以會測試失敗。

Step6 撰寫整功能的測試案例

[TestMethod]
public void A804_Case1()
{
    string[] input = new[] {"gin", "zen", "gig", "msg"};
    int expected = 2;
    Algorithm alg = new Algorithm();
    int actual = alg.UniqueMorseRepresentations(input);
    Assert.AreEqual(expected, actual);
}

Step7 撰寫 UniqueMorseRepresentations 的邏輯程式

public int UniqueMorseRepresentations(string[] words)
{
    List<string> result = new List<string>();
    for (int i = 0; i < words.Length; i++)
    {
        result.Add(decode(words[i]));
    }

    return result.Distinct().Count();
}

    Blogger Comment

0 意見: