knapsack: implement exercise
See original GitHub issueFrom 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.
Contributor Checklist:
- Clone exercism/problem-specifications to local $
SPEC_DIRECTORY
- Make new directory
$PYTHON_TRACK_DIRECTORY/exercises/knapsack
- In
$PYTHON_TRACK_DIRECTORY
, runbin/get-configlet
- Add entry for
knapsack
in$PYTHON_TRACK_DIRECTORY/config.json
(usebin/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:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As in
{"weight": 2, "value": 3}
? That is fine by me.Nested lists are allowed in JSON; they just have to be homogeneous. That said, I think the dict approach is clearer.
Yes, my mistake. I updated the issue description.
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 meantchange
?