mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 09:21:13 +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:
@ -28,7 +28,7 @@ class AssignmentUsingBitmask:
|
||||
# to 1
|
||||
self.final_mask = (1 << len(task_performed)) - 1
|
||||
|
||||
def CountWaysUtil(self, mask, task_no):
|
||||
def count_ways_until(self, mask, task_no):
|
||||
|
||||
# if mask == self.finalmask all persons are distributed tasks, return 1
|
||||
if mask == self.final_mask:
|
||||
@ -43,7 +43,7 @@ class AssignmentUsingBitmask:
|
||||
return self.dp[mask][task_no]
|
||||
|
||||
# Number of ways when we don't this task in the arrangement
|
||||
total_ways_util = self.CountWaysUtil(mask, task_no + 1)
|
||||
total_ways_util = self.count_ways_until(mask, task_no + 1)
|
||||
|
||||
# now assign the tasks one by one to all possible persons and recursively
|
||||
# assign for the remaining tasks.
|
||||
@ -56,14 +56,14 @@ class AssignmentUsingBitmask:
|
||||
|
||||
# assign this task to p and change the mask value. And recursively
|
||||
# assign tasks with the new mask value.
|
||||
total_ways_util += self.CountWaysUtil(mask | (1 << p), task_no + 1)
|
||||
total_ways_util += self.count_ways_until(mask | (1 << p), task_no + 1)
|
||||
|
||||
# save the value.
|
||||
self.dp[mask][task_no] = total_ways_util
|
||||
|
||||
return self.dp[mask][task_no]
|
||||
|
||||
def countNoOfWays(self, task_performed):
|
||||
def count_no_of_ways(self, task_performed):
|
||||
|
||||
# Store the list of persons for each task
|
||||
for i in range(len(task_performed)):
|
||||
@ -71,7 +71,7 @@ class AssignmentUsingBitmask:
|
||||
self.task[j].append(i)
|
||||
|
||||
# call the function to fill the DP table, final answer is stored in dp[0][1]
|
||||
return self.CountWaysUtil(0, 1)
|
||||
return self.count_ways_until(0, 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -81,7 +81,7 @@ if __name__ == "__main__":
|
||||
# the list of tasks that can be done by M persons.
|
||||
task_performed = [[1, 3, 4], [1, 2, 5], [3, 4]]
|
||||
print(
|
||||
AssignmentUsingBitmask(task_performed, total_tasks).countNoOfWays(
|
||||
AssignmentUsingBitmask(task_performed, total_tasks).count_no_of_ways(
|
||||
task_performed
|
||||
)
|
||||
)
|
||||
|
@ -21,10 +21,10 @@ class EditDistance:
|
||||
def __init__(self):
|
||||
self.__prepare__()
|
||||
|
||||
def __prepare__(self, N=0, M=0):
|
||||
self.dp = [[-1 for y in range(0, M)] for x in range(0, N)]
|
||||
def __prepare__(self, n=0, m=0):
|
||||
self.dp = [[-1 for y in range(0, m)] for x in range(0, n)]
|
||||
|
||||
def __solveDP(self, x, y):
|
||||
def __solve_dp(self, x, y):
|
||||
if x == -1:
|
||||
return y + 1
|
||||
elif y == -1:
|
||||
@ -32,30 +32,30 @@ class EditDistance:
|
||||
elif self.dp[x][y] > -1:
|
||||
return self.dp[x][y]
|
||||
else:
|
||||
if self.A[x] == self.B[y]:
|
||||
self.dp[x][y] = self.__solveDP(x - 1, y - 1)
|
||||
if self.a[x] == self.b[y]:
|
||||
self.dp[x][y] = self.__solve_dp(x - 1, y - 1)
|
||||
else:
|
||||
self.dp[x][y] = 1 + min(
|
||||
self.__solveDP(x, y - 1),
|
||||
self.__solveDP(x - 1, y),
|
||||
self.__solveDP(x - 1, y - 1),
|
||||
self.__solve_dp(x, y - 1),
|
||||
self.__solve_dp(x - 1, y),
|
||||
self.__solve_dp(x - 1, y - 1),
|
||||
)
|
||||
|
||||
return self.dp[x][y]
|
||||
|
||||
def solve(self, A, B):
|
||||
if isinstance(A, bytes):
|
||||
A = A.decode("ascii")
|
||||
def solve(self, a, b):
|
||||
if isinstance(a, bytes):
|
||||
a = a.decode("ascii")
|
||||
|
||||
if isinstance(B, bytes):
|
||||
B = B.decode("ascii")
|
||||
if isinstance(b, bytes):
|
||||
b = b.decode("ascii")
|
||||
|
||||
self.A = str(A)
|
||||
self.B = str(B)
|
||||
self.a = str(a)
|
||||
self.b = str(b)
|
||||
|
||||
self.__prepare__(len(A), len(B))
|
||||
self.__prepare__(len(a), len(b))
|
||||
|
||||
return self.__solveDP(len(A) - 1, len(B) - 1)
|
||||
return self.__solve_dp(len(a) - 1, len(b) - 1)
|
||||
|
||||
|
||||
def min_distance_bottom_up(word1: str, word2: str) -> int:
|
||||
|
@ -2,41 +2,41 @@ import math
|
||||
|
||||
|
||||
class Graph:
|
||||
def __init__(self, N=0): # a graph with Node 0,1,...,N-1
|
||||
self.N = N
|
||||
self.W = [
|
||||
[math.inf for j in range(0, N)] for i in range(0, N)
|
||||
def __init__(self, n=0): # a graph with Node 0,1,...,N-1
|
||||
self.n = n
|
||||
self.w = [
|
||||
[math.inf for j in range(0, n)] for i in range(0, n)
|
||||
] # adjacency matrix for weight
|
||||
self.dp = [
|
||||
[math.inf for j in range(0, N)] for i in range(0, N)
|
||||
[math.inf for j in range(0, n)] for i in range(0, n)
|
||||
] # dp[i][j] stores minimum distance from i to j
|
||||
|
||||
def addEdge(self, u, v, w):
|
||||
def add_edge(self, u, v, w):
|
||||
self.dp[u][v] = w
|
||||
|
||||
def floyd_warshall(self):
|
||||
for k in range(0, self.N):
|
||||
for i in range(0, self.N):
|
||||
for j in range(0, self.N):
|
||||
for k in range(0, self.n):
|
||||
for i in range(0, self.n):
|
||||
for j in range(0, self.n):
|
||||
self.dp[i][j] = min(self.dp[i][j], self.dp[i][k] + self.dp[k][j])
|
||||
|
||||
def showMin(self, u, v):
|
||||
def show_min(self, u, v):
|
||||
return self.dp[u][v]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
graph = Graph(5)
|
||||
graph.addEdge(0, 2, 9)
|
||||
graph.addEdge(0, 4, 10)
|
||||
graph.addEdge(1, 3, 5)
|
||||
graph.addEdge(2, 3, 7)
|
||||
graph.addEdge(3, 0, 10)
|
||||
graph.addEdge(3, 1, 2)
|
||||
graph.addEdge(3, 2, 1)
|
||||
graph.addEdge(3, 4, 6)
|
||||
graph.addEdge(4, 1, 3)
|
||||
graph.addEdge(4, 2, 4)
|
||||
graph.addEdge(4, 3, 9)
|
||||
graph.add_edge(0, 2, 9)
|
||||
graph.add_edge(0, 4, 10)
|
||||
graph.add_edge(1, 3, 5)
|
||||
graph.add_edge(2, 3, 7)
|
||||
graph.add_edge(3, 0, 10)
|
||||
graph.add_edge(3, 1, 2)
|
||||
graph.add_edge(3, 2, 1)
|
||||
graph.add_edge(3, 4, 6)
|
||||
graph.add_edge(4, 1, 3)
|
||||
graph.add_edge(4, 2, 4)
|
||||
graph.add_edge(4, 3, 9)
|
||||
graph.floyd_warshall()
|
||||
graph.showMin(1, 4)
|
||||
graph.showMin(0, 3)
|
||||
graph.show_min(1, 4)
|
||||
graph.show_min(0, 3)
|
||||
|
@ -2,20 +2,20 @@ from bisect import bisect
|
||||
from itertools import accumulate
|
||||
|
||||
|
||||
def fracKnapsack(vl, wt, W, n):
|
||||
def frac_knapsack(vl, wt, w, n):
|
||||
"""
|
||||
>>> fracKnapsack([60, 100, 120], [10, 20, 30], 50, 3)
|
||||
>>> frac_knapsack([60, 100, 120], [10, 20, 30], 50, 3)
|
||||
240.0
|
||||
"""
|
||||
|
||||
r = list(sorted(zip(vl, wt), key=lambda x: x[0] / x[1], reverse=True))
|
||||
vl, wt = [i[0] for i in r], [i[1] for i in r]
|
||||
acc = list(accumulate(wt))
|
||||
k = bisect(acc, W)
|
||||
k = bisect(acc, w)
|
||||
return (
|
||||
0
|
||||
if k == 0
|
||||
else sum(vl[:k]) + (W - acc[k - 1]) * (vl[k]) / (wt[k])
|
||||
else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
|
||||
if k != n
|
||||
else sum(vl[:k])
|
||||
)
|
||||
|
@ -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
|
||||
|
@ -38,7 +38,7 @@ def longest_common_subsequence(x: str, y: str):
|
||||
n = len(y)
|
||||
|
||||
# declaring the array for storing the dp values
|
||||
L = [[0] * (n + 1) for _ in range(m + 1)]
|
||||
l = [[0] * (n + 1) for _ in range(m + 1)] # noqa: E741
|
||||
|
||||
for i in range(1, m + 1):
|
||||
for j in range(1, n + 1):
|
||||
@ -47,7 +47,7 @@ def longest_common_subsequence(x: str, y: str):
|
||||
else:
|
||||
match = 0
|
||||
|
||||
L[i][j] = max(L[i - 1][j], L[i][j - 1], L[i - 1][j - 1] + match)
|
||||
l[i][j] = max(l[i - 1][j], l[i][j - 1], l[i - 1][j - 1] + match)
|
||||
|
||||
seq = ""
|
||||
i, j = m, n
|
||||
@ -57,17 +57,17 @@ def longest_common_subsequence(x: str, y: str):
|
||||
else:
|
||||
match = 0
|
||||
|
||||
if L[i][j] == L[i - 1][j - 1] + match:
|
||||
if l[i][j] == l[i - 1][j - 1] + match:
|
||||
if match == 1:
|
||||
seq = x[i - 1] + seq
|
||||
i -= 1
|
||||
j -= 1
|
||||
elif L[i][j] == L[i - 1][j]:
|
||||
elif l[i][j] == l[i - 1][j]:
|
||||
i -= 1
|
||||
else:
|
||||
j -= 1
|
||||
|
||||
return L[m][n], seq
|
||||
return l[m][n], seq
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -34,12 +34,12 @@ def longest_subsequence(array: list[int]) -> list[int]: # This function is recu
|
||||
return array
|
||||
# Else
|
||||
pivot = array[0]
|
||||
isFound = False
|
||||
is_found = False
|
||||
i = 1
|
||||
longest_subseq: list[int] = []
|
||||
while not isFound and i < array_length:
|
||||
while not is_found and i < array_length:
|
||||
if array[i] < pivot:
|
||||
isFound = True
|
||||
is_found = True
|
||||
temp_array = [element for element in array[i:] if element >= array[i]]
|
||||
temp_array = longest_subsequence(temp_array)
|
||||
if len(temp_array) > len(longest_subseq):
|
||||
|
@ -7,7 +7,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def CeilIndex(v, l, r, key): # noqa: E741
|
||||
def ceil_index(v, l, r, key): # noqa: E741
|
||||
while r - l > 1:
|
||||
m = (l + r) // 2
|
||||
if v[m] >= key:
|
||||
@ -17,16 +17,16 @@ def CeilIndex(v, l, r, key): # noqa: E741
|
||||
return r
|
||||
|
||||
|
||||
def LongestIncreasingSubsequenceLength(v: list[int]) -> int:
|
||||
def longest_increasing_subsequence_length(v: list[int]) -> int:
|
||||
"""
|
||||
>>> LongestIncreasingSubsequenceLength([2, 5, 3, 7, 11, 8, 10, 13, 6])
|
||||
>>> longest_increasing_subsequence_length([2, 5, 3, 7, 11, 8, 10, 13, 6])
|
||||
6
|
||||
>>> LongestIncreasingSubsequenceLength([])
|
||||
>>> longest_increasing_subsequence_length([])
|
||||
0
|
||||
>>> LongestIncreasingSubsequenceLength([0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3,
|
||||
... 11, 7, 15])
|
||||
>>> longest_increasing_subsequence_length([0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13,
|
||||
... 3, 11, 7, 15])
|
||||
6
|
||||
>>> LongestIncreasingSubsequenceLength([5, 4, 3, 2, 1])
|
||||
>>> longest_increasing_subsequence_length([5, 4, 3, 2, 1])
|
||||
1
|
||||
"""
|
||||
if len(v) == 0:
|
||||
@ -44,7 +44,7 @@ def LongestIncreasingSubsequenceLength(v: list[int]) -> int:
|
||||
tail[length] = v[i]
|
||||
length += 1
|
||||
else:
|
||||
tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i]
|
||||
tail[ceil_index(tail, -1, length - 1, v[i])] = v[i]
|
||||
|
||||
return length
|
||||
|
||||
|
@ -8,34 +8,34 @@ Space Complexity: O(n^2)
|
||||
"""
|
||||
|
||||
|
||||
def MatrixChainOrder(array):
|
||||
N = len(array)
|
||||
Matrix = [[0 for x in range(N)] for x in range(N)]
|
||||
Sol = [[0 for x in range(N)] for x in range(N)]
|
||||
def matrix_chain_order(array):
|
||||
n = len(array)
|
||||
matrix = [[0 for x in range(n)] for x in range(n)]
|
||||
sol = [[0 for x in range(n)] for x in range(n)]
|
||||
|
||||
for ChainLength in range(2, N):
|
||||
for a in range(1, N - ChainLength + 1):
|
||||
b = a + ChainLength - 1
|
||||
for chain_length in range(2, n):
|
||||
for a in range(1, n - chain_length + 1):
|
||||
b = a + chain_length - 1
|
||||
|
||||
Matrix[a][b] = sys.maxsize
|
||||
matrix[a][b] = sys.maxsize
|
||||
for c in range(a, b):
|
||||
cost = (
|
||||
Matrix[a][c] + Matrix[c + 1][b] + array[a - 1] * array[c] * array[b]
|
||||
matrix[a][c] + matrix[c + 1][b] + array[a - 1] * array[c] * array[b]
|
||||
)
|
||||
if cost < Matrix[a][b]:
|
||||
Matrix[a][b] = cost
|
||||
Sol[a][b] = c
|
||||
return Matrix, Sol
|
||||
if cost < matrix[a][b]:
|
||||
matrix[a][b] = cost
|
||||
sol[a][b] = c
|
||||
return matrix, sol
|
||||
|
||||
|
||||
# Print order of matrix with Ai as Matrix
|
||||
def PrintOptimalSolution(OptimalSolution, i, j):
|
||||
def print_optiomal_solution(optimal_solution, i, j):
|
||||
if i == j:
|
||||
print("A" + str(i), end=" ")
|
||||
else:
|
||||
print("(", end=" ")
|
||||
PrintOptimalSolution(OptimalSolution, i, OptimalSolution[i][j])
|
||||
PrintOptimalSolution(OptimalSolution, OptimalSolution[i][j] + 1, j)
|
||||
print_optiomal_solution(optimal_solution, i, optimal_solution[i][j])
|
||||
print_optiomal_solution(optimal_solution, optimal_solution[i][j] + 1, j)
|
||||
print(")", end=" ")
|
||||
|
||||
|
||||
@ -44,10 +44,10 @@ def main():
|
||||
n = len(array)
|
||||
# Size of matrix created from above array will be
|
||||
# 30*35 35*15 15*5 5*10 10*20 20*25
|
||||
Matrix, OptimalSolution = MatrixChainOrder(array)
|
||||
matrix, optimal_solution = matrix_chain_order(array)
|
||||
|
||||
print("No. of Operation required: " + str(Matrix[1][n - 1]))
|
||||
PrintOptimalSolution(OptimalSolution, 1, n - 1)
|
||||
print("No. of Operation required: " + str(matrix[1][n - 1]))
|
||||
print_optiomal_solution(optimal_solution, 1, n - 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -4,14 +4,14 @@ author : Mayank Kumar Jha (mk9440)
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def find_max_sub_array(A, low, high):
|
||||
def find_max_sub_array(a, low, high):
|
||||
if low == high:
|
||||
return low, high, A[low]
|
||||
return low, high, a[low]
|
||||
else:
|
||||
mid = (low + high) // 2
|
||||
left_low, left_high, left_sum = find_max_sub_array(A, low, mid)
|
||||
right_low, right_high, right_sum = find_max_sub_array(A, mid + 1, high)
|
||||
cross_left, cross_right, cross_sum = find_max_cross_sum(A, low, mid, high)
|
||||
left_low, left_high, left_sum = find_max_sub_array(a, low, mid)
|
||||
right_low, right_high, right_sum = find_max_sub_array(a, mid + 1, high)
|
||||
cross_left, cross_right, cross_sum = find_max_cross_sum(a, low, mid, high)
|
||||
if left_sum >= right_sum and left_sum >= cross_sum:
|
||||
return left_low, left_high, left_sum
|
||||
elif right_sum >= left_sum and right_sum >= cross_sum:
|
||||
@ -20,18 +20,18 @@ def find_max_sub_array(A, low, high):
|
||||
return cross_left, cross_right, cross_sum
|
||||
|
||||
|
||||
def find_max_cross_sum(A, low, mid, high):
|
||||
def find_max_cross_sum(a, low, mid, high):
|
||||
left_sum, max_left = -999999999, -1
|
||||
right_sum, max_right = -999999999, -1
|
||||
summ = 0
|
||||
for i in range(mid, low - 1, -1):
|
||||
summ += A[i]
|
||||
summ += a[i]
|
||||
if summ > left_sum:
|
||||
left_sum = summ
|
||||
max_left = i
|
||||
summ = 0
|
||||
for i in range(mid + 1, high + 1):
|
||||
summ += A[i]
|
||||
summ += a[i]
|
||||
if summ > right_sum:
|
||||
right_sum = summ
|
||||
max_right = i
|
||||
|
@ -7,7 +7,7 @@ https://www.hackerrank.com/challenges/coin-change/problem
|
||||
"""
|
||||
|
||||
|
||||
def dp_count(S, n):
|
||||
def dp_count(s, n):
|
||||
"""
|
||||
>>> dp_count([1, 2, 3], 4)
|
||||
4
|
||||
@ -33,7 +33,7 @@ def dp_count(S, n):
|
||||
# Pick all coins one by one and update table[] values
|
||||
# after the index greater than or equal to the value of the
|
||||
# picked coin
|
||||
for coin_val in S:
|
||||
for coin_val in s:
|
||||
for j in range(coin_val, n + 1):
|
||||
table[j] += table[j - coin_val]
|
||||
|
||||
|
@ -3,7 +3,7 @@ Partition a set into two subsets such that the difference of subset sums is mini
|
||||
"""
|
||||
|
||||
|
||||
def findMin(arr):
|
||||
def find_min(arr):
|
||||
n = len(arr)
|
||||
s = sum(arr)
|
||||
|
||||
|
@ -1,25 +1,25 @@
|
||||
def isSumSubset(arr, arrLen, requiredSum):
|
||||
def is_sum_subset(arr, arr_len, required_sum):
|
||||
"""
|
||||
>>> isSumSubset([2, 4, 6, 8], 4, 5)
|
||||
>>> is_sum_subset([2, 4, 6, 8], 4, 5)
|
||||
False
|
||||
>>> isSumSubset([2, 4, 6, 8], 4, 14)
|
||||
>>> is_sum_subset([2, 4, 6, 8], 4, 14)
|
||||
True
|
||||
"""
|
||||
# a subset value says 1 if that subset sum can be formed else 0
|
||||
# initially no subsets can be formed hence False/0
|
||||
subset = [[False for i in range(requiredSum + 1)] for i in range(arrLen + 1)]
|
||||
subset = [[False for i in range(required_sum + 1)] for i in range(arr_len + 1)]
|
||||
|
||||
# for each arr value, a sum of zero(0) can be formed by not taking any element
|
||||
# hence True/1
|
||||
for i in range(arrLen + 1):
|
||||
for i in range(arr_len + 1):
|
||||
subset[i][0] = True
|
||||
|
||||
# sum is not zero and set is empty then false
|
||||
for i in range(1, requiredSum + 1):
|
||||
for i in range(1, required_sum + 1):
|
||||
subset[0][i] = False
|
||||
|
||||
for i in range(1, arrLen + 1):
|
||||
for j in range(1, requiredSum + 1):
|
||||
for i in range(1, arr_len + 1):
|
||||
for j in range(1, required_sum + 1):
|
||||
if arr[i - 1] > j:
|
||||
subset[i][j] = subset[i - 1][j]
|
||||
if arr[i - 1] <= j:
|
||||
@ -28,7 +28,7 @@ def isSumSubset(arr, arrLen, requiredSum):
|
||||
# uncomment to print the subset
|
||||
# for i in range(arrLen+1):
|
||||
# print(subset[i])
|
||||
print(subset[arrLen][requiredSum])
|
||||
print(subset[arr_len][required_sum])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Reference in New Issue
Block a user