-
Notifications
You must be signed in to change notification settings - Fork 0
929. Unique Email Addresses #16
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
xbam326
wants to merge
3
commits into
main
Choose a base branch
from
929
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,34 @@ | ||
| ## step1 | ||
| - 愚直に解くなら一つずつ前から順に見ていき'.'は無視して'+'は'@'が出てくるまでの文字列を無視して処理した後setに追加していく | ||
| - 100*100なら上記の方針でいいと思うO(N^2) | ||
| - '.'があるか全て見ないといけないのでこれ以上の高速化は難しそう | ||
| - 今回はlowercaseのみだが、他の文字が入ってきた場合どうするかは考えたい | ||
| - 登録処理などで使えない文字を無視したまま登録できるとユーザとシステム側で認識がずれる問題があるので、使用できる文字だけで構成されているかの確認は必要 | ||
| - 元々+の仕様は知っていたが.の仕様は知らなかった | ||
| - 一旦実装する | ||
| - 処理した後のアドレスの一般的な呼び方がついていると思い調べたが、なかった | ||
| - normalized email address/canonical email address | ||
|
|
||
| - ローカルパートとドメインパートでルールが異なるので分けたい気持ちになったがドメインパートはそのままくっつけるだけなのでコメントで補足に留めた(より複雑なルールに対応する時にローカルとドメインでそれぞれ関数にしてもらうつもり) | ||
|
|
||
| ## step2 | ||
| - 他の人のコードを読む | ||
| - https://github.com/Hiroto-Iizuka/coding_practice/pull/14/files | ||
| - partition() | ||
| - https://docs.python.org/ja/3/library/stdtypes.html#str.partition | ||
| - 区切り文字を含めた3要素のタプルが返却される | ||
| - 区切り文字がない場合は真ん中の要素に空文字列が入る | ||
| - いきなり`(local_name, _, domain_name) = email.partition("@")`でいいと思った | ||
| - 真ん中がいらないなら`split('@')`だが@が入っていない場合問題が出てくるので`local_name, domain_name, *rest = email.split("@")`よりもpartitionの方が良さそうに思った | ||
| - for文で書くよりこっちの方がわかりやすいと思った | ||
| - https://github.com/mamo3gr/arai60/pull/14/files | ||
| - 走査を1回にした場合のような感じでstep1を解いていた | ||
| - step1で十分わかりやすく感じた | ||
| - `あ、あと、文字列の追記は文字列の再構築が走るので、(CPython は最適化されるみたいですが、)指摘されたらリストに append して join ですね。` | ||
| - 短くてわかりやすいくらいの理由で+=で書いていたが理由がなければ .joinで書くようにする(長い文字列を都度再構築させていないかという不安を感じさせない) | ||
| - `あくまで個人的な感覚ですが、 try except は重そうという印象があります。実際にどの程度重いかは実装してみないと何とも言えません。個人的には、エラーコード等で代替するなど、現実的なコストで避けられるのであれば避けたいです。この辺りは所属するチームの平均的な書き方に合わせることをお勧めいたします。` | ||
| - try-exceptは重そうという感覚を持っておく | ||
|
|
||
|
|
||
| ## step3 | ||
| - エラー文考えるの毎回手間でAIに丸投げしているが、チーム全体である程度の足並みは揃えないと書き方がバラバラになりそうだなと思った。 | ||
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 @@ | ||
| # | ||
| # @lc app=leetcode id=929 lang=python3 | ||
| # | ||
| # [929] Unique Email Addresses | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def numUniqueEmails(self, emails: list[str]) -> int: | ||
| unique_emails = set() | ||
|
|
||
| for email in emails: | ||
| normalized_email = "" | ||
| is_after_plus = False | ||
| email.partition("@") | ||
| for i, c in enumerate(email): | ||
| # ドメインパートのルール | ||
| if c == "@": | ||
| normalized_email.join(email[i:]) | ||
| break | ||
|
|
||
| # ローカルパートのルール | ||
| if c == "." or is_after_plus: | ||
| continue | ||
| if c == "+": | ||
| is_after_plus = True | ||
| continue | ||
|
|
||
| normalized_email.join(c) | ||
|
|
||
| unique_emails.add(normalized_email) | ||
|
|
||
| return len(unique_emails) | ||
|
|
||
|
|
||
| # @lc code=end |
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,31 @@ | ||
| # | ||
| # @lc app=leetcode id=929 lang=python3 | ||
| # | ||
| # [929] Unique Email Addresses | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def numUniqueEmails(self, emails: list[str]) -> int: | ||
| unique_emails = set() | ||
| for email in emails: | ||
| split_result = email.split("@") | ||
| if len(split_result) != 2: | ||
| raise ValueError("email must contain exactly one '@'.") | ||
| local_part, domain_part = split_result | ||
|
|
||
| local_part_before_plus = local_part.partition("+")[0] | ||
| canonicalized_local_part = local_part_before_plus.replace(".", "") | ||
|
|
||
| if not len(canonicalized_local_part) or not len(domain_part): | ||
| raise ValueError("local and domain names must not be empty") | ||
|
|
||
| if not domain_part.endswith(".com"): | ||
| raise ValueError("email must end with '.com'.") | ||
|
|
||
| unique_emails.add(f"{canonicalized_local_part}@{domain_part}") | ||
|
|
||
| return len(unique_emails) | ||
|
|
||
|
|
||
| # @lc code=end |
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,34 @@ | ||
| # | ||
| # @lc app=leetcode id=929 lang=python3 | ||
| # | ||
| # [929] Unique Email Addresses | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def numUniqueEmails(self, emails: list[str]) -> int: | ||
| unique_emails = set() | ||
|
|
||
| for email in emails: | ||
| split_result = email.split("@") | ||
|
|
||
| if len(split_result) != 2: | ||
| raise ValueError('Email must have one "@".') | ||
|
Comment on lines
+15
to
+16
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. 入力データにひとつでも仕様外のものがあると、全体の処理が止まってしまいます。これが実務上いいかどうかの考察があると良いでしょう。 |
||
|
|
||
| local_part, domain_part = split_result | ||
|
|
||
| local_part_before_plus = local_part.partition("+")[0] | ||
| canonicalized_local_part = local_part_before_plus.replace(".", "") | ||
|
|
||
| if not canonicalized_local_part or not domain_part: | ||
| raise ValueError("local part or domain part is empty.") | ||
|
|
||
| if not domain_part.endswith(".com"): | ||
| raise ValueError('Email must end with ".com".') | ||
|
|
||
| unique_emails.add(f"{canonicalized_local_part}@{domain_part}") | ||
|
|
||
| return len(unique_emails) | ||
|
|
||
|
|
||
| # @lc code=end | ||
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.
Pythonの場合、例外が発生しなければオーバーヘッドはほぼ無し、発生したらスタックトレースを収集するので結構遅くなるそうです。