mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-06 10:31:29 +08:00
Add pep8-naming to pre-commit hooks and fixes incorrect naming conventions (#7062)
* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (#7038) * refactor: Fix naming conventions (#7038) * Update arithmetic_analysis/lu_decomposition.py Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (#7038) * chore: Fix naming conventions in doctests (#7038) * fix: Temporarily disable project euler problem 104 (#7069) * chore: Fix naming conventions in doctests (#7038) Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@ -7,39 +7,39 @@ Note that only the integer weights 0-1 knapsack problem is solvable
|
||||
"""
|
||||
|
||||
|
||||
def MF_knapsack(i, wt, val, j):
|
||||
def mf_knapsack(i, wt, val, j):
|
||||
"""
|
||||
This code involves the concept of memory functions. Here we solve the subproblems
|
||||
which are needed unlike the below example
|
||||
F is a 2D array with -1s filled up
|
||||
"""
|
||||
global F # a global dp table for knapsack
|
||||
if F[i][j] < 0:
|
||||
global f # a global dp table for knapsack
|
||||
if f[i][j] < 0:
|
||||
if j < wt[i - 1]:
|
||||
val = MF_knapsack(i - 1, wt, val, j)
|
||||
val = mf_knapsack(i - 1, wt, val, j)
|
||||
else:
|
||||
val = max(
|
||||
MF_knapsack(i - 1, wt, val, j),
|
||||
MF_knapsack(i - 1, wt, val, j - wt[i - 1]) + val[i - 1],
|
||||
mf_knapsack(i - 1, wt, val, j),
|
||||
mf_knapsack(i - 1, wt, val, j - wt[i - 1]) + val[i - 1],
|
||||
)
|
||||
F[i][j] = val
|
||||
return F[i][j]
|
||||
f[i][j] = val
|
||||
return f[i][j]
|
||||
|
||||
|
||||
def knapsack(W, wt, val, n):
|
||||
dp = [[0 for i in range(W + 1)] for j in range(n + 1)]
|
||||
def knapsack(w, wt, val, n):
|
||||
dp = [[0 for i in range(w + 1)] for j in range(n + 1)]
|
||||
|
||||
for i in range(1, n + 1):
|
||||
for w in range(1, W + 1):
|
||||
for w in range(1, w + 1):
|
||||
if wt[i - 1] <= w:
|
||||
dp[i][w] = max(val[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w])
|
||||
else:
|
||||
dp[i][w] = dp[i - 1][w]
|
||||
|
||||
return dp[n][W], dp
|
||||
return dp[n][w], dp
|
||||
|
||||
|
||||
def knapsack_with_example_solution(W: int, wt: list, val: list):
|
||||
def knapsack_with_example_solution(w: int, wt: list, val: list):
|
||||
"""
|
||||
Solves the integer weights knapsack problem returns one of
|
||||
the several possible optimal subsets.
|
||||
@ -90,9 +90,9 @@ def knapsack_with_example_solution(W: int, wt: list, val: list):
|
||||
f"got weight of type {type(wt[i])} at index {i}"
|
||||
)
|
||||
|
||||
optimal_val, dp_table = knapsack(W, wt, val, num_items)
|
||||
optimal_val, dp_table = knapsack(w, wt, val, num_items)
|
||||
example_optional_set: set = set()
|
||||
_construct_solution(dp_table, wt, num_items, W, example_optional_set)
|
||||
_construct_solution(dp_table, wt, num_items, w, example_optional_set)
|
||||
|
||||
return optimal_val, example_optional_set
|
||||
|
||||
@ -136,10 +136,10 @@ if __name__ == "__main__":
|
||||
wt = [4, 3, 2, 3]
|
||||
n = 4
|
||||
w = 6
|
||||
F = [[0] * (w + 1)] + [[0] + [-1 for i in range(w + 1)] for j in range(n + 1)]
|
||||
f = [[0] * (w + 1)] + [[0] + [-1 for i in range(w + 1)] for j in range(n + 1)]
|
||||
optimal_solution, _ = knapsack(w, wt, val, n)
|
||||
print(optimal_solution)
|
||||
print(MF_knapsack(n, wt, val, w)) # switched the n and w
|
||||
print(mf_knapsack(n, wt, val, w)) # switched the n and w
|
||||
|
||||
# testing the dynamic programming problem with example
|
||||
# the optimal subset for the above example are items 3 and 4
|
||||
|
Reference in New Issue
Block a user