1

Lua

values = {1, 2, 3, 4, 5, 6, 7, 8, 9};
inSack = {}
total  = 0;
function knapsack(i, weight)
        if weight == 0 then
                print("Success The following combination is in the knapsack:");
                for i = 1, #inSack do
                        total = total + inSack[i];
                        print(inSack[i]);
                end
                print("Totaling to Input of: "..total)
                return
        elseif i <= 0 then
                print("Sorry your weight combination, is not possible with the current values ");
                return;
        end
        if values[i] > weight then
                return knapsack(i-1, weight);
        else 
                inSack[#inSack + 1] = values[i];
                return knapsack(i-1, weight - values[i]);
        end

end
-- Waits for user input to terminal
local number = io.read()
knapsack(#values, tonumber(number));

My Python code

values = [1,2,3,4,5,6,7,8,9]
inSack = []
total = 0

def knapsack(i,weight):
    if weight == 0:
        print("success: ")
        for i in inSack:
            total = total +inSack[i]
            print(inSack[i])
        print("totaling to input of: "+total)
        return
    elif i<= 0:
        print("didn't work yo")
        return
    if values[i] > weight:
        return knapsack(i-1, weight)
    else:
        inSack[inSack+1] = values[i]
        return knapsack(i-1, weight - values[i])

number = raw_input("Enter a number: ")
knapsack(values, number)

I'm getting errors with the if values[i] > weight statement that i ported to python. What is the mistake I'm making?

Traceback

Traceback (most recent call last):
  File "lua.py", line 23, in <module>
    knapsack(values, number)
  File "lua.py", line 16, in knapsack
    if values[i] > weight:
TypeError: list indices must be integers, not list
9
  • inSack[inSack+1] = values[i] Adding 1 to inSack which supposed to be a list? Maybe inSack.append(values[i]) instead? :) Also for i in inSack: iterates throught inSack elements. Not it's indices. for index, element in inSack: instead. Commented Oct 16, 2013 at 16:45
  • Considering i is a list, then this comparison makes no sense :i <= 0. (Note that in the loop for i in inSack:, you've actually masked the original item passed to i) Commented Oct 16, 2013 at 16:46
  • 1
    Stop trying to use Lua tables as Python lists...They are sooo different. :P Commented Oct 16, 2013 at 16:47
  • 1
    In python to sum a list you can simply do sum(inSack). Commented Oct 16, 2013 at 16:47
  • The traceback you copy paste is probably missing the last line: TypeError: list indices must be integers, not list. Commented Oct 16, 2013 at 16:52

1 Answer 1

2

I guess you are missing a len() at the end ; the Python equivalent of

knapsack(#values, tonumber(number));

would be

knapsack(len(values), number)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.