-
Notifications
You must be signed in to change notification settings - Fork 0
929. Unique Email Addresses #6
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
tsadamor
wants to merge
1
commit into
main
Choose a base branch
from
0929-unique-email-addresses
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+174
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # 929. Unique Email Addresses | ||
|
|
||
| - localname@domainnameのメールアドレスが与えられる。 | ||
| - **localname**について、 | ||
| - ピリオド('.')が含まれるなら、含まれていないアドレスと同じアドレスと認識される。 | ||
| - ex. "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address. | ||
| - プラス('+')が含まれるなら、プラス以降の文字列がないアドレスと同じアドレスと認識される。 | ||
| - "m.y+name@email.com" will be forwarded to "my@email.com". | ||
| - メールアドレスのリストが与えられたとき、実際にメールを受け取る(ユニークと認識される)アドレスの数を返せ。 | ||
|
|
||
|
|
||
| ## Step1 | ||
| 1. リストを回しながら、各アドレス@でsplit | ||
| 2. localnameを走査して、ドットがあったら無視、プラスがあったらbreakしながらアップデートしたアドレスをつくる。 | ||
| 3. それらをsetに放り込んで、その長さを返せば良さそう。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def numUniqueEmails(self, emails: list[str]) -> int: | ||
| unique_emails = set() | ||
|
|
||
| for email in emails: | ||
| email = email.split('@') | ||
| local = email[0] | ||
| domain = email[1] | ||
|
|
||
| parsed_email = [] | ||
| for c in local: | ||
| if c == '.': | ||
| continue | ||
| elif c == '+': | ||
| break | ||
| else: | ||
| parsed_email.append(c) | ||
| parsed_email.extend(domain) | ||
| unique_emails.add(tuple(parsed_email)) | ||
|
|
||
| return len(unique_emails) | ||
| ``` | ||
|
|
||
| - set()にlistを渡そうとしたらunhashableと怒られて、tuple()にした。勉強になった。 | ||
| - 時間計算量はリストの入力をN、文字列の長さをMとしてO(NM)、空間計算量は再生成分でO(n)。リストもアドレスもlength <= 100なので特に問題なさそう。 | ||
|
|
||
|
|
||
| ## Step2 | ||
| ### AI | ||
| - split()はアンパックにすべき。 | ||
| ```python | ||
| email = email.split('@') | ||
| local = email[0] | ||
| domain = email[1] | ||
| ↓ | ||
| local, domain = email.split('@') | ||
| ``` | ||
| - `elif`, `else`は不要。 | ||
| `continue`, `break`でそのループを抜けるから。 | ||
| - タプル変換と文字列連結で処理量は同じなので、データ型も文字列に合わせたほうがよい。 | ||
| 文字列連結を絶対悪だと思っていたが、たしかに今回は`+=`による再生成ではなく、素直につなぐほうが見やすそう。 | ||
| ```python | ||
| parsed_email.extend(domain) | ||
| unique_emails.add(tuple(parsed_email)) | ||
| ↓ | ||
| parsed_local = ''.join(parsed_local) | ||
| unique_emails.add(parsed_local + '@' + domain) | ||
| ``` | ||
|
|
||
| ### 先達 | ||
| - [ユースケースの想定](https://discord.com/channels/1084280443945353267/1251052599294296114/1254245440690589787) | ||
| - この次元では考えられていなかった。 | ||
| - 今回の自分のコードはエラーを吐いていないので(結果として)サービス向け、[データサイエンスならそうでもない](https://discord.com/channels/1084280443945353267/1355903616032178327/1370028729186910269) | ||
| - 同じような話として、今回は'@'がひとつと保証されているけれど、実際の使用を想定するなら`split('@', 1)`にしたほうがよさそう | ||
|
|
||
| - [正規表現](https://github.com/X-XsleepZzz/leetcode/pull/15/changes) | ||
| ```python | ||
| plus_ignored_local_name = re.sub(r"\+.*", "", local_name) | ||
| dot_removed_local_name = re.sub(r"\.", "", plus_ignored_local_name) | ||
|
|
||
| normalized_set.add(f"{dot_removed_local_name}@{domain_name}") | ||
| ``` | ||
| - 正規表現を知っていれば処理内容を追いやすいかも? | ||
|
|
||
| ### ブラッシュアップ | ||
| ```python | ||
| class Solution: | ||
| def numUniqueEmails(self, emails: list[str]) -> int: | ||
| unique_emails = set() | ||
|
|
||
| for email in emails: | ||
| local, domain = email.split('@', 1) | ||
|
|
||
| parsed_local = [] | ||
| for c in local: | ||
| if c == ".": | ||
| continue | ||
| if c == "+": | ||
| break | ||
| parsed_local.append(c) | ||
| parsed_local = "".join(parsed_local) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parsed_localの型が変わっているので,すこし戸惑いますね. normalized_local, canonicalized_localなどの変数を宣言するなどしてもいいかもしれません
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. たしかに型が変わるときに同じ名前に再代入すべきではないですね。ありがとうございます。 |
||
| unique_emails.add(parsed_local + '@' + domain) | ||
|
|
||
| return len(unique_emails) | ||
| ``` | ||
|
|
||
| ## Step3 | ||
| 上記コードを三回再現。 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| class Solution: | ||
| def numUniqueEmails(self, emails: list[str]) -> int: | ||
| unique_emails = set() | ||
|
|
||
| for email in emails: | ||
| email = email.split('@') | ||
| local = email[0] | ||
| domain = email[1] | ||
|
|
||
| parsed_email = [] | ||
| for c in local: | ||
| if c == '.': | ||
| continue | ||
| elif c == '+': | ||
| break | ||
| else: | ||
| parsed_email.append(c) | ||
| parsed_email.append('@') | ||
| parsed_email.extend(domain) | ||
| unique_emails.add(tuple(parsed_email)) | ||
|
|
||
| return len(unique_emails) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| emails1 = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"] | ||
| emails2 = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"] | ||
|
|
||
| Solver = Solution() | ||
|
|
||
| print(f"Expected: 2 Actual: {Solver.numUniqueEmails(emails1)}") | ||
| print(f"Expected: 3 Actual: {Solver.numUniqueEmails(emails2)}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| class Solution: | ||
| def numUniqueEmails(self, emails: list[str]) -> int: | ||
| unique_emails = set() | ||
|
|
||
| for email in emails: | ||
| local, domain = email.split('@', 1) | ||
|
|
||
| parsed_local = [] | ||
| for c in local: | ||
| if c == ".": | ||
| continue | ||
| if c == "+": | ||
| break | ||
| parsed_local.append(c) | ||
| parsed_local = "".join(parsed_local) | ||
| unique_emails.add(parsed_local + '@' + domain) | ||
|
|
||
| return len(unique_emails) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| emails1 = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"] | ||
| emails2 = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"] | ||
|
|
||
| Solver = Solution() | ||
|
|
||
| print(f"Expected: 2 Actual: {Solver.numUniqueEmails(emails1)}") | ||
| print(f"Expected: 3 Actual: {Solver.numUniqueEmails(emails2)}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 address豆知識として,RFCで一応フォーマットが定められていることは知っておいてもいいかもです.
あとはコーディングテストでは使うことを想定されていないと思いますが,
標準ライブラリにあるので,実務上はこちらを使うべきと思います.このページ,RFCとかについても言及されているので,ざっと眼を通すとよさそうです
https://docs.python.org/3/library/email.html
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.
ざっと見てみましたが,parserはあるものの,addressのvalidationに相当する関数がなかったかもしれません.
ので,この部分は無視してください
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.
RFCが種々のプロトコルを定めているということも知らなかったので勉強になりました。