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

1108. Defanging an IP Address #73

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

1108. Defanging an IP Address #73

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

Comments

@twy30
Copy link
Contributor

twy30 commented Nov 3, 2020

https://leetcode.com/problems/defanging-an-ip-address/

public class Solution
{
    public string DefangIPaddr(string address)
    {
        // 「 IP 位址」
        var ipAddress = address;

        return ipAddress.Replace(".", "[.]");
    }
}

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

@twy30 twy30 added the LeetCode label Nov 3, 2020
@adks3489
Copy link

adks3489 commented Nov 3, 2020

我有時候也會遇到這種情況,會覺得ipAddress跟address比起來,ipAddress有重複上下文的感覺(因為function name已有或class name有),
不知道對於這種情況有沒有什麼衡量、判斷的方法?

@twy30
Copy link
Contributor Author

twy30 commented Nov 3, 2020

@adks3489

我有時候也會遇到這種情況,會覺得ipAddress跟address比起來,ipAddress有重複上下文的感覺(因為function name已有或class name有),
不知道對於這種情況有沒有什麼衡量、判斷的方法?

我還沒有很完整的想法,以下是我想到的兩個方向:

附加的字是否「太多」 (使得資訊變雜訊) ?

以這個案例來說,在 address 前面加 ip, 最後形成的 ipAddress 仍然只有 1 種讀法,整個字也不至於「太長」,所以我會覺得「加上 ip 」是正面大於負面。

有的時候,加字會變成 3 個字,例如 storeManagerName, 這時候有 2 種讀法: storeManager + Namestore + ManagerName ,還算可以接受,但整個字就覺得「有點長」。

有的時候,加字會變成 4 個字,那就會有 n 種讀法,變得很複雜,整個字多半也會太長,這樣子的「加字」大概就會是負面多於正面。

程式碼長度

在這個案例來說,的確可以從 address 就知道它是指 "IP address", 但如果程式碼再長一點,無法一次裝進人腦工作記憶區的話,或許 ipAddress 會比較好。

@LPenny-github
Copy link

再麻煩大大給予命名上的建議 orz

(註解的部分,猶豫很久後決定不放上來。程式碼很簡單,放註解應該會浪費大大們的時間 XD )

總之,非常感謝 orz

            
public string DefangIPaddr(string inputIPAddress)
        {
            string[] IPNumbersArray = inputIPAddress.Split(".");

            string defangedIPAddress = IPNumbersArray[0];

            for (int i = 1; i < IPNumbersArray.Length; ++i)
            {
                defangedIPAddress +=$"[.]{IPNumbersArray[i]}";
            }

            return defangedIPAddress;
         }

@twy30
Copy link
Contributor Author

twy30 commented Nov 17, 2020

@LPenny-github

(註解的部分,猶豫很久後決定不放上來。程式碼很簡單,放註解應該會浪費大大們的時間 XD )

談到「註解」,有興趣的話可以參考這篇文章: https://github.com/EngTW/English-for-Programmers/blob/master/notes/2018-12-30-commenting.md 😊


public string DefangIPaddr(string inputIPAddress)

這裡的語意我覺得沒問題,但這裡有個很有趣的「語法」案例: IP 的大小寫。


            string[] IPNumbersArray = inputIPAddress.Split(".");

@LPenny-github
Copy link

@twy30 非常感謝大大的指教 orz

@lddr99
Copy link

lddr99 commented Dec 13, 2020

...

  • 沒看到像這裡這樣『該「兩個字母的縮寫」是在字中間』的情形的例子 🤔

找到啦!

ContainerUIElement3Dhttps://docs.microsoft.com/en-us/dotnet/api/system.windows.media.media3d.containeruielement3d?view=net-5.0)

@twy30
Copy link
Contributor Author

twy30 commented Dec 13, 2020

@lddr99 十分感謝提供資料 😊

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.

4 participants