import sys def subsetSum(nums, total): n = len(nums) T = [[False for x in range(total + 1)] for y in range(n + 1)] for i in range(n + 1): T[i][0] = True for i in range(1, n + 1): for j in range(1, total + 1): if nums[i - 1] > j: T[i][j] = T[i - 1][j] else: T[i][j] = T[i - 1][j] or T[i - 1][j - nums[i - 1]] return T[n][total] def partition(nums): total = sum(nums) return total & 1 == 0 and subsetSum(nums, total // 2) def backtrack(nums, i, p, q): if i == len(nums): if sum(p) == sum(q): print(f"List: {nums}") print("The two sublists with equal sum are: ") print(p) print(q) sys.exit() return p.append(nums[i]) backtrack(nums, i + 1, p, q) p.pop() q.append(nums[i]) backtrack(nums, i + 1, p, q) q.pop() if __name__ == '__main__': nums = [7, 3, 1, 5, 4, 8] if partition(nums): backtrack(nums, 0, [], []) else: print('Set cannot be partitioned') How can I edit my code such that it can handle float input? The image shows the error I have encountered. Thanks in advanced!

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter8: Advanced Method Concepts
Section: Chapter Questions
Problem 8RQ
Question

import sys

def subsetSum(nums, total):
n = len(nums)

T = [[False for x in range(total + 1)] for y in range(n + 1)]

for i in range(n + 1):
T[i][0] = True

for i in range(1, n + 1):

for j in range(1, total + 1):

if nums[i - 1] > j:
T[i][j] = T[i - 1][j]
else:


T[i][j] = T[i - 1][j] or T[i - 1][j - nums[i - 1]]

return T[n][total]


def partition(nums):
total = sum(nums)
return total & 1 == 0 and subsetSum(nums, total // 2)

def backtrack(nums, i, p, q):
if i == len(nums):
if sum(p) == sum(q):
print(f"List: {nums}")
print("The two sublists with equal sum are: ")
print(p)
print(q)
sys.exit()
return
p.append(nums[i])
backtrack(nums, i + 1, p, q)
p.pop()
q.append(nums[i])
backtrack(nums, i + 1, p, q)
q.pop()


if __name__ == '__main__':

nums = [7, 3, 1, 5, 4, 8]
if partition(nums):
backtrack(nums, 0, [], [])
else:
print('Set cannot be partitioned')

How can I edit my code such that it can handle float input? The image shows the error I have encountered. Thanks in advanced!

return int(total & 1 == 0) and int(subsetSum(nums, total // 2))
TypeError: unsupported operand type(s) for &: 'float' and 'int'
Transcribed Image Text:return int(total & 1 == 0) and int(subsetSum(nums, total // 2)) TypeError: unsupported operand type(s) for &: 'float' and 'int'
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Counting Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning