A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
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?
anonymous
 5 years ago
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?

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0When you say, "its not working", do you get an error message or does it not output anything? Try posting your code to pastebin.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0well 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
 5 years ago
Best ResponseYou've already chosen the best response.0HERES 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
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.