Skip to content

Commit

Permalink
Merge branch 'main' into zebra-puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Oct 16, 2024
2 parents 2eb5c80 + 8458e31 commit c6dfa8a
Show file tree
Hide file tree
Showing 26 changed files with 946 additions and 0 deletions.
32 changes: 32 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "knapsack",
"name": "Knapsack",
"uuid": "cfd8e973-fd62-4ffe-8e16-faa5e34a6340",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "high-scores",
"name": "High Scores",
Expand Down Expand Up @@ -784,6 +792,30 @@
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "twelve-days",
"name": "Twelve Days",
"uuid": "da138984-77d2-49a9-a553-11d2448b7bb2",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "house",
"name": "House",
"uuid": "c08f6841-3b9a-409c-81c7-636a69e84643",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "run-length-encoding",
"name": "Run-Length Encoding",
"uuid": "e39b5e8b-cd91-4adf-bb1e-015367ef4f8c",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
Expand Down
105 changes: 105 additions & 0 deletions exercises/practice/house/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Instructions

Recite the nursery rhyme 'This is the House that Jack Built'.

> [The] process of placing a phrase of clause within another phrase of clause is called embedding.
> It is through the processes of recursion and embedding that we are able to take a finite number of forms (words and phrases) and construct an infinite number of expressions.
> Furthermore, embedding also allows us to construct an infinitely long structure, in theory anyway.
- [papyr.com][papyr]

The nursery rhyme reads as follows:

```text
This is the house that Jack built.
This is the malt
that lay in the house that Jack built.
This is the rat
that ate the malt
that lay in the house that Jack built.
This is the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the horse and the hound and the horn
that belonged to the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
```

[papyr]: https://papyr.com/hypertextbooks/grammar/ph_noun.htm
19 changes: 19 additions & 0 deletions exercises/practice/house/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"house.R"
],
"test": [
"test_house.R"
],
"example": [
".meta/example.R"
]
},
"blurb": "Output the nursery rhyme 'This is the House that Jack Built'.",
"source": "British nursery rhyme",
"source_url": "https://en.wikipedia.org/wiki/This_Is_The_House_That_Jack_Built"
}
23 changes: 23 additions & 0 deletions exercises/practice/house/.meta/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
subjects <- c(
"house that Jack built",
"malt that lay in",
"rat that ate",
"cat that killed",
"dog that worried",
"cow with the crumpled horn that tossed",
"maiden all forlorn that milked",
"man all tattered and torn that kissed",
"priest all shaven and shorn that married",
"rooster that crowed in the morn that woke",
"farmer sowing his corn that kept",
"horse and the hound and the horn that belonged to"
)

verse <- function(number) {
verse_subjects <- paste0(subjects[number:1], collapse = " the ")
paste("This is the ", verse_subjects, ".", sep = "")
}

recite <- function(start_verse, end_verse) {
sapply(start_verse:end_verse, verse)
}
52 changes: 52 additions & 0 deletions exercises/practice/house/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[28a540ff-f765-4348-9d57-ae33f25f41f2]
description = "verse one - the house that jack built"

[ebc825ac-6e2b-4a5e-9afd-95732191c8da]
description = "verse two - the malt that lay"

[1ed8bb0f-edb8-4bd1-b6d4-b64754fe4a60]
description = "verse three - the rat that ate"

[64b0954e-8b7d-4d14-aad0-d3f6ce297a30]
description = "verse four - the cat that killed"

[1e8d56bc-fe31-424d-9084-61e6111d2c82]
description = "verse five - the dog that worried"

[6312dc6f-ab0a-40c9-8a55-8d4e582beac4]
description = "verse six - the cow with the crumpled horn"

[68f76d18-6e19-4692-819c-5ff6a7f92feb]
description = "verse seven - the maiden all forlorn"

[73872564-2004-4071-b51d-2e4326096747]
description = "verse eight - the man all tattered and torn"

[0d53d743-66cb-4351-a173-82702f3338c9]
description = "verse nine - the priest all shaven and shorn"

[452f24dc-8fd7-4a82-be1a-3b4839cfeb41]
description = "verse 10 - the rooster that crowed in the morn"

[97176f20-2dd3-4646-ac72-cffced91ea26]
description = "verse 11 - the farmer sowing his corn"

[09824c29-6aad-4dcd-ac98-f61374a6a8b7]
description = "verse 12 - the horse and the hound and the horn"

[d2b980d3-7851-49e1-97ab-1524515ec200]
description = "multiple verses"

[0311d1d0-e085-4f23-8ae7-92406fb3e803]
description = "full rhyme"
3 changes: 3 additions & 0 deletions exercises/practice/house/house.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
recite <- function(start_verse, end_verse) {

}
93 changes: 93 additions & 0 deletions exercises/practice/house/test_house.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
source("./house.R")
library(testthat)

test_that("Verse one - the house that jack built", {
expected <- c("This is the house that Jack built.")
expect_equal(recite(1, 1), expected)
})

test_that("Verse two - the malt that lay", {
expected <- c("This is the malt that lay in the house that Jack built.")
expect_equal(recite(2, 2), expected)
})

test_that("Verse three - the rat that ate", {
expected <- c("This is the rat that ate the malt that lay in the house that Jack built.") # nolint
expect_equal(recite(3, 3), expected)
})

test_that("Verse four - the cat that killed", {
expected <- c("This is the cat that killed the rat that ate the malt that lay in the house that Jack built.") # nolint
expect_equal(recite(4, 4), expected)
})

test_that("Verse five - the dog that worried", {
expected <- c("This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.") # nolint
expect_equal(recite(5, 5), expected)
})

test_that("Verse six - the cow with the crumpled horn", {
expected <- c("This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.") # nolint
expect_equal(recite(6, 6), expected)
})

test_that("Verse seven - the maiden all forlorn", {
expected <- c("This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.") # nolint
expect_equal(recite(7, 7), expected)
})

test_that("Verse eight - the man all tattered and torn", {
expected <- c("This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.") # nolint
expect_equal(recite(8, 8), expected)
})

test_that("Verse nine - the priest all shaven and shorn", {
expected <- c("This is the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.") # nolint
expect_equal(recite(9, 9), expected)
})

test_that("Verse 10 - the rooster that crowed in the morn", {
expected <- c("This is the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.") # nolint
expect_equal(recite(10, 10), expected)
})

test_that("Verse 11 - the farmer sowing his corn", {
expected <- c("This is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.") # nolint
expect_equal(recite(11, 11), expected)
})

test_that("Verse 12 - the horse and the hound and the horn", {
expected <- c("This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.") # nolint
expect_equal(recite(12, 12), expected)
})

test_that("Multiple verses", {
expected <-
c(
"This is the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built." # nolint
)
expect_equal(recite(4, 8), expected)
})

test_that("Full rhyme", {
expected <-
c(
"This is the house that Jack built.",
"This is the malt that lay in the house that Jack built.",
"This is the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built.", # nolint
"This is the horse and the hound and the horn that belonged to the farmer sowing his corn that kept the rooster that crowed in the morn that woke the priest all shaven and shorn that married the man all tattered and torn that kissed the maiden all forlorn that milked the cow with the crumpled horn that tossed the dog that worried the cat that killed the rat that ate the malt that lay in the house that Jack built." # nolint
)
expect_equal(recite(1, 12), expected)
})
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.
Loading

0 comments on commit c6dfa8a

Please sign in to comment.