-
Notifications
You must be signed in to change notification settings - Fork 0
2. Add Two Numbers.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?
Conversation
いいと思います! |
|
||
```python | ||
class Solution: | ||
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: |
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.
なんかうまく言語化できないんですけど、結構読みづらいなと思いました。
L90のnode.valに何が入るかを知るのに、以下の5ステップをちゃんと追わないといけなくてこの短い行数でも結構しんどかったです。
- L81でnode.nextに
ListNode(carry, None)
でcarryが初期値として渡され - L82でそれがnodeに代入されて
- L84でl1.valで足されて
- L87でl2.valが足されて
- 最後にL90で10で割った余りが入ってくる
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.
あとはループの中でいきなりnodeが進んで意味が変わるのもちょっと難しかったです。
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.
ありがとうございます!率直なフィードバックありがたいです。
コメントを見て、やっぱり、読み手としはsum = getValue(l1) + getValue(l2) + carry
のような、
- 「足し算の結果の桁数を入れるよ」の意味が伝わるような変数を用意する。
- 素直に足し算処理はまとめて書く。Node作ったり進めたりするのは足し算の処理とごっちゃにしない。
の方がわかりやすいのかなと気づきました。Step4として書き直してみます。
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.
https://discord.com/channels/1084280443945353267/1200089668901937312/1206180274442993694
あたりから主役が変わる話についてしています。
carry = 0 | ||
node = sentinel | ||
while l1 is not None or l2 is not None or carry > 0: | ||
sum = _get_value(l1) + _get_value(l2) + carry |
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.
sum
はbuiltin関数なので使用はできるかぎり避けておくほうが無難だと思います。
https://docs.python.org/3/library/functions.html#sum
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.
2. Add Two Numbers.md
Outdated
@@ -0,0 +1,124 @@ | |||
|
|||
URL: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/ |
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 addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
sentinel = ListNode(0,None) |
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.
,
のあとにスペースを空けることをおすすめいたします。
また、第一引数のデフォルト値が 0、第二引数のデフォルト値が None のため、引数は渡さなくてよいと思います。
```python | ||
class Solution: | ||
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
def _get_value(node): |
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.
inner function の関数名の先頭には _ を付けなくてよいと思います。
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.
ありがとうございます 🙇
以下、自分で調べたメモです。
- あくまでクラスのprivate/protectedな関数に
_
をつける。 - “Internal” means internal to a module, or protected or private within a class.
https://google.github.io/styleguide/pyguide.html#3162-naming-conventions
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://docs.python.org/3/tutorial/classes.html#private-variables
“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail and subject to change without notice.
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://peps.python.org/pep-0008/#descriptive-naming-styles
_single_leading_underscore: weak “internal use” indicator. E.g. from M import * does not import objects whose names start with an underscore.
while l1 is not None or l2 is not None or carry > 0: | ||
sum_value = get_value(l1) + get_value(l2) + carry | ||
carry = sum_value // 10 | ||
sum_value %= 10 |
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.
自分はこれも別の変数にする派です。digit = sum_value % 10
的な感じです
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://zenn.dev/miya_tech/articles/8e6781c24e85ba
https://leetcode.com/problems/add-two-numbers/description/