Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Knapsack exercise #310

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/generate
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def indent(n, s):


def camelize(s):
return re.sub('[-_](\w)', lambda x: x.group(1).upper(), s)
return re.sub(r'[-_](\w)', lambda x: x.group(1).upper(), s)


def sml_type(value, force=False):
Expand Down
103 changes: 41 additions & 62 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"test_runner": {
"average_run_time": 1
},
"checklist_issue": 8,
"files": {
"solution": [
"%{kebab_slug}.sml"
Expand All @@ -34,43 +33,38 @@
]
},
"exercises": {
"concept": [],
"practice": [
{
"slug": "hello-world",
"name": "Hello World",
"uuid": "a00f98a6-98dd-4c8a-b3d1-adc1d56eef29",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "leap",
"name": "Leap",
"uuid": "622644a8-55c3-498e-ac72-ad6d7d31109d",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "two-fer",
"name": "Two Fer",
"uuid": "a56bad29-684c-47e7-9724-8c7f165a51af",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "scrabble-score",
"name": "Scrabble Score",
"uuid": "cc6e7e8d-84f8-4b85-8f5e-7a68b3b34b1f",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"topics": []
"difficulty": 3
},
{
"slug": "space-age",
Expand Down Expand Up @@ -100,8 +94,7 @@
"uuid": "d17ab3c6-82a1-413e-9018-8775ec9ea498",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "collatz-conjecture",
Expand All @@ -120,26 +113,23 @@
"uuid": "38a13f45-9e80-4322-b896-d2cf06add3f9",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"topics": []
"difficulty": 3
},
{
"slug": "kindergarten-garden",
"name": "Kindergarten Garden",
"uuid": "7d74175b-a2c6-4e77-a908-ebcbcf1e29b6",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"topics": []
"difficulty": 3
},
{
"slug": "secret-handshake",
"name": "Secret Handshake",
"uuid": "55048224-25dc-4c91-b4f4-1ea1bacdd87f",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"topics": []
"difficulty": 3
},
{
"slug": "matching-brackets",
Expand Down Expand Up @@ -188,8 +178,7 @@
"uuid": "7be1122c-33cd-40ed-a70f-c71a6713c47a",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "binary",
Expand All @@ -198,7 +187,6 @@
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": null,
"status": "deprecated"
},
{
Expand All @@ -208,7 +196,6 @@
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": [],
"status": "deprecated"
},
{
Expand All @@ -228,26 +215,23 @@
"uuid": "216334c2-bdb5-481a-a22b-20ac088f20e9",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "atbash-cipher",
"name": "Atbash Cipher",
"uuid": "0f488d4b-89da-4d1f-958b-a7b3171f43d5",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "bob",
"name": "Bob",
"uuid": "4fb40e77-727b-41bc-ac15-b909a26bb917",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "diamond",
Expand Down Expand Up @@ -279,8 +263,7 @@
"uuid": "fb0a030d-33bc-4066-a30a-1b8b02cc42f1",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "wordy",
Expand All @@ -296,8 +279,7 @@
"uuid": "ce5dd1a4-7c90-48e4-b260-d2b62f29fbaf",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "list-ops",
Expand Down Expand Up @@ -327,8 +309,7 @@
"uuid": "5184de5a-4772-4389-aa3c-b1117b601450",
"practices": [],
"prerequisites": [],
"difficulty": 5,
"topics": []
"difficulty": 5
},
{
"slug": "perfect-numbers",
Expand Down Expand Up @@ -366,8 +347,7 @@
"uuid": "24edacd5-6e6d-4d37-8486-25283dce806f",
"practices": [],
"prerequisites": [],
"difficulty": 3,
"topics": []
"difficulty": 3
},
{
"slug": "rotational-cipher",
Expand All @@ -386,8 +366,7 @@
"uuid": "f923bffb-109f-4257-957c-cea8cae2d5c5",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "isogram",
Expand Down Expand Up @@ -441,8 +420,7 @@
"uuid": "76b1e1d1-2360-487c-89be-37f9780540b7",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "resistor-color",
Expand All @@ -458,8 +436,7 @@
"uuid": "db3e09ee-6c93-417f-b0a0-196cdccf4984",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": []
"difficulty": 1
},
{
"slug": "pythagorean-triplet",
Expand All @@ -478,8 +455,7 @@
"uuid": "b4359037-8457-49ea-ac33-44710ada3b4d",
"practices": [],
"prerequisites": [],
"difficulty": 5,
"topics": []
"difficulty": 5
},
{
"slug": "sum-of-multiples",
Expand Down Expand Up @@ -534,8 +510,7 @@
"uuid": "90237a3f-595a-4238-bcef-46d6f84b0945",
"practices": [],
"prerequisites": [],
"difficulty": 2,
"topics": []
"difficulty": 2
},
{
"slug": "gigasecond",
Expand Down Expand Up @@ -566,17 +541,15 @@
"uuid": "a503a4e2-b6eb-4ab0-ac67-8b136157f3dd",
"practices": [],
"prerequisites": [],
"difficulty": 4,
"topics": []
"difficulty": 4
},
{
"slug": "yacht",
"name": "Yacht",
"uuid": "c4a24f88-febd-4955-a08d-67895a3d119f",
"practices": [],
"prerequisites": [],
"difficulty": 4,
"topics": []
"difficulty": 4
},
{
"slug": "triangle",
Expand Down Expand Up @@ -618,16 +591,22 @@
"uuid": "95b2f13e-e59f-477c-905a-8cfa652607b3",
"practices": [],
"prerequisites": [],
"difficulty": 6,
"topics": []
"difficulty": 6
},
{
"slug": "knapsack",
"name": "Knapsack",
"uuid": "4aaa8cbf-a48e-475a-a986-bd2cc3468e67",
"practices": [],
"prerequisites": [],
"difficulty": 5
}
],
"foregone": [
"bank-account",
"parallel-letter-frequency"
]
},
"foregone": [
"bank-account",
"parallel-letter-frequency"
],
Comment on lines -626 to -629
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This key is now gone, but it should be moved under the exercises key.

"concepts": [],
"key_features": [
{
"title": "Algebraic data types",
Expand Down Expand Up @@ -661,15 +640,15 @@
}
],
"tags": [
"execution_mode/interpreted",
"paradigm/functional",
"paradigm/imperative",
"typing/static",
"typing/strong",
"execution_mode/interpreted",
"platform/windows",
"platform/mac",
"platform/linux",
"platform/mac",
"platform/windows",
"runtime/language_specific",
"typing/static",
"typing/strong",
"used_for/financial_systems",
"used_for/scientific_calculations"
]
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/knapsack/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Instructions

Your task is to determine which items to take so that the total value of his selection is maximized, taking into account the knapsack's carrying capacity.

Items will be represented as a list of items.
Each item will have a weight and value.
All values given will be strictly positive.
Bob can take only one of each item.

For example:

```text
Items: [
{ "weight": 5, "value": 10 },
{ "weight": 4, "value": 40 },
{ "weight": 6, "value": 30 },
{ "weight": 4, "value": 50 }
]

Knapsack Maximum Weight: 10
```

For the above, the first item has weight 5 and value 10, the second item has weight 4 and value 40, and so on.
In this example, Bob should take the second and fourth item to maximize his value, which, in this case, is 90.
He cannot get more than 90 as his knapsack has a weight limit of 10.
8 changes: 8 additions & 0 deletions exercises/practice/knapsack/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Introduction

Bob is a thief.
After months of careful planning, he finally manages to crack the security systems of a fancy store.

In front of him are many items, each with a value and weight.
Bob would gladly take all of the items, but his knapsack can only hold so much weight.
Bob has to carefully consider which items to take so that the total value of his selection is maximized.
19 changes: 19 additions & 0 deletions exercises/practice/knapsack/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"kahgoh"
],
"files": {
"solution": [
"knapsack.sml"
],
"test": [
"test.sml"
],
"example": [
".meta/example.sml"
]
},
"blurb": "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.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Knapsack_problem"
}
Loading