Taylor Higgins
Taylor Higgins

Reputation: 35

Fractional Knapsack Algorithm Python Input Issues

I have a fledgling knowledge of python and am trying to solve this knapsack algorithm problem.

An example input/output is:

Input:

3 50

60 20

100 50

120 30

Output:

180.000

Below is the code I don't understand, which was provided in the starter code file for the algorithm problem. For the previous problem sets I'd been replacing sys.stdin.read() with input() because I understand it better. I think it's trying to map the inputs so it ends up being: n = 3, capacity = 50, values = [60, 100, 120], weights = [20, 50, 30].

if __name__ == "__main__":
    data = list(map(int, sys.stdin.read().split()))
    n, capacity = data[0:2]
    values = data[2:(2 * n + 2):2]
    weights = data[3:(2 * n + 2):2]
    opt_value = get_optimal_value(capacity, weights, values)
    print("{:.10f}".format(opt_value))

I read somewhere on stack that I may need to change the last line to:

print("{!s:10f}".format(opt_value))

but that produced a different error

ValueError: Unknown format code 'f' for object of type 'str'.

Please if you can help me understand or point me in the right direction, I'd really appreciate it. Thank you.

Upvotes: 0

Views: 590

Answers (2)

Ajay Shewale
Ajay Shewale

Reputation: 159

You can also use below code.

if __name__ == "__main__":
data = list(map(int, input().split()))
n, capacity = data
values = [0]*n
weights = [0]*n
for i in range(n):
    values[i], weights[i] = map(int, input().split())
opt_value = get_optimal_value(capacity, weights, values)
print("{:.10f}".format(opt_value))

Upvotes: 1

Peritract
Peritract

Reputation: 769

  1. I'd go back to input(), as it's simpler to read and is one less thing to fret about.

  2. Your final point about the last line producing "unknown format code'f' for object of type 'str'" probably means that it is trying to format a number, but you are giving it a string. Check the type of the value that get_optimal_value() returns.

  3. Here's an extremely roughly commented version of your code:

    if __name__ == "__main__": #if it's the main program
    data = list(map(int, sys.stdin.read().split())) #make a list of the input, split up and turned into integers.
    n, capacity = data[0:2] #set n and capacity to the first two values in data
    values = data[2:(2 * n + 2):2] set values equal to a subsection of data. Specifically, every second character from index 2 to index (2 * n + 2).
    weights = data[3:(2 * n + 2):2] #the same as the line above, but starting at 3.
    opt_value = get_optimal_value(capacity, weights, values) #set opt_value to whatever the function returns.
    print("{:.10f}".format(opt_value)) #print the value of opt_value in a fancy way. Specifically, as a floating point number. If you're currently feeding it a string, then Python will squawk at you.
    
  4. Here are some useful links that might help: https://www.digitalocean.com/community/tutorials/how-to-index-and-slice-strings-in-python-3 https://pyformat.info

Hopefully some of the above is useful to you.

Upvotes: 0

Related Questions