-
Notifications
You must be signed in to change notification settings - Fork 0
Unique email addresses #30
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: arai60
Are you sure you want to change the base?
Conversation
| def numUniqueEmails(self, emails: List[str]) -> int: | ||
| 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.
rsplit() をあえて選ぶ必要はないと思います。 split() のほうが良いと思います。
@ は 1 つしか含まれないため、 maxsplit = 1 の指定もいらないと思います。
キーワード引数を指定する際の = の両隣のスペースは、消すことをお勧めいたします。
https://peps.python.org/pep-0008/#other-recommendations
Don’t use spaces around the = sign when used to indicate a keyword argument, or when used to indicate a default value for an unannotated function parameter:
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つですが、一応、RFC 上は、複数持てるので、これでいいんじゃないでしょうか。
(RFC を見よう、標準マニュアルを確認しようという教育的な意図も入っていますが。)
あと、本当はセキュリティー上、メールアドレスはユーザーから渡されるものである可能性が高く、ゴミを渡されたときに落ちるプログラムにしてはいけないですね。
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_email_strings.append(email[prev_cursor:cursor]) | ||
| split_email_strings.append(email[cursor:]) | ||
| valid_emails.add("".join(split_email_strings)) | ||
|
|
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.
各emailに対する処理は、関数化した方が読みやすいと思いました。
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.
phase1のところ確かに何やってるのかわかりにくいですね...ありがとうございます!
| local_name, domain_name = email.rsplit("@", maxsplit = 1) | ||
| local_name = local_name.replace(".", "") | ||
| 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.
Python あまり詳しくないのですが、文字列型って immutable でしたよね?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.
split("+") が2番目でもいいかも知れませんね。
現実的には、CPython の場合はこう書くと native code で動くと思われるので、ループを回すよりも速いでしょう。(試してみるといいと思います。)
https://github.com/python/cpython/blob/main/Objects/stringlib/split.h
あと、正直、ここは速度を取る理由があまりないです。どれくらい速くて、どれくらいよいのでしょうか。
問題
929. Unique Email Addresses