Skip to content

Add two numbers#7

Open
ask-1407 wants to merge 3 commits intomainfrom
add_two_numbers
Open

Add two numbers#7
ask-1407 wants to merge 3 commits intomainfrom
add_two_numbers

Conversation

@ask-1407
Copy link
Copy Markdown
Owner

@ask-1407 ask-1407 commented Aug 3, 2024

while l1 or l2:
l1_val = l1.val if l1 else 0
l2_val = l2.val if l2 else 0
total = carry + l1_val + l2_val
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

total = l1_val + l2_val + carryの順の方が自然だと思いました。
また、l1_val, l2_valよりv1, v2でもいいのかなと思いました。


思考ログ
- Step1の改修できる箇所をさがす
- x、y⇒l1_val、l2_valとか? 関数化も視野かなと思ったが、これくらいならむしろしなくていいんじゃないかという気もする
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だと3項演算子でパッと書けるので僕も関数化しなくていいと思いました

# Step3
かかった時間:5min

step2を何回か書き直してSubした
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

最終的にiterativeな解法と再帰のどちらを選びましたか?

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.

iterativeの解法です!

Copy link
Copy Markdown

@kazukiii kazukiii left a comment

Choose a reason for hiding this comment

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

良いと思います!

carry = 0

while l1 or l2:
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.

0を足すことはつまり何もしないことと同じなので、下のif文とまとめて分岐の数を減らすことも出来そうです。

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がなくてもよいことに気づけました。ありがとうございます。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここの inline if と下の同じ条件のところをまとめて、以下のようなものを考えてました。元のコードで十分読みやすいので、単なる選択肢の一つとして捉えていただけますと。

class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        dummy_head = ListNode()
        current = dummy_head
        carry = 0
        while l1 or l2 or carry:
            total = carry
            if l1:
                total += l1.val
                l1 = l1.next
            if l2:
                total += l2.val
                l2 = l2.next

            carry = total // 10
            current.next = ListNode(total % 10) 
            current = current.next

        return dummy_head.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.

丁寧にありがとうございます!

```python
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.

snake caseを使うことをおすすめします。
https://peps.python.org/pep-0008/#function-and-variable-names

@rihib rihib mentioned this pull request Aug 6, 2024
思考ログ
- Step1の改修できる箇所をさがす
- x、y⇒l1_val、l2_valとか? 関数化も視野かなと思ったが、これくらいならむしろしなくていいんじゃないかという気もする
- sumは予約語になってるケースもあるので名前変える
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

予約語 (reserved words) という言葉は、言語によって意味が変わることもあるのですが、Python においては、予約語は keywords のことで、def, elif, pass みたいにパースの仕方から変わるものです。
https://docs.python.org/3/reference/lexical_analysis.html#keywords
sum は組み込み関数 (Built-in Functions) ですね。
https://docs.python.org/3/library/functions.html#sum

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.

雰囲気一緒だなと思って誤って覚えてました。ご指摘ありがとうございます。

@Mike0121
Copy link
Copy Markdown

step4まで見ましたが、既に指摘されている部分を除き全体的に良いと思いました。

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.

5 participants