Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1470. Shuffle the Array #72

Closed
twy30 opened this issue Nov 3, 2020 · 9 comments · Fixed by #90
Closed

1470. Shuffle the Array #72

twy30 opened this issue Nov 3, 2020 · 9 comments · Fixed by #90
Labels

Comments

@twy30
Copy link
Contributor

twy30 commented Nov 3, 2020

https://leetcode.com/problems/shuffle-the-array/

public class Solution
{
    public int[] Shuffle(int[] nums, int n)
    {
        // 「輸入的數字」(複數)
        var inputNumbers = nums;

        // 「後半起始索引值」
        var secondHalfStartIndex = n;

        // 「輸出值」(複數)
        var output = new int[inputNumbers.Length];

        for (int i = 0; i < output.Length; ++i)
        {
            output[i] = (i % 2 == 0)
            ? inputNumbers[i / 2]
            : inputNumbers[secondHalfStartIndex + (i - 1) / 2];
        }

        return output;
    }
}

請參考「刷 LeetCode 練習命名」 #69 😊

@LPenny-github
Copy link

又來打擾了 XD
再麻煩大大有空時,給予建議。感激不盡 orz

    public int[] Shuffle(int[] unmodifiedArray, int halfOfArrayLength)
        {
            //製作新容器 List
            List<int> shffledArray = new List<int>(halfOfArrayLength*2);

            for (int i = 0; i < halfOfArrayLength; ++i)
            {
                //每加原始陣列的一個數字後,插入一個 halfOfArrayLength 起算的數字
                shffledArray.Add(unmodifiedArray[i]);
                shffledArray.Add(unmodifiedArray[i + halfOfArrayLength]);
            }
            //將容器的格式轉回成 Array
            return shffledArray.ToArray();
        }

@twy30
Copy link
Contributor Author

twy30 commented Nov 16, 2020

@LPenny-github

又來打擾了 XD

實力就是這樣經由練習累積起來的 😊


    public int[] Shuffle(int[] unmodifiedArray, int halfOfArrayLength)
  • unmodifiedArray 的語意是「未更改過的 array 」,在這個情景下,也可以叫它 originalArray, inputArray, 看你想要強調什麼樣的語意
  • halfOfArrayLength 或許可以把 Array 說的更清楚一些,例如 halfOfOriginalArrayLength
            List<int> shffledArray = new List<int>(halfOfArrayLength*2);
  • shffledArray 的拼字有個小錯誤,少了個 u 😅 應該是 shuffledArray

@LPenny-github
Copy link

@twy30 首先,感謝大大的指教 orz

unmodifiedArray
originalArray
inputArray

我其實不確定這三者的區別 😅:未更改過的 vs. 原始的 vs. 輸入的
記得在 1480. Running Sum of 1d Array 有過這樣的討論,所以才選用 unmodified

(#70 (comment))

        // 2. 先把最後一個數字記下來
        int originalNumber = nums[nums.Length-1];

這裡的「最後一個數字」與 "original" 中間些微落差。

或許可以試試 "last unmodified number", 「最後一個未被更動的數字」。 🤔

但我覺得自己可能沒有搞懂三者真正的差別 🤔


halfOfArrayLength
halfOfOriginalArrayLength

當初是考量到盡量減少用字 🤔 因為,原始 inputArray / unmodifiedArray 和 outputArray / shuffledArray 是相同長度的。
不過,就為了少那一個字,讓人花時間想 哪來的 Array XD 的確兩相權衡下,寫清楚比較好。


shffledArray
shuffledArray

對不起,一時不察 orz 下次會更注意 😆rz

@twy30
Copy link
Contributor Author

twy30 commented Nov 16, 2020

@LPenny-github

不客氣 😊


我其實不確定這三者的區別 😅:未更改過的 vs. 原始的 vs. 輸入的

但我覺得自己可能沒有搞懂三者真正的差別 🤔

這裡沒有「唯一的正確答案」,就看你想要強調什麼語意、想要如何引導你的讀者。

#70 (comment) 裡, nums 是「傳進來的參數 / 原本的數值」,而你的程式碼會去更動(modify) nums 的成員;在那個情景下,我會覺得 "unmodified" 與「那更動(modify)的動作」是更好的對映。

回到這題來,如果「該 int[] 參數」的 用途 就只是拿來作為「輸入(input)資料的來源」,那麼, inputArray 會更貼切一些。

易言之, unmodifiedArray, originalArray, inputArray 這三個名稱都能回答「這個 int[] 變數 什麼(what)」,而 inputArray 能更進一步回答「這個 int[] 變數 為了什麼(why)而存在的」。

@LPenny-github
Copy link

@twy30 感謝大大 orz

@lddr99
Copy link

lddr99 commented Dec 6, 2020

教考卷!

# ruby
def shuffle(input_nums, second_half_start_index)
    first_half_end_index = second_half_start_index - 1
    
     # add the first element in the first half and second half to result every time
    (0..first_half_end_index).inject([]) do |result, index| 
        combie_nums = [input_nums[index], input_nums[second_half_start_index + index]]
        
        result + combie_nums
    end
end

@twy30
Copy link
Contributor Author

twy30 commented Dec 7, 2020

@lddr99

歡迎 😊


        combie_nums = [input_nums[index], input_nums[second_half_start_index + index]]

我猜 combiecombine 的筆誤? 😊 或許原本是想寫 combined_nums

從「是什麼(what)」的角度來看的話,這個變數也可以命名為:

  • num_tuple
  • num_pair

另一個可能性,與題目中的 "shuffle" 稍微有連結:

  • shuffled_num_pair

可以參考看看 😊

@lddr99
Copy link

lddr99 commented Dec 13, 2020

...
從「是什麼(what)」的角度來看的話,這個變數也可以命名為:

  • num_tuple
  • num_pair

另一個可能性,與題目中的 "shuffle" 稍微有連結:

  • shuffled_num_pair

可以參考看看 😊

@twy30 學習了!

想問個問題,一般在看到 tuple 或是 pair 的變數命名結尾時,有辦法大概猜到資料型態是 Array 嗎?或是在 statically typed languages 中 IDE 可以直接提示,所以並不是那麼重要?

@twy30
Copy link
Contributor Author

twy30 commented Dec 13, 2020

@lddr99

一般在看到 tuple 或是 pair 的變數命名結尾時,有辦法大概猜到資料型態是 Array 嗎?

我覺得不一定 🤔

例如說, C# 有 tuple 這個觀念,那習慣使用 C# 的讀者看到 tuple 或許會先想到 C# 的 tuple, 而非 array 。

反過來說,如果在某個專案/語言中,慣例是用 array 來實作 tuple, pair (甚至 list), 那或許讀者會第一時間聯想到 array 。

或是在 statically typed languages 中 IDE 可以直接提示,所以並不是那麼重要?

這大概無法一概而論 🤔 我想到這幾個方向:

  • 「用變數名稱來實作 type hint/annotation 的效益與成本」
  • 「就這個變數的生命週期、使用情景來說,讀者有多需要知道該變數的類別?」
  • 「就這個專案、語言來說,用 array 來實作 tuple, pair 是否已經常見到成為『慣例』 (或是罕見到是為『特例』) ?」

等等。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

3 participants