-
Notifications
You must be signed in to change notification settings - Fork 0
Create Unique Email Addresses.md #4
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Step1 | ||
何も見ずに実行 | ||
```python | ||
class Solution: | ||
def numUniqueEmails(self, emails: List[str]) -> int: | ||
valid_emails = [] | ||
for email in emails: | ||
local, domain = email.split("@") | ||
local = local.replace(".", "") | ||
local = local.split("+")[0] | ||
email = local+"@"+domain | ||
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. 書き方として |
||
if email not in valid_emails: | ||
valid_emails.append(email) | ||
return len(valid_emails) | ||
``` | ||
emailをlocalとdomainに分割し,ルールに従い,domainを実際に有効な形式に変換した.単純にリストにappendして数えたが,dictを使ったらより計算効率が上がるかもしれない. | ||
|
||
# Step2 | ||
他の人の解答を参照する. | ||
https://github.com/potrue/leetcode/pull/14/files#diff-6a8efe56493bdbfeb045b7aa123660406ebdd5dd4f0a011935562d4eefb9ab46 | ||
|
||
上記を見ると,概ね実装方針は同じだった.ただ,以下が参考になった. | ||
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. 1文字ずつ処理する方法も選択肢として持って良いと思います! |
||
- listの代わりにset()を使っていたこと.setは重複を排除する. | ||
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. 要素数 n の list では in は O(n) かかります。set の場合は O(1) という違いもあります。 |
||
- @が含まれない場合も考慮して,partitionを使用していたこと.partitionは区切り文字も返す. | ||
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. Step3では、@ が含まれない場合、全体を 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. そういう意味だったのですね.誤った認識をしてました.ご指摘ありがとうございます. |
||
|
||
また,RFC規格という存在を知った.https://info.yamap.com/archives/3434 | ||
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. 私も間違った認識をしていましたが、 RFCは規格ではないようです. 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. 規格ではなく,文書ということですね. |
||
|
||
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. 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. 初めて正規表現の概念を知りました.正規表現を使って解き直してみます.ありがとうございます. |
||
# Step3 | ||
他の人の解答を参考に解き直す | ||
```python | ||
class Solution: | ||
def numUniqueEmails(self, emails: List[str]) -> int: | ||
valid_emails = set() | ||
for email in emails: | ||
local, at, domain = email.partition("@") | ||
local = local.replace(".", "") | ||
local = local.split("+")[0] | ||
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. 先に無視される部分を決めた方が(+の処理を先に行う)効率的ではないでしょうか。 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. 仰るとおりですね.ありがとうございます. |
||
email = local + at + domain | ||
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. partitionするときにlist型で取っておいてjoinすることもできそうですね |
||
valid_emails.add(email) | ||
return len(valid_emails) | ||
``` | ||
# Step4 | ||
いただいたアドバイスを元に修正 | ||
```python | ||
class Solution: | ||
def numUniqueEmails(self, emails: List[str]) -> int: | ||
valid_emails = set() | ||
for email in emails: | ||
local, at, domain = email.partition("@") | ||
local = local.split("+")[0] | ||
local = local.replace(".", "") | ||
email = f"{local}@{domain}" | ||
valid_emails.add(email) | ||
return len(valid_emails) | ||
``` | ||
|
||
正規表現を使って解き直してみる | ||
```python | ||
class Solution: | ||
def numUniqueEmails(self, emails: List[str]) -> int: | ||
valid_emails = set() | ||
for email in emails: | ||
local, domain = email.split('@') | ||
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. re.matchやre.fullmatchあたりを使うこともできると思います |
||
local = re.sub(r'\+.*', '', local) | ||
local = re.sub(r'\.', '', local) | ||
email = f'{local}@{domain}' | ||
valid_emails.add(email) | ||
return len(valid_emails) | ||
``` | ||
|
||
- r'.'はすべての文字列を対象とする | ||
- r'\.'は.自体を対象とする | ||
- [\w\.-]+@[\w\.-]+\.\w+とするとemailアドレスの識別に使えるらしい.本問題では,localに.が2つある場合適用できないため,re.subで対応した | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
私はf-stringの方が結果の文字列が想像しやすくて好きです。Googleのスタイルガイドにも、
とあり、私個人は、スタイルガイドにあるということは多くの人がf-stringの方を好ましく思うのだろう、と想定しています。