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!

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education