Skip to content

Add Restaurant problem solution and SRI calculation script#264

Merged
myoshi2891 merged 1 commit into
mainfrom
dev-from-macmini
Feb 11, 2026
Merged

Add Restaurant problem solution and SRI calculation script#264
myoshi2891 merged 1 commit into
mainfrom
dev-from-macmini

Conversation

@myoshi2891

@myoshi2891 myoshi2891 commented Feb 11, 2026

Copy link
Copy Markdown
Owner

Mathematics/Fundamentals/HackerRank/Claude/Easy/Restaurant/Restaurant.md
: 問題解説
Mathematics/Fundamentals/HackerRank/Claude/Easy/Restaurant/Restaurant.ipynb
: 実装コード
calc_sri_fix.sh: SRIハッシュ計算スクリプト

@coderabbitai

coderabbitai Bot commented Feb 11, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Summary by CodeRabbit

リリースノート

  • 新機能

    • 矩形を最大サイズの正方形に分割する問題の数学的ソリューションを追加しました
    • CDN URLのサブリソース整合性(SRI)値を自動生成するユーティリティスクリプトを追加しました
  • ドキュメント

    • Restaurant問題に関する包括的なドキュメント(数学的アプローチ、実装例、エッジケース分析を含む)を追加しました

Walkthrough

このPRは、HackerRankの「Restaurant」問題の解決策をPythonノートブックと詳細なドキュメントで新規追加し、さらにPrism CDNのSRI値を計算するBashスクリプトを追加します。矩形を最大サイズの正方形に分割する数学的アプローチをGCDを使用して実装しています。

Changes

Cohort / File(s) Summary
Restaurant 問題解決
Mathematics/Fundamentals/HackerRank/Claude/Easy/Restaurant/Restaurant.ipynb, Mathematics/Fundamentals/HackerRank/Claude/Easy/Restaurant/Restaurant.md
HackerRankの矩形分割問題のPythonソリューションを実装。restaurant(l, b) 関数でGCD計算により最大正方形サイズを決定し、個数を算出。ノートブックとドキュメントで数学的証明、計算量解析、エッジケース、最適化手法を網羅。
SRI計算スクリプト
calc_sri_fix.sh
Prism CDN URLのSubresource Integrity(SRI)値をSHA-384ハッシュで生成するBashスクリプトを追加。calculate_sri() 関数で2つのCDN URLを処理。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 四角い庭にウサギは考えた
GCDで最大の正方形を見つけ
矩形を分割し、数えて数えて
SRI値も秋風にのせて
数学の道、ホップスキップジャンプ! 🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes both main additions: the Restaurant problem solution and the SRI calculation script, covering the primary changes in the changeset.
Description check ✅ Passed The description relates to the changeset by listing the three new files added, though it uses Japanese language labels without detailed context about what each component does.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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: 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.

Comment thread calc_sri_fix.sh
@@ -0,0 +1,10 @@
#!/bin/bash

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.

🛠️ 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.

Suggested change
#!/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 ハッシュ生成の箇所での空レスポンスチェック)。

Comment thread calc_sri_fix.sh
Comment on lines +3 to +7
calculate_sri() {
url="$1"
hash=$(curl -sL "$url" | openssl dgst -sha384 -binary | openssl base64 -A)
echo "$url sha384-$hash"
}

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 | 🟡 Minor

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.

Suggested change
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.

Comment on lines +215 to +216
if TYPE_CHECKING:
pass

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

未使用の 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.

Comment on lines +300 to +308
```python
# ❌ 非効率: 中間変数の無駄な生成
area = l * b
square_area = g * g
result = area // square_area

# ✅ 最適: 1行で計算
result = (l * b) // (g * g)
```

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 | 🟡 Minor

中間変数の使用を「非効率」とする記述は誤解を招きます。

中間変数への代入は可読性を向上させる場合があり、パフォーマンス差はこの規模の計算ではほぼゼロです。「非効率」ではなく「より簡潔に書ける」程度の表現が適切です。

🤖 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.

@myoshi2891 myoshi2891 merged commit 4c015b2 into main Feb 11, 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