Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subset_sum_all_approaches.py #12134

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions knapsack/subset_sum_all_approaches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""
Given a set of integers and a target sum, determine if there exists a subset whose sum equals the target.

Check failure on line 2 in knapsack/subset_sum_all_approaches.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

knapsack/subset_sum_all_approaches.py:2:89: E501 Line too long (105 > 88)
Return True if such a subset exists, otherwise return False.
"""
------------------------------------------------------Recursion-Approach-------------------------------------------------------------------------

Check failure on line 6 in knapsack/subset_sum_all_approaches.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

knapsack/subset_sum_all_approaches.py:5:146: SyntaxError: Expected an expression
def is_subset_sum_recursive(arr, n, target):
# Base cases
if target == 0:
return True
if n == 0:
return False

# If last element is greater than target, ignore it
if arr[n-1] > target:
return is_subset_sum_recursive(arr, n-1, target)

# Check if including or excluding the current item achieves the target
return is_subset_sum_recursive(arr, n-1, target) or is_subset_sum_recursive(arr, n-1, target - arr[n-1])

Check failure on line 19 in knapsack/subset_sum_all_approaches.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

knapsack/subset_sum_all_approaches.py:19:89: E501 Line too long (108 > 88)

arr = [3, 34, 4, 12, 5, 2]
target = 9
n = len(arr)
print(is_subset_sum_recursive(arr, n, target)) # Output: True

arr = [3, 34, 4, 12, 5, 2]
target = 30
n = len(arr)
print(is_subset_sum_recursive(arr, n, target)) # Output: False

-------------------------------------------------------Memoization-Approach-------------------------------------------------------------------------

Check failure on line 32 in knapsack/subset_sum_all_approaches.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

knapsack/subset_sum_all_approaches.py:31:149: SyntaxError: Expected an expression
def is_subset_sum_memo(arr, n, target, memo):
# Base cases
if target == 0:
return True
if n == 0:
return False

# If this subproblem has already been solved
if memo[n][target] is not None:
return memo[n][target]

# If last element is greater than target, ignore it
if arr[n-1] > target:
memo[n][target] = subset_sum_memo(arr, n-1, target, memo)
else:
# Check if including or excluding the current item achieves the target
memo[n][target] = subset_sum_memo(arr, n-1, target, memo) or subset_sum_memo(arr, n-1, target - arr[n-1], memo)

Check failure on line 49 in knapsack/subset_sum_all_approaches.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

knapsack/subset_sum_all_approaches.py:49:89: E501 Line too long (119 > 88)

return memo[n][target]

# Helper function
def subset_sum_memo_helper(arr, target):
n = len(arr)
memo = [[None] * (target + 1) for _ in range(n + 1)]
return is_subset_sum_memo(arr, n, target, memo)

arr = [3, 34, 4, 12, 5, 2]
target = 9
print(subset_sum_memo_helper(arr, target)) # Output: True

arr = [3, 34, 4, 12, 5, 2]
target = 30
print(subset_sum_memo_helper(arr, target)) # Output: False

-------------------------------------------------------Top-Down-Approach--------------------------------------------------------------------

Check failure on line 68 in knapsack/subset_sum_all_approaches.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

knapsack/subset_sum_all_approaches.py:67:141: SyntaxError: Expected an expression
def is_subset_sum_dp(arr, target):
n = len(arr)
# Create a (n+1) x (target+1) DP table
dp = [[False] * (target + 1) for _ in range(n + 1)]

# Initialize the table: If target is 0, answer is True
for i in range(n + 1):
dp[i][0] = True

# Fill the DP table
for i in range(1, n + 1):
for j in range(1, target + 1):
if arr[i-1] > j:
dp[i][j] = dp[i-1][j]
else:
dp[i][j] = dp[i-1][j] or dp[i-1][j - arr[i-1]]

return dp[n][target]

arr = [3, 34, 4, 12, 5, 2]
target = 9
print(is_subset_sum_dp(arr, target)) # Output: True

arr = [3, 34, 4, 12, 5, 2]
target = 30
print(is_subset_sum_dp(arr, target)) # Output: False
Loading