python - Output error is 0x03BB7390 -
def fastmaxval(w, v, i, aw): global numcalls numcalls += 1 try: return m[(i, aw)] except keyerror: if == 0: if w[i] <= aw: m[(i, aw)] = v[i] return v[i] else: m[(i, aw)] = 0 return 0 without_i = fastmaxval(w, v, i-1, aw) if w[i] > aw: m[(i, aw)] = without_i return without_i else: with_i = v[i] + fastmaxval(w, v, i-1, aw - w[i]) res = max(with_i, without_i) m[(i, aw)] = res return res def maxval0(w, v, i, aw): m = {} return fastmaxval weights = [1, 1, 5, 5, 3, 3, 4, 4] vals = [15, 15, 10, 10, 9, 9, 5, 5] numcalls = 0 res = maxval0(weights, vals, len(vals) - 1, 8) print ('max val =', res, 'number of calls =', numcalls)
the program 01knapsack problem. use decision tree solve problem. however, ran program , got following result.
max val = function fastmaxval @ 0x03bb7390 number of calls = 0
what's wrong program?
in maxval0
function you're returning fastmaxval
function itself, instead of call result.
i think should this:
def maxval0(w, v, i, aw): m = {} return fastmaxval(w, v, i, aw)
also, fastmaxval
using m
, isn't in scope.
Comments
Post a Comment