Skip to content

Create add_two_numbers.md#2

Open
yakataN wants to merge 1 commit into
mainfrom
add_two_numbers
Open

Create add_two_numbers.md#2
yakataN wants to merge 1 commit into
mainfrom
add_two_numbers

Conversation

@yakataN
Copy link
Copy Markdown
Owner

@yakataN yakataN commented May 13, 2025

- [x] 2025/05/13

- ListNodeのポインタで渡されたリストをreverse→joinして、下の位から並べたListNodeにすることを要求されている。
- ListNode→int→ListNodeが要求。ListNode→intはいくつかあり、
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あー、おそらくこの問題のみたいところは LinkedList を操作できるかなので、コメント集を見ておいてください。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.lxzt19oefrb8

あと、読むほうが大事なので、完走した人を何人かみて、気に入った人を見繕っておくといいと思います。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.14wn5hdkkebn

```python
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
# 23:28 start
ary1 = []
Copy link
Copy Markdown

@potrue potrue May 13, 2025

Choose a reason for hiding this comment

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

aryと略して書いてある場合とarrayと書いてある場合の認知負荷を比較したときに、単にarrayと書いてしまう方がよい気がします。
個人的には、略語は読むときの認知負荷を上げてしまうような気がするので、よほどそれによって文字数が節約できる場合でない限り使わない方が最近は好みです。(ただ、一般的に使われている略語であればもうちょっと使ってもいいのかなという感じはしています。)

ary1 = []
while l1 is not None:
ary1.append(l1.val)
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.

ary1にあたる変数をstringとして初期化しておいて、最初から文字にして足してしまう(num1_string = str(val) + num1_stringのような感じで)のもありだと思います。
あとは、次のような感じで数値にして足してしまってもよいかもしれません。

num1 = 0
power = 0
while l1 is not none:
    num1 += 10 ** power
    l1 = l1.next


反省
未知のStruct, Classの扱い方がわかるまでが遅い。(連結リストは知っているが、実装となると手が止まる)
step2と3で方針同じだがコードが異なっているのはいいのか。
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

Choose a reason for hiding this comment

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

短ければそうかもしれませんが、私が SWEJP 75 で実装量が一番多いやつでやったときは、収束するまで10回以上かかりました。
https://discord.com/channels/1084280443945353267/1358347241164902411/1358349459943325776

繰り返し書くときに意味と表現をつなげて欲しいんですね。方針という圧縮状態から自然に展開されてくるイメージで、引っかかる箇所があったら調整して自然なフォームになるようにします。

l2 = l2.next

reverse_num1 = int(''.join(list(reversed(list(map(str, ary1))))))
reverse_num2 = int(''.join(list(reversed(list(map(str, ary2))))))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

同じ処理を2回書いているので関数にする書き方もあるかもしれません。
外側の list は不要でしょうか。iterator のままでも可のようです。
https://docs.python.org/3.13/library/functions.html#reversed
https://docs.python.org/3.13/library/stdtypes.html#str.join


sum_num = reverse_num1 + reverse_num2

ans = 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.

個人的に、ans よりは result の方が自然と思いました。何の変数かわかるような変数名が好ましいですが、この規模の関数なら result でもよいと思っています。

sum_num = reverse_num1 + reverse_num2

ans = None
for val in map(int, list(str(sum_num))):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここの list は不要ですね。

ary2.append(l2.val)
l2 = l2.next

reverse_num1 = int(''.join(list(reversed(list(map(str, ary1))))))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

一行に多くの要素が含まれており、認知負荷が高いように関しました。関数に抽出したうえで、意味のある要素に分解して書いたほうが読みやすいと思います。

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