Based on the previous task (https://www.chegg.com/homework-help/questions-and-an
ID: 3839282 • Letter: B
Question
Based on the previous task (https://www.chegg.com/homework-help/questions-and-answers/python-suppose-three-people-acquire-bag-jewels-know-exact-positive-value--wish-know-whethe-q19303264), we now have to do this. I don't think I really understand what fork does and how it would be used in this case. Can someone explain it in theory first?
import os def subsetSum (A,target): subset for i in A: if os fork 0: subset append(i) if sum (subset) targets print subset sums to target) If I run subset Sum (C1,5,2,7,18,22],15) It outputs: 1, 5, 2, 7] sums to 15 Recall that the os command causes the execution of the program to split into two (to enter two parallel universes as I said in class). In one of these two executions 0 is returned, in the other a nonzero value is returned. Note that this solution runs in time O(N), assuming the fork's are free. This is the nondeterministic execution time. Since N is a polynomial, the problem subset-surn can be solved in nondeterministic polynomial time and thus is in NP Your task for this question is to write code using fork to solve the Fair Share problem. It should have the same functionality as your previous code but should run in nondeterministic polynomial time. Clearly indicate the nondeterministic runtime of your code. Note: You may encounter difficulties getting code with fork to run. Suggestions: (1) Use very small inputs. (2) Run from the command line rather than from the IDE. (3) If all else fails, don't run it, but submit your code assuming fork works as describedExplanation / Answer
import os
def doJob(job):
for i in xrange(10):
if job > 0:
print(job, i)
time.sleep(1)
Father = True
sebset = []
def subsetSum(A , target):
for i in A:
pid = os.fork()
if pid == 0:
sebset.append(i)
if sum(sebset) == target:
print(sebset, "sum to ", target)
else:
Father = False
doJob(i)
break
if Father:
for pid in sebset:
os.waitpid(sebset, 0)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.