Skip to content

141. linked list cycle#1

Open
Rinta-Rinta wants to merge 5 commits intomainfrom
141.-Linked-List-Cycle
Open

141. linked list cycle#1
Rinta-Rinta wants to merge 5 commits intomainfrom
141.-Linked-List-Cycle

Conversation

@Rinta-Rinta
Copy link
Copy Markdown
Owner

@@ -0,0 +1,184 @@
step1
訪れたノードのアドレスを記録しておき、同じアドレスが出てきたらサイクルがあると判断できると考えた。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[fyi]
「正確にアドレスそのものなの?」と思ってドキュメントを見てみました。
https://docs.python.org/3.13/library/functions.html#id

Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

CPython implementation detail: This is the address of the object in memory.

CPython ではそうで、他の処理系では当てはまらない場合もあるかもしれません。

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^13<15000<2^14だから2^14個スロットを用意する
16バイト×2^14スロット=2^18バイト必要らしい
また、わざわざアドレスを調べなくても、変数名でインスタンスを参照できるので、id()はやめることにした。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[fyi]
結局、内部では id() を使っていそうですね。

https://docs.python.org/3.13/reference/expressions.html#membership-test-operations

For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression x in y is equivalent to any(x is e or x == e for e in y).

https://docs.python.org/3.13/reference/expressions.html#is

The operators is and is not test for an object’s identity: x is y is true if and only if x and y are the same object. An Object’s identity is determined using the id() function.

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.

勉強になります。確かに、変数名で参照するときに、内部でどう動いているか気になるのが自然ですね。

Comment on lines +14 to +24
visited = set()

curr = head

while curr is not None:
if curr in visited:
return True

visited.add(curr)

curr = curr.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 の後は空けない
    • else のつもり
Suggested change
visited = set()
curr = head
while curr is not None:
if curr in visited:
return True
visited.add(curr)
curr = curr.next
visited = set()
curr = head
while curr is not None:
if curr in visited:
return True
visited.add(curr)
curr = curr.next

2^13<15000<2^14だから2^14個スロットを用意する
16バイト×2^14スロット=2^18バイト必要らしい
また、わざわざアドレスを調べなくても、変数名でインスタンスを参照できるので、id()はやめることにした。
現在見ているものを指すときはcurrが一般的な様なのでcheckではなく、currを使うようにした。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

おっしゃるように、curr のように単語を略記する場合は一般的な記法かどうかに注意することが重要そうです。
その上で、私が C++ で書いた際には curr ではなく current とし、さらにどのようなオブジェクトなのかがわかるように node_current としました。自分はPythonの一般的な記法に明るくないので参考程度に考えて欲しいですが、他のPythonで書いている方のコードも見てみると良いかもしれません。

自分はなんとなくリストで書いた。
他の人がsetで書いている理由がわからなかったので、geminiに聞いてみた。
リストは要素の探索の際に線形探索を行うので、時間計算量がO(n^2)かかり、setではハッシュを使うので時間計算量はO(n)になると分かった。
空間計算量はどちらもO(n)だが、setでは衝突を防ぐためにリストより大きめにメモリを確保するらしい。
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 False
```

解説の動画を見てみると知らない方法を使っていたのでそれも真似してみた。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

念のため確認させてください。なぜこの方法で上手くいくか説明できますか?

正解のコードを真似て、 Accept されることはできると思います。一方、現場では、なぜそれで正しく動くのかが分かったうえで書かないとまずいです。理由は、意図しない動作が起きた場合に修正が難しくなるためです。

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.

コメントありがとうございます。このコードは仕組みと上手くいく理由、時間計算量、空間計算量を理解したうえで書いています。
ご心配いただいた点は重要だと思うので、今後も理由まで説明できる形で実装することを意識します。

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 False
```

解説の動画を見てみると知らない方法を使っていたのでそれも真似してみた。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらのコメントをご参照ください。
komdoroid/arai60#9 (comment)

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.

6 participants