Datasets:
Download code/math_code_003_solution.py from efe-T/bilingual-math-code-verification-v1: direct link, hf CLI and curl.
- Browser
- Download file 783 Bytes
-
https://huggingface.co/datasets/efe-T/bilingual-math-code-verification-v1/resolve/main/code/math_code_003_solution.py
- Command line
-
hf download hf://datasets/efe-T/bilingual-math-code-verification-v1/code/math_code_003_solution.py
-
curl -L -o math_code_003_solution.py https://huggingface.co/datasets/efe-T/bilingual-math-code-verification-v1/resolve/main/code/math_code_003_solution.py
783 Bytes
| items = [("A", 2, 3), ("B", 3, 4), ("C", 4, 5), ("D", 5, 8), ("E", 9, 10)] | |
| capacity = 10 | |
| dp = [[0] * (capacity + 1) for _ in range(len(items) + 1)] | |
| for i, (_, weight, value) in enumerate(items, 1): | |
| for c in range(capacity + 1): | |
| dp[i][c] = dp[i - 1][c] | |
| if weight <= c: | |
| dp[i][c] = max(dp[i][c], dp[i - 1][c - weight] + value) | |
| c, chosen = capacity, [] | |
| for i in range(len(items), 0, -1): | |
| if dp[i][c] != dp[i - 1][c]: | |
| name, weight, _ = items[i - 1] | |
| chosen.append(name) | |
| c -= weight | |
| chosen.reverse() | |
| weight = sum(w for name, w, _ in items if name in chosen) | |
| assert weight <= capacity | |
| result = {"items": chosen, "weight": weight, "value": dp[-1][capacity]} | |
| import json | |
| print(json.dumps(result, ensure_ascii=False, sort_keys=True)) | |