Open
Conversation
nodchip
reviewed
Apr 29, 2024
|
|
||
| - splitメソッドを知らなかった | ||
| - $が終わりマークなので、「〇〇以降のものを取り除く」という処理ができる | ||
| - 変数名が、中身が変化していってもずっとlocal_nameと同じ変数名だが、これでいいのかは正直迷った。こういうのは専門家にとって忌避感があるのか?(が、個々に対して適切な変数名が思いつかない) |
|
|
||
| class Solution: | ||
| def numUniqueEmails(self, emails: List[str]) -> int: | ||
| email_set = set() |
There was a problem hiding this comment.
email_set の _set は、変数の型に含まれているため、情報が重複しています。外したほうが良いと思います。 unique_emails はいかがでしょうか?
| local_name, domain_name = email.split('@', 1) | ||
| local_name = re.sub('\.', '', local_name) | ||
| local_name = re.sub('\+.*$', '', local_name) | ||
| email_set.add(str.join(local_name, domain_name)) |
There was a problem hiding this comment.
['ab@c', 'a@bc'] のときに正しい解を返さないように思いました。
|
|
||
| email_set = set() | ||
| for email in emails: | ||
| print(canonicalize(email)) |
There was a problem hiding this comment.
print すると、実行時に標準出力を汚してしまいます。意図して行う場合以外は、提出する前、またはレビューする前に削除しましょう。
oda
reviewed
Apr 29, 2024
| def canonicalize(email): | ||
| if len(email.split('@')) != 2: | ||
| raise Exception('Invalid Inputs') | ||
| local_name = email.split('@')[0] |
There was a problem hiding this comment.
split 2回はちょっと違和感ですね。上みたいに
local_name, domain_name = email.split('@', 1)
のほうがいいでしょう。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
問題文URL: https://leetcode.com/problems/unique-email-addresses/description/
Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'.
For example, in "alice@leetcode.com", "alice" is the local name, and "leetcode.com" is the domain name.
If you add periods '.' between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names.
For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address.
If you add a plus '+' in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered. Note that this rule does not apply to domain names.
For example, "m.y+name@email.com" will be forwarded to "my@email.com".
It is possible to use both of these rules at the same time.
Given an array of strings emails where we send one email to each emails[i], return the number of different addresses that actually receive mails.
Example 1:
Input: emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails.
Example 2:
Input: emails = ["a@leetcode.com","b@leetcode.com","c@leetcode.com"]
Output: 3