-
Notifications
You must be signed in to change notification settings - Fork 0
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
1. Two Sum #10
base: master
Are you sure you want to change the base?
1. Two Sum #10
Conversation
``` | ||
# 例外について | ||
- 全ての親であるBaseExceptionクラスの継承クラスにはException、KeyBoardInterruptなどのがあり、大体のエラーはExceptionの継承クラスといえる。ZeroDivisionErrorやOverFlowErrorなどの計算に関する例外はArithmeticErrorというExceptionのサブクラスの、サブクラス(Exceptionから見て孫)となる。 | ||
- 覚える必要はないと思うが、大体の関係性は把握しておきたい。 |
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 | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
numbers_to_indexes = {} |
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.
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.
個人的には num_to_indices が良いと思います。 number という英単語については、入力の変数名が nums と number を省略して複数形にした形のため、省略してしまってよいと思います。また、あくまで個人的な好みになりますが、複数の値が含まれる変数の変数名は、複数の値が含まれることを明示するため、複数形で終えるようにしています。
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.
@nodchip 個人的な好みと強調されているところすみません、こちら、num一つに対しindex一つの1対1のmapだと思うのですが、それでも複数形にするということでしょうか??
(number_to_index) + -es(複数形)というイメージですかね??
numの省略に関しては同意です(統一するのがいいと思います)
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.
はい、変数の中に複数の値が含まれる場合は、変数名の最後の単語を複数形にしています。この規則に違和感を持たれる方がいるであろうことも承知しています。また、チームの平均的な書き方と異なっているのであれば、チームの平均的な書き方に合わせようとも考えています。
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.
@nodchip なるほどです!ありがとうございます!
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.
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.
ありがとうございます。揚げ足取りになって試合申し訳ないのですが、 data は複数形です。数的には大勢に影響はないため、無視していただいて大丈夫だと思います。
https://docs.python.org/ja/3.13/library/exceptions.html#bltin-exceptions | ||
- 一番近いのはValueError。他の方のコードなどをみる限り、やはりそうらしい。 | ||
- raiseはエラーを発生させてるだけだし、実務上では関数呼び出しがtryの中で行われるのかな? | ||
- しばらく経って改めて眺めてみるとraise Exceptionはやばい。広すぎる。 |
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.
私見ですが載せておきます t0hsumi/leetcode#11 (comment)
L76の話、これどういう意味か聞いてもいいですか?
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.
ありがとうございます。
L76は私が例外に対して無知すぎたためかなり初歩的な話なのですが、
raise で例外を発生させる部分だけを今は実装していて、実務ではこれが部品として使われる際は
try:
solution.twoSum(nums, target)
except ValueError as e:
print(e)
などと書かれるのだろう、という話をしています
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
numbers_to_indexes = {} | ||
for i, num in enumerate(nums): |
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.
好みのレベルですが、今回の問題に限ってはindexそのものを扱っているのでiではなくindex
としても良いと思いました。
return [-1, -1] | ||
``` | ||
- 時間計算量:O(n^2) 空間計算量O(1) | ||
- データ量が多くなるとTLEになる可能性が高い。 |
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.
時間計算量から、おおよその実行時間を見積もることができます。詳しくは過去のレビューコメントをご参照ください。
https://discord.com/channels/1084280443945353267/1307605446538039337/1336992875577081917
```python | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
numbers_to_indexes = {} |
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.
個人的には num_to_indices が良いと思います。 number という英単語については、入力の変数名が nums と number を省略して複数形にした形のため、省略してしまってよいと思います。また、あくまで個人的な好みになりますが、複数の値が含まれる変数の変数名は、複数の値が含まれることを明示するため、複数形で終えるようにしています。
discord側に貼ってるのは重々承知&自分が行き来めんどくさいからというだけなのですが、GitHubのdescriptionにもleetcodeのリンクあると嬉しいです。 |
よろしくお願いします。