Conversation
oda
reviewed
Jan 27, 2025
Comment on lines
+30
to
+31
| self.dummy_head = ListNode() | ||
| self.dummy_tail = ListNode() |
oda
reviewed
Jan 27, 2025
Comment on lines
+91
to
+95
| self.access_history.append(key) | ||
| self.cache[key] = value | ||
| else: | ||
| self.access_history.append(key) | ||
| self.cache[key] = value |
Owner
Author
There was a problem hiding this comment.
L105-L127辺りにも書いたんですが、こういうのって基本はまとめるもんなんでしょうか?
まとめてしまうと、視線が上下に飛ぶ感覚があるんですが、コードの読み方が悪いんでしょうか?
if Condition1:
Statement1
elif Condition2:
Statement2
Statement3
みたいな時って、自分は「if, elifだからStatement3が共通処理だな」「Statement1見た後にStatement3みよう」「その次にStatement2」みたいな感じなんですが、先にStatement3理解してから前処理見ようっていう感覚なんでしょうか?
There was a problem hiding this comment.
私の読み方は、
- 前処理として、すでに加えてあったらそれを消す
- 前処理として、溢れるようだったら一番古いのを消す
- 追加
なので、あまり違和感はないです。
経験的に同じものを繰り返し書くと事故を起こしやすいので、そちらをより避けているかもしれません。
There was a problem hiding this comment.
あと、「消す」メソッドがあってもいいかもしれません。
(データ構造を整合性を保って触る最小単位が何になっているかを考えると、そういう関数があってもいいでしょう。そういうものだけを使って動かしていると、何らかの意味で各関数が整合性を保って全体が動いていることが分かりやすくなります。)
Owner
Author
There was a problem hiding this comment.
ありがとうございます。
すでに加えてある場合と、溢れる場合とで、似たようなことをしているのに書き方が異なることも、自分の中のわかりにくさの原因な気がしてきました。
oda
reviewed
Jan 27, 2025
| node.value = value | ||
| elif len(self.key_to_node) == self.capacity: | ||
| oldest_node = self._popleft() | ||
| del self.key_to_node[oldest_node.key] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/lru-cache/description/