Question

In: Computer Science

Peter and John are great friends. They like to play games with numbers. This time, Peter...

Peter and John are great friends. They like to play games with numbers. This time, Peter has given John a list of numbers and given him the task of determining if it is possible to choose a subset of them such that they sum is equal to another given number.

Build an algorithm in Python using brute force to help John with his problem.

INPUT

  • The first line corresponds to N, the amount of numbers given by Peter
  • The next N lines contain each of the numbers
  • The last line contains the desired sum

OUTPUT

  • True or False (Whether is possible to obtain the desired sum or not)

Sample Input 1:

7

7

3

6

1

10

11

4

20

Sample Output 1:

True

Sample Input 2:

4

1

2

3

4

2

Sample Output 2:

True

Sample Input 3:

5

2

1

5

8

10

4

Sample Output 3:

False

Sample Input 4:

5

92

16

27

72

96

303

Sample Output 4:

True

Sample Input 5:

8

82

36

46

91

42

3

57

61

294

Sample Output 5:

True

Write a program, test using stdin → stdout

Solutions

Expert Solution

def isSubsetSum(set, n, sum):
   subset =([[False for i in range(sum + 1)] for i in range(n + 1)])
   for i in range(n + 1):
       subset[i][0] = True
   for i in range(1, sum + 1):
       subset[0][i]= False
   for i in range(1, n + 1):
       for j in range(1, sum + 1):
           if j<set[i-1]:
               subset[i][j] = subset[i-1][j]
           if j>= set[i-1]:
               subset[i][j] = (subset[i-1][j] or subset[i - 1][j-set[i-1]])
   return subset[n][sum]
      

n = int(input())
set=[]
for i in range(0,n):
s = int(input())
set.append(s)
sum = int(input())

if (isSubsetSum(set, n, sum) == True):
print("True")
else:
print("False")
      


Related Solutions

Michael and Mary are great friends. They like to play games with numbers. This time, Michael...
Michael and Mary are great friends. They like to play games with numbers. This time, Michael has given Mary a list of numbers and given him the task of determining if it is possible to choose a subset of them such that they sum is equal to another given number. Build an algorithm using dynamic programming to help Mary with his problem. INPUT The first line corresponds to N, the amount of numbers given by Michael The next N lines...
1. The following conversation took place between two friends John and Peter. John is the general...
1. The following conversation took place between two friends John and Peter. John is the general manager of XYZ Company while Peter is the CEO of LMN Company: John: Hi Peter, it is nice to meet you. I want to share to you the new policies and procedures that we have implemented in my company. Peter: Oh really, I want to know those new policies so maybe I can also use and apply those in my company specially at this...
Peter and John are tossing a coin. If it is a Head, then peter will give...
Peter and John are tossing a coin. If it is a Head, then peter will give John 1dollar. If it is a Tail, then John will give Peter 1dollar. Suppose Peter has a dollar, John has b dollar. Question: the probability that Peter wins all the money from John.
A group of six friends play some games of ping-pong with these results: Amy beats Bob...
A group of six friends play some games of ping-pong with these results: Amy beats Bob Bob beats Carl Frank beats Bob Amy beats Elise Carl beats Dave Elise beats Carl Elise beats Dave Frank beats Elise Frank beats Amy Consider the relation R = {hx, yi : x has beaten y}. (a) Draw the directed graph G representing R. (b) Is R reflexive? Irreflexive? Symmetric? Asymmetric? Antisymmetric? Transitive? An equivalence? An order? (c) The players want to rank themselves....
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like...
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like Games was launched eight years ago, whereas Our Play is a relatively new company that has been in operation for only the past two years. However, both companies have an equal market share with sales of $200,000 each. You’ve collected company data to compare Like Games and Our Play. Last year, the average sales for all industry competitors was $510,000. As an analyst, you...
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like...
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like Games was launched eight years ago, whereas Our Play is a relatively new company that has been in operation for only the past two years. However, both companies have an equal market share with sales of $200,000 each. You’ve collected company data to compare Like Games and Our Play. Last year, the average sales for all industry competitors was $510,000. As an analyst, you...
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like...
You are analyzing two companies that manufacture electronic toys—Like Games Inc. and Our Play Inc. Like Games was launched eight years ago, whereas Our Play is a relatively new company that has been in operation for only the past two years. However, both companies have an equal market share with sales of $300,000 each. You’ve collected company data to compare Like Games and Our Play. Last year, the average sales for all industry competitors was $765,000. As an analyst, you...
Dam and peter both like to count things. Adam likes yo count numbers devisible by 7....
Dam and peter both like to count things. Adam likes yo count numbers devisible by 7. Peter likes to count numbers that add to 7 (e.g. 7, 16, 25 etc). Output all the bunbers that both adam and peter like tk count between 1 and 1000 c++
You are analyzing two companies that manufacture electronic toys - Like Games Inc. and Our Play...
You are analyzing two companies that manufacture electronic toys - Like Games Inc. and Our Play Inc. Like Games was launched eight years ago, whereas Our Play is a relatively new company that has been in operation for only the past two years. However, both companies have an equal market share with sales of $400,000 each. You've collected company data to compare Like Games and Our Play. Last year, the average sales for all industry competitors was $1,020,000. As an...
Great Games Company operates miniature golf courses throughout southern Arizona. On July 1, 2016, Great Games’...
Great Games Company operates miniature golf courses throughout southern Arizona. On July 1, 2016, Great Games’ balance sheet showed (all dollar figures in thousands): Cash $40 -- Accounts Payable $50 -- Credit Card Receivables 25 -- Accruals Payable 0 -- Golf Supplies 35 -- Long-term Notes Payable 0 -- Office Equipment (net) 45 -- Common Stock 65 -- Retained Earnings 30 Draft the journal Entry and Balance Sheet During July the following events occurred to Great Games: a) Collected $20...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT