Skip to content

knapsack: implement exercise #1736

Closed
Closed

Description

@cmccandless

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

Activity

  1. omer-g commented on Mar 30, 2019

    @omer-g
    Contributor

    Hello, I'd be happy to try and work on this.

  2. omer-g commented on Apr 1, 2019

    @omer-g
    Contributor

    Hello, in the config.json entry, should a new UUID be generated (if so how?) ? Or is there an existing UUID for all knapsack exercises in all tracks? What should the difficulty be? Thanks

  3. cmccandless commented on Apr 1, 2019

    @cmccandless
    ContributorAuthor

    The UUID must be unique for each implementation of the exercise (i.e. this track's knapsack uuid will not be the same as another track's).

    To generate this, use configlet uuid

  4. omer-g commented on Apr 1, 2019

    @omer-g
    Contributor

    What parameters would you suggest for the stub function? I was thinking it may be the clearest to pass a list of weights and a list of values (albeit it may be arguably more idiomatic to pass a list of tuples).

  5. cmccandless commented on Apr 1, 2019

    @cmccandless
    ContributorAuthor

    I think a list of tuples would be best; it better represents that the weights and values are tied together.

  6. AnAccountForReportingBugs commented on Apr 2, 2019

    @AnAccountForReportingBugs
    Contributor

    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?

  7. cmccandless commented on Apr 2, 2019

    @cmccandless
    ContributorAuthor

    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.

  8. added a commit that references this issue on Apr 4, 2019
    f34e328
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions