Add Restaurant problem solution and SRI calculation script#264
Conversation
📝 WalkthroughSummary by CodeRabbitリリースノート
WalkthroughこのPRは、HackerRankの「Restaurant」問題の解決策をPythonノートブックと詳細なドキュメントで新規追加し、さらにPrism CDNのSRI値を計算するBashスクリプトを追加します。矩形を最大サイズの正方形に分割する数学的アプローチをGCDを使用して実装しています。 Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Fix all issues with AI agents
In `@calc_sri_fix.sh`:
- Around line 3-7: The calculate_sri function currently pipes curl output
directly to openssl and computes a hash even if curl failed; update
calculate_sri to invoke curl with --fail -sL (or --silent --show-error --fail
-L) and capture curl's exit code or output before hashing (e.g., read into a
variable or temp file), check if curl succeeded, and if not print a clear error
mentioning the URL and return a non-zero status instead of producing a hash;
ensure the variable names (url, hash) and function name calculate_sri are used
so you modify the correct code path.
- Line 1: スクリプトの先頭に set -euo pipefail を追加して失敗時に即終了するようにし、curl 呼び出しには -f(--fail)と
-S を付けて HTTP エラーで非ゼロ終了させるか、curl の戻り値をチェックして空レスポンスの場合は続行せずエラーを出すようにしてください(ファイル:
calc_sri_fix.sh、キー記号: set -euo pipefail、curl -fS、および SRI ハッシュ生成の箇所での空レスポンスチェック)。
In `@Mathematics/Fundamentals/HackerRank/Claude/Easy/Restaurant/Restaurant.md`:
- Around line 300-308: The comment labeling the use of intermediate variables
(area, square_area) as "非効率" is misleading—update the explanatory text and
examples to say that using intermediate variables can improve readability and
that the one-line form "(l * b) // (g * g)" is simply more concise; keep both
code variants but change the headings/annotations to something like "より簡潔に書ける"
for the one-line version and "可読性重視: 中間変数を使った例" for the multi-line version, and
remove or soften the "非効率" wording near area, square_area and result.
- Around line 215-216: Remove the unused TYPE_CHECKING block and any
corresponding import: locate the conditional "if TYPE_CHECKING: pass" and delete
that block, and also remove the "TYPE_CHECKING" import wherever it appears
(e.g., top-level imports) after verifying it's not used elsewhere in the file
(search for "TYPE_CHECKING" and any type-only imports referencing it); this
cleans up dead code and improves readability.
| @@ -0,0 +1,10 @@ | |||
| #!/bin/bash | |||
There was a problem hiding this comment.
🛠️ Refactor suggestion | 🟠 Major
set -euo pipefail を追加してスクリプトの堅牢性を向上させてください。
curl が失敗した場合(ネットワークエラー、404等)、空のレスポンスに対してハッシュが計算され、無効な SRI 値が出力されます。
🛡️ 修正案
#!/bin/bash
+set -euo pipefail📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| #!/bin/bash | |
| #!/bin/bash | |
| set -euo pipefail |
🤖 Prompt for AI Agents
In `@calc_sri_fix.sh` at line 1, スクリプトの先頭に set -euo pipefail
を追加して失敗時に即終了するようにし、curl 呼び出しには -f(--fail)と -S を付けて HTTP エラーで非ゼロ終了させるか、curl
の戻り値をチェックして空レスポンスの場合は続行せずエラーを出すようにしてください(ファイル: calc_sri_fix.sh、キー記号: set -euo
pipefail、curl -fS、および SRI ハッシュ生成の箇所での空レスポンスチェック)。
| calculate_sri() { | ||
| url="$1" | ||
| hash=$(curl -sL "$url" | openssl dgst -sha384 -binary | openssl base64 -A) | ||
| echo "$url sha384-$hash" | ||
| } |
There was a problem hiding this comment.
curl の失敗時にエラーハンドリングを追加してください。
curl がネットワークエラーや HTTP エラー(404 等)で失敗した場合、空または不正なコンテンツに対してハッシュが計算され、誤った SRI 値がサイレントに出力されます。--fail オプションの追加とリターンコードのチェックを推奨します。
🛡️ 修正案
calculate_sri() {
url="$1"
- hash=$(curl -sL "$url" | openssl dgst -sha384 -binary | openssl base64 -A)
+ hash=$(curl -sfL "$url" | openssl dgst -sha384 -binary | openssl base64 -A) || {
+ echo "Error: Failed to fetch $url" >&2
+ return 1
+ }
echo "$url sha384-$hash"
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| calculate_sri() { | |
| url="$1" | |
| hash=$(curl -sL "$url" | openssl dgst -sha384 -binary | openssl base64 -A) | |
| echo "$url sha384-$hash" | |
| } | |
| calculate_sri() { | |
| url="$1" | |
| hash=$(curl -sfL "$url" | openssl dgst -sha384 -binary | openssl base64 -A) || { | |
| echo "Error: Failed to fetch $url" >&2 | |
| return 1 | |
| } | |
| echo "$url sha384-$hash" | |
| } |
🤖 Prompt for AI Agents
In `@calc_sri_fix.sh` around lines 3 - 7, The calculate_sri function currently
pipes curl output directly to openssl and computes a hash even if curl failed;
update calculate_sri to invoke curl with --fail -sL (or --silent --show-error
--fail -L) and capture curl's exit code or output before hashing (e.g., read
into a variable or temp file), check if curl succeeded, and if not print a clear
error mentioning the URL and return a non-zero status instead of producing a
hash; ensure the variable names (url, hash) and function name calculate_sri are
used so you modify the correct code path.
| if TYPE_CHECKING: | ||
| pass |
There was a problem hiding this comment.
🧹 Nitpick | 🔵 Trivial
未使用の TYPE_CHECKING ブロックを削除してください。
TYPE_CHECKING をインポートしていますが、pass のみで何も使用されていません。不要なコードは可読性を下げます。
♻️ 修正案
-from typing import TYPE_CHECKING
-
-if TYPE_CHECKING:
- pass
-🤖 Prompt for AI Agents
In `@Mathematics/Fundamentals/HackerRank/Claude/Easy/Restaurant/Restaurant.md`
around lines 215 - 216, Remove the unused TYPE_CHECKING block and any
corresponding import: locate the conditional "if TYPE_CHECKING: pass" and delete
that block, and also remove the "TYPE_CHECKING" import wherever it appears
(e.g., top-level imports) after verifying it's not used elsewhere in the file
(search for "TYPE_CHECKING" and any type-only imports referencing it); this
cleans up dead code and improves readability.
| ```python | ||
| # ❌ 非効率: 中間変数の無駄な生成 | ||
| area = l * b | ||
| square_area = g * g | ||
| result = area // square_area | ||
|
|
||
| # ✅ 最適: 1行で計算 | ||
| result = (l * b) // (g * g) | ||
| ``` |
There was a problem hiding this comment.
中間変数の使用を「非効率」とする記述は誤解を招きます。
中間変数への代入は可読性を向上させる場合があり、パフォーマンス差はこの規模の計算ではほぼゼロです。「非効率」ではなく「より簡潔に書ける」程度の表現が適切です。
🤖 Prompt for AI Agents
In `@Mathematics/Fundamentals/HackerRank/Claude/Easy/Restaurant/Restaurant.md`
around lines 300 - 308, The comment labeling the use of intermediate variables
(area, square_area) as "非効率" is misleading—update the explanatory text and
examples to say that using intermediate variables can improve readability and
that the one-line form "(l * b) // (g * g)" is simply more concise; keep both
code variants but change the headings/annotations to something like "より簡潔に書ける"
for the one-line version and "可読性重視: 中間変数を使った例" for the multi-line version, and
remove or soften the "非効率" wording near area, square_area and result.
Mathematics/Fundamentals/HackerRank/Claude/Easy/Restaurant/Restaurant.md
: 問題解説
Mathematics/Fundamentals/HackerRank/Claude/Easy/Restaurant/Restaurant.ipynb
: 実装コード
calc_sri_fix.sh: SRIハッシュ計算スクリプト