question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

knapsack: implement exercise

See original GitHub issue

From metadata:

Given a knapsack that can only carry a certain weight, determine which items to put in the knapsack in order to maximize their combined value.

Canonical Data

Contributor Checklist:

  • Clone exercism/problem-specifications to local $SPEC_DIRECTORY
  • Make new directory $PYTHON_TRACK_DIRECTORY/exercises/knapsack
  • In $PYTHON_TRACK_DIRECTORY, run bin/get-configlet
  • Add entry for knapsack in $PYTHON_TRACK_DIRECTORY/config.json (use bin/configlet uuid to generate a new UUID for entry)
  • Run $PYTHON_TRACK_DIRECTORY/bin/configlet generate -p $SPEC_DIRECTORY -O knapsack $PYTHON_TRACK_DIRECTORY to generate the exercise README.md
  • Implement test suite $PYTHON_TRACK_DIRECTORY/exercises/knapsack/knapsack_test.py (use existing exercise ~coins~ change as an example)
  • Create a solution stub $PYTHON_TRACK_DIRECTORY/exercises/knapsack/knapsack.py
  • Implement example solution $PYTHON_TRACK_DIRECTORY/exercises/knapsack/example.py. This solution may be tested using $PYTHON_TRACK_DIRECTORY/test/check-exercises.py knapsack

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
cmccandlesscommented, Apr 2, 2019

When I wrote my implementation, I used a list of dictionaries. I tend to favor mappings for various reasons.

As in {"weight": 2, "value": 3}? That is fine by me.

In addition, they are an allowed json type, so it makes automatic test generation easier.

Nested lists are allowed in JSON; they just have to be homogeneous. That said, I think the dict approach is clearer.

Also, another note from the bug text, but I don’t think coins is an exercise in this track. Perhaps you meant change?

Yes, my mistake. I updated the issue description.

1reaction
AnAccountForReportingBugscommented, Apr 2, 2019

When I wrote my implementation, I used a list of dictionaries. I tend to favor mappings for various reasons. In addition, they are an allowed json type, so it makes automatic test generation easier.

Also, another note from the bug text, but I don’t think coins is an exercise in this track. Perhaps you meant change?

Read more comments on GitHub >

github_iconTop Results From Across the Web

0-1 Knapsack Problem | DP-10
A simple solution is to consider all subsets of items and calculate the total weight and value of all subsets. Consider the only...
Read more >
COMP26120 Lab Exercise 11: The 0/1 Knapsack Problem
A straightforward method for solving any 0/1 Knapsack Problem is to try out all possible ways of packing/leaving out the items. For each...
Read more >
Exercise: Dynamic Programming for the Knapsack Problem
In the second part of the exercise, we want to develop and implement an exact algorithm for the knapsack problem based on the...
Read more >
The Knapsack problem
The way this is optimally solved is using dynamic programming – solving for smaller sets of knapsack problems and then expanding them for...
Read more >
How I used algorithms to solve the knapsack problem for ...
The steps of the algorithm we'll use to solve our knapsack problem are: Sort items by worth, in descending order.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found