Skip to main content
Tweeted twitter.com/StackCodeReview/status/1247041580025946118
added 8 characters in body
Source Link

I have implemented a solution for the knapsack problem white fractionwhile breaking an item is allowed. I've used the dictionary data structure where I used weight as a key and value per weight as a value. After that, I sorted the dictionary by value and filled the knapsack until there's no space left.

Here is my python code -

def calculateKnapsack():
    userinputNW = list(map(int, input().split()))
    n = userinputNW[0]
    weight = userinputNW[1]
    weightvaluedict = {}
    revenue = 0
    for x in range(n):
        vw = list(map(int, input().split()))
        weightvaluedict[vw[1]] = vw[0]/vw[1]
    weightvaluedict = {k: v for k, v in sorted(
        weightvaluedict.items(), key=lambda item: item[1], reverse=True)}
    for k, v in weightvaluedict.items():
        if weight == 0:
            break
        if k < weight:
            revenue += k * v
            weight -= k
        else:
            revenue += weight * v
            weight = 0
    return revenue

I'm confused if it is an optimal solution or not. I searched online and most of the tutorial used 2D array. I wanted to ensure my code's optimality. Suggestions are welcome.

Thanks. And also this is my first question on the Code review platform so please forgive me if I did anything wrong.

I have implemented a solution for the knapsack problem white fraction is allowed. I've used the dictionary data structure where I used weight as a key and value per weight as a value. After that, I sorted the dictionary by value and filled the knapsack until there's no space left.

Here is my python code -

def calculateKnapsack():
    userinputNW = list(map(int, input().split()))
    n = userinputNW[0]
    weight = userinputNW[1]
    weightvaluedict = {}
    revenue = 0
    for x in range(n):
        vw = list(map(int, input().split()))
        weightvaluedict[vw[1]] = vw[0]/vw[1]
    weightvaluedict = {k: v for k, v in sorted(
        weightvaluedict.items(), key=lambda item: item[1], reverse=True)}
    for k, v in weightvaluedict.items():
        if weight == 0:
            break
        if k < weight:
            revenue += k * v
            weight -= k
        else:
            revenue += weight * v
            weight = 0
    return revenue

I'm confused if it is an optimal solution or not. I searched online and most of the tutorial used 2D array. I wanted to ensure my code's optimality. Suggestions are welcome.

Thanks. And also this is my first question on the Code review platform so please forgive me if I did anything wrong.

I have implemented a solution for the knapsack problem while breaking an item is allowed. I've used the dictionary data structure where I used weight as a key and value per weight as a value. After that, I sorted the dictionary by value and filled the knapsack until there's no space left.

Here is my python code -

def calculateKnapsack():
    userinputNW = list(map(int, input().split()))
    n = userinputNW[0]
    weight = userinputNW[1]
    weightvaluedict = {}
    revenue = 0
    for x in range(n):
        vw = list(map(int, input().split()))
        weightvaluedict[vw[1]] = vw[0]/vw[1]
    weightvaluedict = {k: v for k, v in sorted(
        weightvaluedict.items(), key=lambda item: item[1], reverse=True)}
    for k, v in weightvaluedict.items():
        if weight == 0:
            break
        if k < weight:
            revenue += k * v
            weight -= k
        else:
            revenue += weight * v
            weight = 0
    return revenue

I'm confused if it is an optimal solution or not. I searched online and most of the tutorial used 2D array. I wanted to ensure my code's optimality. Suggestions are welcome.

Thanks. And also this is my first question on the Code review platform so please forgive me if I did anything wrong.

Source Link

Is this an optimal implementation for the knapsack algorithm?

I have implemented a solution for the knapsack problem white fraction is allowed. I've used the dictionary data structure where I used weight as a key and value per weight as a value. After that, I sorted the dictionary by value and filled the knapsack until there's no space left.

Here is my python code -

def calculateKnapsack():
    userinputNW = list(map(int, input().split()))
    n = userinputNW[0]
    weight = userinputNW[1]
    weightvaluedict = {}
    revenue = 0
    for x in range(n):
        vw = list(map(int, input().split()))
        weightvaluedict[vw[1]] = vw[0]/vw[1]
    weightvaluedict = {k: v for k, v in sorted(
        weightvaluedict.items(), key=lambda item: item[1], reverse=True)}
    for k, v in weightvaluedict.items():
        if weight == 0:
            break
        if k < weight:
            revenue += k * v
            weight -= k
        else:
            revenue += weight * v
            weight = 0
    return revenue

I'm confused if it is an optimal solution or not. I searched online and most of the tutorial used 2D array. I wanted to ensure my code's optimality. Suggestions are welcome.

Thanks. And also this is my first question on the Code review platform so please forgive me if I did anything wrong.