-
Notifications
You must be signed in to change notification settings - Fork 0
Unique Email Addresses.md #13
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1文字ずつ処理していく方法や正規表現とかも選択肢の幅として解いても良いかなと思います。
class Solution: | ||
def numUniqueEmails(self, emails: List[str]) -> int: | ||
def normalize_local_name(local_name: str) -> str: | ||
local_name = local_name.split('+')[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
せっかくならlocal_name
を使い回さずにそれぞれ別の命名をしてあげても良いかなと思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python わりと使い回す人多い印象がありますね。(いいかはともかく。)
|
||
# Step 3 | ||
|
||
- N: len(emails), M: len(local_name)として |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mがemail自体の長さでなくて、local_nameの長さなのが気になりました。
|
||
書いた後に気づいたこと。`unique_emails`という名前ならば、シンプルなmailアドレス文字列の集合であって欲しい気がした。 | ||
- 今回は、`(local_name, domain_name)`というタプルなので、驚きを最小にするために型ヒント書いてみた。 | ||
- けど、ここまでするなら素直に結合してmailアドレス文字列の集合にした方がいいかも。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分もこっち派です。上から読んでいって型を見たときになんでtupleなんだろうと思いました
|
||
unique_emails = set() | ||
for email in emails: | ||
local_name, domain_name = email.rsplit('@', maxsplit=1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分が関数に切り出すならmailアドレス全体の正規化処理として切り出すかなと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上のコメントと合わせですが、(みなさんが多くやってるように)
- アドレス全体を正規化処理する
- メールアドレス文字列をsetで管理する
が素直でしたね。。。ありがとうございます!
|
||
- docsに眼を通す | ||
- https://docs.python.org/3.12/library/stdtypes.html#str.split | ||
- maxsplit引数は使ったことがなかったが、不正な入力に対して強くするために、今回は使った方が良さそう。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
それでいうと、@が含まれない場合も不正な入力になると思います。splitして返り値のlistの長さを見るとかも選択肢としてありかなと思います。
拝見しましたが、いいと思いました。コメント含め勉強になります |
https://leetcode.com/problems/unique-email-addresses/description/