Skip to content

Commit 74bc839

Browse files
committed
Fix HackerRank: Filling Jars review findings - Part 2
1 parent 41c2850 commit 74bc839

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

  • Mathematics/Fundamentals/HackerRank/Claude/Easy/Filling Jars

Mathematics/Fundamentals/HackerRank/Claude/Easy/Filling Jars/Filling_Jars.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,27 @@ def solve_production(n: int, operations: List[List[int]]) -> int:
220220
ValueError: n が正でない、または操作が制約違反の場合
221221
TypeError: 引数の型が不正な場合
222222
"""
223-
if not isinstance(n, int) or n <= 0:
223+
if not isinstance(n, int):
224+
raise TypeError(f"n は整数である必要があります: {type(n)}")
225+
if n <= 0:
224226
raise ValueError(f"n は正の整数である必要があります: {n}")
225-
if not operations:
227+
228+
if not isinstance(operations, (list, tuple)):
229+
raise TypeError(f"operations はリストまたはタプルである必要があります: {type(operations)}")
230+
if len(operations) == 0:
226231
return 0
227232

228233
total: int = 0
229234
for op in operations:
235+
if not isinstance(op, (list, tuple)):
236+
raise TypeError(f"各操作はリストまたはタプルである必要があります: {type(op)}")
230237
if len(op) != 3:
231-
raise ValueError(f"操作は 3 要素のリストである必要があります: {op}")
238+
raise ValueError(f"操作は 3 要素である必要があります: {op}")
239+
232240
a, b, v = op
241+
if not (isinstance(a, int) and isinstance(b, int) and isinstance(v, int)):
242+
raise TypeError(f"操作の要素 a, b, v はすべて整数である必要があります: {a}, {b}, {v}")
243+
233244
if not (1 <= a <= b <= n):
234245
raise ValueError(f"無効なインデックス範囲 [{a}, {b}](n={n}")
235246
if v < 0:
@@ -243,19 +254,17 @@ def solve_production(n: int, operations: List[List[int]]) -> int:
243254
# HackerRank エントリポイント
244255
# ──────────────────────────────────────────────
245256
if __name__ == '__main__':
246-
fptr = open(os.environ['OUTPUT_PATH'], 'w')
247-
248-
first_multiple_input = input().rstrip().split()
249-
n = int(first_multiple_input[0])
250-
m = int(first_multiple_input[1])
257+
with open(os.environ['OUTPUT_PATH'], 'w') as fptr:
258+
first_multiple_input = input().rstrip().split()
259+
n = int(first_multiple_input[0])
260+
m = int(first_multiple_input[1])
251261

252-
operations: List[List[int]] = []
253-
for _ in range(m):
254-
operations.append(list(map(int, input().rstrip().split())))
262+
operations: List[List[int]] = []
263+
for _ in range(m):
264+
operations.append(list(map(int, input().rstrip().split())))
255265

256-
result = solve(n, operations)
257-
fptr.write(str(result) + '\n')
258-
fptr.close()
266+
result = solve(n, operations)
267+
fptr.write(str(result) + '\n')
259268
```
260269

261270
---

0 commit comments

Comments
 (0)