Skip to content

Conversation

@SuperHotDogCat
Copy link
Owner

def numUniqueEmails(self, emails: List[str]) -> int:
unique_emails = set()
for email in emails:
local_name, domain_name = email.rsplit("@", maxsplit = 1)
Copy link

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:

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この問題では @ は1つですが、一応、RFC 上は、複数持てるので、これでいいんじゃないでしょうか。
(RFC を見よう、標準マニュアルを確認しようという教育的な意図も入っていますが。)

あと、本当はセキュリティー上、メールアドレスはユーザーから渡されるものである可能性が高く、ゴミを渡されたときに落ちるプログラムにしてはいけないですね。

Copy link

@Yoshiki-Iwasa Yoshiki-Iwasa Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oda

一応、RFC 上は、複数持てるので

すいません、これってどこから読み取れますか(?? )
RFC5322 を見てます

addr-spec = local-part "@" domain

ここ以外で、@が許容されている構成要素が見当たらず。

split_email_strings.append(email[prev_cursor:cursor])
split_email_strings.append(email[cursor:])
valid_emails.add("".join(split_email_strings))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

各emailに対する処理は、関数化した方が読みやすいと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phase1のところ確かに何やってるのかわかりにくいですね...ありがとうございます!

Comment on lines +5 to +7
local_name, domain_name = email.rsplit("@", maxsplit = 1)
local_name = local_name.replace(".", "")
local_name = local_name.split("+")[0]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python あまり詳しくないのですが、文字列型って immutable でしたよね?local_name の生成が三回行われてしまうので、そうならないように工夫してもいいかもと思いました。

Copy link

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

あと、正直、ここは速度を取る理由があまりないです。どれくらい速くて、どれくらいよいのでしょうか。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants