EngTW / English-for-Programmers

《程式英文》:用英文提昇程式可讀性
971 stars 45 forks source link

1528. Shuffle String #77

Closed twy30 closed 3 years ago

twy30 commented 3 years ago

https://leetcode.com/problems/shuffle-string/

public class Solution
{
    public string RestoreString(string s, int[] indices)
    {
        // 「輸入的字串」
        var inputString = s;

        // 「索引值的對映」(複數)
        var indexMaps = indices;

        // 「輸出值」
        var output = new char[inputString.Length];

        for (int i = 0; i < inputString.Length; ++i)
        {
            output[indexMaps[i]] = inputString[i];
        }

        return new string(output);
    }
}

參考資料


請參考「刷 LeetCode 練習命名」 https://github.com/EngTW/English-for-Programmers/issues/69 😊

LPenny-github commented 3 years ago

再麻煩大大給予命名上的建議,感激不盡 orz

using System.Linq;

public class Solution
{
    public class DigitWithIndex
    {
        public char Digit;
        public int Index;
    }

    public string RestoreString(string inputString, int[] indexMaps)
    {
        // 把 inputString 和 預期排序陣列資料 (indexMaps) 兩者結合
        var inputWithIndexMaps= new DigitWithIndex[inputString.Length];

        for (int i = 0; i < inputString.Length; ++i)
        {
            inputWithIndexMaps[i] = new DigitWithIndex
            {
                Digit = inputString[i]
                                        ,
                Index = indexMaps[i]
            };
        }

        // 按照 預期排序陣列資料 重新排序,取出數字做成字串
        return new string(
                    inputWithIndexMaps
                    .OrderBy(character => character.Index)
                    .Select(character => character.Digit)
                    .ToArray());
    }
}
twy30 commented 3 years ago

@LPenny-github

你好 😊


    public class DigitWithIndex
    {
        public char Digit;
        public int Index;
    }

"digit" 在字典上的定義是「數字 (符號)」,在這裡我會稱呼這個 class 為

同理,以下這個 field,

        public char Digit;

我會稱它為 Character


        var inputWithIndexMaps= new DigitWithIndex[inputString.Length];

這個或許可以叫

這是個很有趣的「是什麼(what)」、「為什麼(why)」的案例。 🤔

都可以參考看看 😊

LPenny-github commented 3 years ago

感謝 @twy30 orz

額外好奇,如果:

(即照大大的建議 inputWithIndexMaps 改成 indexedCharacters DigitWithIndex 改成 IndexedCharacter

 var indexedCharacters = new IndexedCharacter[inputString.Length];

這樣還是好的命名/是可以被接受的 嗎?(雖然編輯器有顏色提示,但容易讓 人/我 混淆?)

因為我沒有實務經驗,真的不知道答案。感謝大大 orz

twy30 commented 3 years ago

@LPenny-github

這樣還是好的命名/是可以被接受的 嗎?

如果我們假設讀者讀了 LeetCode 原題、有把這整個解答方法讀完,我主觀覺得那讀者應該能抓到 character, index 這兩個觀念在這個題目下的意義,我會覺得「這種命名是可以接受的」。 🤔

(雖然編輯器有顏色提示,但容易讓 人/我 混淆?)

能否說明一下你覺得哪裡混淆? 😊 (是指 indexedCharacters, IndexedCharacter 兩個很相似的字出現在同一行?)

LPenny-github commented 3 years ago

是指 indexedCharacters, IndexedCharacter 兩個很相似的字出現在同一行?

是的,大大。我期待 變數名稱 與 類別名稱 在命名組成上可以有比較明確的分別。

還是說,這樣的期待不符合實際狀況呢?

twy30 commented 3 years ago

@LPenny-github

是指 indexedCharacters, IndexedCharacter 兩個很相似的字出現在同一行?

是的,大大。我期待 變數名稱 與 類別名稱 在命名組成上可以有比較明確的分別。 還是說,這樣的期待不符合實際狀況呢?

var indexedCharacters = new IndexedCharacter[inputString.Length];

或許對讀寫 C# 程式比較有經驗的人來說,以上列出的幾點就足夠分辨 變數 與 類別 了 😊


有的時候還可能會有這樣的情形 😅

// 類別「標題」
class Title
{
  // ...
}

// 類別「書」
class Book
{
  // 一個名叫「標題」的「標題」 property 😅
  public Title Title { get; set; }
}
LPenny-github commented 3 years ago

@twy30 感謝大大 orz,是我經驗不足(不,是完全沒有 😆)

twy30 commented 3 years ago

@LPenny-github 每個人都是從零開始學起的 😊


題外話,我剛正好看到 Array.Sort() ( https://docs.microsoft.com/en-us/dotnet/api/system.array.sort?view=net-5.0#System_Array_Sort_System_Array_System_Array_ )

你的解法 ( https://github.com/EngTW/English-for-Programmers/issues/77#issuecomment-735623949 ) 可以改寫成這樣:

public class Solution
{
    public string RestoreString(string s, int[] indices)
    {
        var output = s.ToCharArray();
        Array.Sort(indices, output);
        return new string(output);
    }
}
LPenny-github commented 3 years ago

喔喔喔喔喔, @twy30 感謝大大指導 orz