Conversation
| ```py | ||
| class Solution: | ||
| def numUniqueEmails(self, emails: List[str]) -> int: | ||
| def parse_local_name(local_name): |
There was a problem hiding this comment.
関数名から、どのような値が返ってくるのかが分かりにくく感じました。比較のために正規化する意味合いを込めて、 canonicalize() はいかがでしょうか?引数の名前が local_name なのであれば、関数名に重複して入れる必要はないと思います。
| if local_name[0] == '+': | ||
| raise ValueError("local name start with plus operator.") | ||
|
|
||
| return local_name.split('+')[0].replace('.', '') |
There was a problem hiding this comment.
圧縮され過ぎていて分かりにくく感じました。適宜分割したほうが良いと思います。
before_plus = local_name.split('+')[0]
return before_plus.replace('.', '')| return len(unique_emails) | ||
| ``` | ||
| `+`による文字列の結合はオブジェクトの再生成を伴うので効率が悪い. ユニークな数がわかればいいのでタプルをキーにすれば良さそう. | ||
| 平均7 ms => 3 msほどに改善 |
There was a problem hiding this comment.
LeetCode 上での計測でしょうか? LeetCode の時間計測は誤差が大きいため、あまり信用しないほうが良いと思います。時間を計測するのであれば、ローカルで十分な回数実行し、平均値や中央値を取ったほうが良いと思います。
また、ミリ秒単位の実行時間がボトルネックにならないのであれば、実行時間ではなく、コードの可読性やメンテナンス性等、別の部分に注力したほうが良いと思います。さらに言えば、ミリ秒単位の実行時間を気にするシチュエーションなら、 C++ 等、 Python より高速な言語を使うことをおすすめします。
| continue | ||
|
|
||
| if c == '+': | ||
| first_plus_appeared = True |
There was a problem hiding this comment.
ここで break したほうが、 first_plus_appeared も消せて、シンプルになると思います。
| if not domain_name.endswith(".com"): | ||
| return | ||
|
|
||
| if domain_name[:-4] == "": |
There was a problem hiding this comment.
-4がマジックナンバーになっていると感じました。(どこ由来かな?と考えました。)
".com"の長さであることが分かるようにするのが個人的に良いと思いました。
| if domain_name[:-4] == "": | |
| if domain_name == len(".com"): |
解く問題
Unique Email Addresses
次に解く問題
First Unique Character In A String