Skip to content

Update notebook metadata and improve documentation for Sleep, Cache, and Memoize#251

Merged
myoshi2891 merged 2 commits into
mainfrom
dev-from-macmini
Feb 1, 2026
Merged

Update notebook metadata and improve documentation for Sleep, Cache, and Memoize#251
myoshi2891 merged 2 commits into
mainfrom
dev-from-macmini

Conversation

@myoshi2891

Copy link
Copy Markdown
Owner

No description provided.

## 修正概要
3つのLeetCode問題(2621 Sleep、2622 Cache With Time Limit、2623 Memoize)に対するレビュー指摘事項を修正

## 2622. Cache With Time Limit
- ipynb: メタデータをtypescriptに変更、cleanup()を1パス削除に最適化
- README: メモリリーク記述の正確化、ベンチマーク値に参考値明記
- README_react.html: React本番ビルド使用、自動再生間隔を定数化

## 2623. Memoize
- ipynb: メタデータ修正、キー衝突対策、二重検索回避
- README: 文法・用語修正、可変長関数の注意書き追加

## 2621. Sleep
- ipynb: メタデータ修正、バリデーション順序改善
- README: 非推奨API修正、マイクロ最適化主張の見直し
## 修正内容

### Space複雑度の正確化
- cleanup()はインプレース削除のため Space: O(n) → O(1) に修正
  - CacheWithTimeLimit_TS.ipynb
  - README.md (2622)

### Pythonバージョン情報の修正
- asyncio.get_running_loop() は Python 3.7+ で利用可能
- コメントを "3.10+推奨" → "3.7+で推奨" に修正
  - README.md (2621)

### 可読性優先の実装変更
- cache.has() + cache.get()! パターンに戻す (2箇所)
- パフォーマンスより明示性を優先
  - Memoize_TS.ipynb
@coderabbitai

coderabbitai Bot commented Feb 1, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

ウォークスルー

複数のノートブックのメタデータをPythonからTypeScriptに更新し、Sleep実装のasyncio APIを最新化、キャッシュ有効期限戦略(怠惰削除とアクティブ削除)の説明を明確化、複数ファイルにおける用語と制約を整理しました。

変更内容

コホート / ファイル(s) 変更概要
Sleep実装
JavaScript/2621. Sleep/Claude Code Sonnet 4.5/Sleep_TS.ipynb, JavaScript/2621. Sleep/Claude Code Sonnet 4.5/README.md
ノートブックメタデータをTypeScriptに更新、asyncio.get_event_loop()asyncio.get_running_loop()に置き換え(Python 3.7+推奨)、計算ロジックのコメントを可読性重視に変更。
キャッシュタイムリミット実装
JavaScript/2622. Cache With Time Limit/Claude Code Sonnet 4.5/CacheWithTimeLimit_TS.ipynb, JavaScript/2622. Cache With Time Limit/Claude Code Sonnet 4.5/README.md, JavaScript/2622. Cache With Time Limit/Claude Code Sonnet 4.5/README_react.html
ノートブックメタデータをTypeScriptに更新、怠惰削除の挙動説明を拡充、キーの二回パス削除をシングルパスに最適化(O(k)空間→O(1)空間)、CDNをプロダクション版に、オートプレイ間隔を定数化。
Memoize実装
JavaScript/2623. Memoize/Claude Code Sonnet 4.5/Memoize_TS.ipynb, JavaScript/2623. Memoize/Claude Code Sonnet 4.5/README.md
ノートブックメタデータをTypeScriptに更新、キー設計の固定長アリティ明確化、キャッシュ動作に関する用語統一、技術用語の表記ゆれ修正(「ハネス」→「ハーネス」など)。

推定コードレビュー労力

🎯 3 (中程度) | ⏱️ ~25分

関連PRの可能性

  • PR #243: Sleep実装のREADMEとノートブックへの同一ファイルへの小規模なコード編集(asyncio APIリプレイスとメタデータ調整)が関連しています。
  • PR #244: CacheWithTimeLimit実装/READMEへの変更で、二回パス削除アプローチから単一パス/有効期限タイムスタンプ方式への内部変更が同一の最適化を実現しており関連しています。

ウサギからの詩

🐰 ノート本のTypeScriptへ衣替え
怠惰削除の戦略、より明快に
キャッシュ最適化、O(k)がO(1)へ
用語の誤字も整い、コード本来の輝きよ
小さな修正が、全体の美しさを磨く

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description check ⚠️ Warning プルリクエストの説明が提供されていないため、変更内容の意図や背景を理解することができません。 プルリクエストの説明を追加してください。複数ファイルの変更(ノートブックメタデータの更新、ドキュメントの改善など)の目的と背景を明記してください。
✅ Passed checks (2 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed プルリクエストのタイトルは、Sleep、Cache、Memoizeの複数の問題にわたるノートブックメタデータの更新とドキュメント改善という全体的な変更内容を適切に要約している。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch dev-from-macmini

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@JavaScript/2623`. Memoize/Claude Code Sonnet 4.5/Memoize_TS.ipynb:
- Around line 130-142: The memoize implementation incorrectly compresses keys
assuming 1–2 numeric args (in function memoize, cache Map and memoized), causing
collisions for 3+ args; change the key-generation to handle variable-length
args: keep the numeric compression for 1 or 2 args if you want that
optimization, but when args.length > 2 (or when any arg is non-number) fall back
to a safe generic key (e.g., stringified args via JSON.stringify or args.join
with a separator) and update the cache Map type accordingly (e.g., Map<string |
number, number> or unify to Map<string, number>) so all keys map correctly
without collisions. Ensure memoized uses that unified key strategy for all
calls.

In `@JavaScript/2623`. Memoize/Claude Code Sonnet 4.5/README.md:
- Around line 38-41: The README's fixed-arity memoize example lacks an
argument-count guard, allowing mixed arity calls to produce NaN/compressed-key
collisions; update the sample for the memoize implementation to explicitly
validate the number of arguments (e.g., check args.length or the expected arity
at the start of the memoized wrapper) and either throw a clear error or handle
mismatched arity before computing keys (particularly before using the compressed
key formula `a * 100001 + b`), so callers cannot produce NaN keys or mix 1- and
2-argument invocations.

Comment on lines +130 to +142
"function memoize(fn: Fn): Fn {\n",
" // sum: 引数2つ(a, b) → a * 100001 + b で一意な整数キーに圧縮\n",
" // fib/factorial: 引数1つ(n) → nそのもの\n",
" // 両方対応するため、ネスト Map を使用しない。\n",
" // 引数数で分岐し、数値キーのみで Map を構築する。\n",
" const cache = new Map<number, number>();\n",
" let callCount = 0;\n",
"\n",
" const memoized: Fn = function (...args: number[]): number {\n",
" // 引数が1つなら n そのもの、2つなら圧縮キー\n",
" const key = args.length === 1\n",
" ? args[0]\n",
" : args[0] * 100001 + args[1];\n",

@coderabbitai coderabbitai Bot Feb 1, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

可変長引数に非対応なキー圧縮は誤解を招きます

memoize のシグネチャは可変長引数ですが、この最適化は 1~2 引数前提で args[2] 以降を無視するため、3 つ以上の引数で衝突し誤ったキャッシュヒットになります。最適化を残すなら「1〜2 引数限定」を明記するか、超過時に汎用キーへフォールバックするなどのガードが必要です。

🛠️ 修正案(可変長引数は汎用キーにフォールバック)
-    const cache = new Map<number, number>();
+    const cache = new Map<string, number>();
@@
-    const key = args.length === 1
-        ? args[0]
-        : args[0] * 100001 + args[1];
+    const key =
+        args.length === 1
+            ? `n:${args[0]}`
+            : args.length === 2
+                ? `s:${args[0] * 100001 + args[1]}`
+                : `v:${args.join(",")}`;
🤖 Prompt for AI Agents
In `@JavaScript/2623`. Memoize/Claude Code Sonnet 4.5/Memoize_TS.ipynb around
lines 130 - 142, The memoize implementation incorrectly compresses keys assuming
1–2 numeric args (in function memoize, cache Map and memoized), causing
collisions for 3+ args; change the key-generation to handle variable-length
args: keep the numeric compression for 1 or 2 args if you want that
optimization, but when args.length > 2 (or when any arg is non-number) fall back
to a safe generic key (e.g., stringified args via JSON.stringify or args.join
with a separator) and update the cache Map type accordingly (e.g., Map<string |
number, number> or unify to Map<string, number>) so all keys map correctly
without collisions. Ensure memoized uses that unified key strategy for all
calls.

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.

make this issue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No description provided.

Comment on lines +38 to +41
- **キー設計(固定アリティ前提)**:
- 引数1つの場合: キー = `n` そのもの
- 引数2つの場合: キー = `a * 100001 + b`(圧縮キー)
- ※ 同一 memoize で可変長(1/2引数混在)を許すとキー衡突が起こり得るため不可

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick | 🔵 Trivial

固定アリティ前提なら、実装サンプルにも引数数チェックを明記して整合性を上げると良いです。
TL;DR で固定アリティを強調している一方、コード例にはガードがなく、誤った呼び出しで NaN キー化する余地があります。README 内のサンプルに軽いチェックを入れると意図が明確になります。

🔧 追記例(README のコードブロック内)
-        const key = args.length === 1 ? args[0] : args[0] * 100001 + args[1];
+        if (args.length !== 1 && args.length !== 2) {
+            throw new Error("memoize expects 1 or 2 numeric arguments.");
+        }
+        const key = args.length === 1 ? args[0] : args[0] * 100001 + args[1];
🤖 Prompt for AI Agents
In `@JavaScript/2623`. Memoize/Claude Code Sonnet 4.5/README.md around lines 38 -
41, The README's fixed-arity memoize example lacks an argument-count guard,
allowing mixed arity calls to produce NaN/compressed-key collisions; update the
sample for the memoize implementation to explicitly validate the number of
arguments (e.g., check args.length or the expected arity at the start of the
memoized wrapper) and either throw a clear error or handle mismatched arity
before computing keys (particularly before using the compressed key formula `a *
100001 + b`), so callers cannot produce NaN keys or mix 1- and 2-argument
invocations.

@myoshi2891 myoshi2891 changed the title Dev from macmini Update notebook metadata and improve documentation for Sleep, Cache, and Memoize Feb 1, 2026
@myoshi2891 myoshi2891 merged commit 34bc30b into main Feb 1, 2026
1 check passed
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.

1 participant