Modify the below existing codes to print the output in Python. Explain the changes made to the codes.

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

Modify the below existing codes to print the output in Python.

Explain the changes made to the codes.

# Returns true if there exists a sublist of list `nums[0…n]` with the given total
def subsetSum(nums, n, total):

# return true if the sum becomes 0 (subset found)
if total == 0:
return True

# base case: no items left or sum becomes negative
if n < 0 or total < 0:
return False

# Case 1. Include the current item `nums[n]` in the subset and recur
# for remaining items `n-1` with the remaining sum `total-nums[n]`
include = subsetSum(nums, n - 1, total - nums[n])

# return true if we get subset by including the current item
if include:
return True

# Case 2. Exclude the current item `nums[n]` from the subset and recur for
# remaining items `n-1`
exclude = subsetSum(nums, n - 1, total)

# return true if we get subset by excluding the current item
return exclude


# Returns true if given list `nums[0…n-1]` can be divided into two
# sublists with equal sum
def partition(nums):

total = sum(nums)

# return true if the sum is even and the list can be divided into
# two sublists with equal sum
return (total & 1) == 0 and subsetSum(nums, len(nums) - 1, total/2)


if __name__ == '__main__':

# Input: a set of items
nums = [8, 4, 6, 2, 5, 3]

if partition(nums):
print('Set can be partitioned')
else:
print('Set cannot be partitioned')

Output:

Set can be partitioned
[4, 2, 5, 3]
[8, 6]

Expert Solution
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Matrix multiplication
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
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