anonymous
  • anonymous
I am stuck on problem number 4 for ps8. I am storing the entire input to the dpadvisorhelper function as a tuple (with the list elements converted to a tuple) in a global dictionary as keys and the 2 returned elements as corresponding data values for memoization. But its not working. Any suggestions?
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
anonymous
  • anonymous
When you say, "its not working", do you get an error message or does it not output anything? Try posting your code to pastebin.
anonymous
  • anonymous
well I tried checking if the dictionary was being used but at every function call for the recursive function dpadvisorhelper, the function never uses any of the global dictionary elements and hence dynamic programming isnt implemented.
anonymous
  • anonymous
HERES THE CODE def dpAdvisor(subjects, maxWork): """ Returns a dictionary mapping subject name to (value, work) that contains a set of subjects that provides the maximum value without exceeding maxWork. subjects: dictionary mapping subject name to (value, work) maxWork: int >= 0 returns: dictionary mapping subject name to (value, work) """ # TODO... nameList = subjects.keys() tupleList = subjects.values() bestSubset, bestSubsetValue = \ dpAdvisorHelper(tupleList, maxWork, 0, None, None, [], 0, 0) outputSubjects = {} for i in bestSubset: outputSubjects[nameList[i]] = tupleList[i] return outputSubjects dpdict = {} def list2tuple(lyst): if lyst==None: return None ret = () for element in lyst: ret += (element,) return ret def dpAdvisorHelper(subjects, maxWork, i, bestSubset, bestSubsetValue, subset, subsetValue, subsetWork): global dpdict #saving the input elements as a tuple inpoot = (i,list2tuple(bestSubset),bestSubsetValue,list2tuple(subset),subsetValue,subsetWork) #cant figure out why this condition is never satisfied if dpdict.get(inpoot,0)!=0: print 'DP used' return dpdict[inpoot][0],dpdict[inpoot][1] #from here on rest of code is pretty much same as bruteforceadvisorhelper # Hit the end of the list. if i >= len(subjects): if bestSubset == None or subsetValue > bestSubsetValue: # Found a new best. dpdict[inpoot] = (subset[:], subsetValue) return subset[:], subsetValue else: # Keep the current best. dpdict[inpoot] = (bestSubset, bestSubsetValue) return bestSubset, bestSubsetValue else: s = subjects[i] # Try including subjects[i] in the current working subset. if subsetWork + s[WORK] <= maxWork: subset.append(i) bestSubset, bestSubsetValue = dpAdvisorHelper(subjects, maxWork, i+1, bestSubset, bestSubsetValue, subset, subsetValue + s[VALUE], subsetWork + s[WORK]) subset.pop() bestSubset, bestSubsetValue = dpAdvisorHelper(subjects, maxWork, i+1, bestSubset, bestSubsetValue, subset, subsetValue, subsetWork) dpdict[inpoot] = (bestSubset, bestSubsetValue) return bestSubset, bestSubsetValue

Looking for something else?

Not the answer you are looking for? Search for more explanations.