Conversation
torotoki
left a comment
There was a problem hiding this comment.
コメントしてみました。全体的に良かったと思います。(なのでコメントは細かい点かもしれません)
|
|
||
| 問題設定上 `return []` には到達しない。例外を返すか異常終了させるかも迷ったがこうしてみた。使われ方によって選択を変えるだろう。 | ||
|
|
||
| remainingじゃなくてrestの方がより合ってる?そもそも形容詞だから微妙か。discord上の議論を見てcomplementもいいかもと思った。 |
There was a problem hiding this comment.
restは名詞でも使うので変じゃないと思います: https://eow.alc.co.jp/search?q=rest
There was a problem hiding this comment.
ちなみに私はexceptionを返すのが好みですが、ここは本当にユースケースによりますよね
There was a problem hiding this comment.
remainingじゃなくてrestの方がより合ってる?そもそも形容詞だから微妙か
これremainingについての言及でした (分かりづらい)
| def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
| for i in range(len(nums)): | ||
| for j in range(i+1, len(nums)): | ||
| if nums[i]+nums[j] == target: |
There was a problem hiding this comment.
pep8を把握しているなら全然問題ないと思いますが、syntax checkerを回すとnums[i] + nums[j]に直されそうなコードかもしれません
There was a problem hiding this comment.
https://peps.python.org/pep-0008/#other-recommendations
If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies). Use your own judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator:
# Correct: i = i + 1 submitted += 1 x = x*2 - 1 hypot2 = x*x + y*y c = (a+b) * (a-b) # Wrong: i=i+1 submitted +=1 x = x * 2 - 1 hypot2 = x * x + y * y c = (a + b) * (a - b)
いまいちこのルールよく分かってないんですが、多分どちらでも良いんだろうという解釈をしています (実際の開発ではformatterに従うだけなので良いですが)
There was a problem hiding this comment.
Google Style Guideの方はこう書いてありますね。
https://google.github.io/styleguide/pyguide.html#36-whitespace
Surround binary operators with a single space on either side for assignment (=), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), and Booleans (and, or, not). Use your better judgment for the insertion of spaces around arithmetic operators (+, -, *, /, //, %, **, @).
There was a problem hiding this comment.
ありがとうございます。Google Style Guideも算術演算子周りのスペースは決められてないんですね
| if remaining in num_to_index: | ||
| return [num_to_index[remaining], i] | ||
| num_to_index[num] = i | ||
| return [] # never reached |
There was a problem hiding this comment.
すごく細かいんですけど、これだと返り値の型と合わなくなるんですかね。
かといって[-1, -1]みたいなのを返すのもアレなので、例外か落とした方が自然なのかしら。
There was a problem hiding this comment.
これ返り値の型と合わなくなるんですか?空のリストもList[int]だと思いますが。
There was a problem hiding this comment.
すみません、変なことを言っているかもしれません。
List[Optional[int]]なら良いのかなと思っていたのですが、[] ∈ List[int]でしたっけ。
There was a problem hiding this comment.
Optional[int] は int | None (https://docs.python.org/3/library/typing.html#typing.Optional) なので、たとえば [1, None] みたいなやつだと思います。
[] ∈ List[int]でしたっけ。
mypyとかでチェックされてみるといいかもしれないですね
https://leetcode.com/problems/two-sum/description/