Skip to content

2. Add Two Numbers#4

Open
su33331 wants to merge 2 commits intomainfrom
su_24_2_2
Open

2. Add Two Numbers#4
su33331 wants to merge 2 commits intomainfrom
su_24_2_2

Conversation

@su33331
Copy link
Copy Markdown
Owner

@su33331 su33331 commented Jun 1, 2024

24_2.py Outdated
#3rd:4min
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummyHead = ListNode(0)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pythonで用いる一般的な命令規則で_を使うというのがあるので、dummy_headの方が良い気がします。(LeetCodeの仕様で決まる名前は別として)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tail.next = next_node
tail = tail.next

l1 = l1.next if l1 else None
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空の場合はl1.next = Noneになっていると思うので、l1 = l1.nextで良さそうです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あれ、それは None.next アクセスした途端に例外では?

24_2.py Outdated
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None

l3 = dummyHead.next
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好みかもしれませんが、return dummyHead.nextの方が分りやすいかも?l3だとl1,l2と関連がありそうな気がしてしまいます。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dummy を使わない書き方もあるので見ておいてください。

https://discord.com/channels/1084280443945353267/1226508154833993788/1246022270984392724

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにそのまま返す内容と変わらないのに変数にすることもないと思うので、そのままreturnしようと思います。l1やl2から生成されるl3というニュアンスで書いていましたが、他の方からも分かりにくいという指摘があるので、変数名を考える場合はもう少し意味のある名前にしようと思います。ありがとうございます。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dummyの解法を見てからそれ以外の解法が頭になかったように思います。シンプルにifの場合分けでdummyを使わない書き方もできますね。ありがとうございます。

24_2.py Outdated
#3rd:4min
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummyHead = ListNode(0)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valは使わない、かつ、デフォルトと同じなので、ListNode()でいいと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仰るように、わざわざ0と明示する必要もないですね。修正させていただきます。

24_2.py Outdated
tail = dummyHead
carry = 0

while l1 != None or l2 != None or carry != 0:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isと==の違いはご存知ですか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isは〇〇かどうか、 ==は評価、というイメージでなんとなく使っておりました。
調べてみると、isは同一objか判定しており、==は同じ値かを判別している。ということで、
特に、Noneオブジェクトとの比較の際にはisが推奨されているということは知りませんでした。

carry = 0

while l1 != None or l2 != None or carry != 0:
l1_val = l1.val if l1 else 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if l1l1 != Noneの二つの書き方が混在しているので、どちらかに統一した方がいいです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

今まで意識せず書いておりました。修正しようと思います。

24_2.py Outdated
def add_two_num_helper(l1, l2, carry, l3):
if l1 is None and l2 is None and carry == 0:
return None
else:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このelseを除いて、インデントを一つ減らした方がいいでしょう。

24_2.py Outdated
l3.next = add_two_num_helper(l1, l2, carry, l3)
return l3
l3 = add_two_num_helper(l1, l2, carry, l3)
result = dummyHead.next
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

torotokiさんのコメントにもありましたが、わざわざ新しい変数を作る必要はないかと思います。上のnext_valも同様です。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仰るように一度しか出現せず、そのような変数は修正しようと思います。ありがとうございます。

24_2.py Outdated
next_node = ListNode(next_val)
l3.next = next_node
l3 = l3.next
l1 = l1.next if l1 else None
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if l1を2回確認しているので、上とまとめても良さそうです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上記でif l1確認をしているものの、

  • l1 がNone かつ l2がNone を確認
  • l1やl2がNoneの場合に0を変数に入れている
    確認をしているのみなので、l1 がNone もしくは l2がNone のケースで、l1 = l1.next if l1 else Nonel1 = l1.nextとするとNone.nextとなるケースが発生して、エラーが出てしまうのではないかなと思いました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if l1:
    total += l1.val
    l1 = l1.next

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントいただいた意味を理解できておりませんでした。
確かに再度コメントいただいたようにも書けますね。補足をありがとうございます。

24_2.py Outdated
l3 = l3.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
l3.next = add_two_num_helper(l1, l2, carry, l3)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

末尾再帰でも書けますか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

末尾再帰となるように修正したコードを後ほどcommitしようと思います。これまで末尾再帰を聞いたことしかなく意識したことがなかったので、違う点などあればご指摘いただけると嬉しいです。

24_2.py Outdated
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummyHead = ListNode(0)
l3 = dummyHead
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l3に意味のある名前をつけた方がいいかと思います。特にadd_two_num_helper内では理解しにくくなります。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。変数名をつける際に、同じようなものくらいのニュアンスしかなかったと思いました。変数名を考えようと思います。

24_2.py Outdated
#再帰を使う方法
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummyHead = ListNode(0)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この方法だとdummyHeadは必要ない気がします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仰るようにdummyHeadが無くても問題ないですね。そのように修正してみます。

Copy link
Copy Markdown
Owner Author

@su33331 su33331 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントいただいた方、ありがとうございます。
後ほど修正版をcommitさせていただきます。

24_2.py Outdated
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None

l3 = dummyHead.next
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにそのまま返す内容と変わらないのに変数にすることもないと思うので、そのままreturnしようと思います。l1やl2から生成されるl3というニュアンスで書いていましたが、他の方からも分かりにくいという指摘があるので、変数名を考える場合はもう少し意味のある名前にしようと思います。ありがとうございます。

24_2.py Outdated
#3rd:4min
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummyHead = ListNode(0)
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仰るように、わざわざ0と明示する必要もないですね。修正させていただきます。

24_2.py Outdated
#再帰を使う方法
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummyHead = ListNode(0)
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仰るようにdummyHeadが無くても問題ないですね。そのように修正してみます。

24_2.py Outdated
next_node = ListNode(next_val)
l3.next = next_node
l3 = l3.next
l1 = l1.next if l1 else None
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

上記でif l1確認をしているものの、

  • l1 がNone かつ l2がNone を確認
  • l1やl2がNoneの場合に0を変数に入れている
    確認をしているのみなので、l1 がNone もしくは l2がNone のケースで、l1 = l1.next if l1 else Nonel1 = l1.nextとするとNone.nextとなるケースが発生して、エラーが出てしまうのではないかなと思いました。

24_2.py Outdated
l3 = l3.next
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None
l3.next = add_two_num_helper(l1, l2, carry, l3)
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

末尾再帰となるように修正したコードを後ほどcommitしようと思います。これまで末尾再帰を聞いたことしかなく意識したことがなかったので、違う点などあればご指摘いただけると嬉しいです。

24_2.py Outdated
l3.next = add_two_num_helper(l1, l2, carry, l3)
return l3
l3 = add_two_num_helper(l1, l2, carry, l3)
result = dummyHead.next
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仰るように一度しか出現せず、そのような変数は修正しようと思います。ありがとうございます。

24_2.py Outdated
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
dummyHead = ListNode(0)
l3 = dummyHead
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。変数名をつける際に、同じようなものくらいのニュアンスしかなかったと思いました。変数名を考えようと思います。

carry = 0

while l1 != None or l2 != None or carry != 0:
l1_val = l1.val if l1 else 0
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

今まで意識せず書いておりました。修正しようと思います。

24_2.py Outdated
tail = dummyHead
carry = 0

while l1 != None or l2 != None or carry != 0:
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isは〇〇かどうか、 ==は評価、というイメージでなんとなく使っておりました。
調べてみると、isは同一objか判定しており、==は同じ値かを判別している。ということで、
特に、Noneオブジェクトとの比較の際にはisが推奨されているということは知りませんでした。

24_2.py Outdated
l1 = l1.next if l1 else None
l2 = l2.next if l2 else None

l3 = dummyHead.next
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dummyの解法を見てからそれ以外の解法が頭になかったように思います。シンプルにifの場合分けでdummyを使わない書き方もできますね。ありがとうございます。

@liquo-rice
Copy link
Copy Markdown

書いたコードを上書き修正せず、新しく付け足すことが推奨されています。https://discord.com/channels/1084280443945353267/1235066663150420038/1237295008541966336

@rihib rihib mentioned this pull request Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants