Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

katataku
Copy link
Owner

Copy link

@hayashi-ay hayashi-ay left a 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]

Choose a reason for hiding this comment

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

せっかくならlocal_nameを使い回さずにそれぞれ別の命名をしてあげても良いかなと思いました。

Copy link

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)として

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アドレス文字列の集合にした方がいいかも。

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)

Choose a reason for hiding this comment

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

自分が関数に切り出すならmailアドレス全体の正規化処理として切り出すかなと思います。

Copy link
Owner Author

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引数は使ったことがなかったが、不正な入力に対して強くするために、今回は使った方が良さそう。

Choose a reason for hiding this comment

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

それでいうと、@が含まれない場合も不正な入力になると思います。splitして返り値のlistの長さを見るとかも選択肢としてありかなと思います。

@hroc135
Copy link

hroc135 commented Dec 24, 2024

拝見しましたが、いいと思いました。コメント含め勉強になります

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.

4 participants