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 strain #206

Merged
merged 4 commits into from
Jul 12, 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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "strain",
"name": "Strain",
"uuid": "af541ed6-4112-4b61-99dd-fd29424ae859",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "high-scores",
"name": "High Scores",
Expand Down
10 changes: 10 additions & 0 deletions exercises/practice/strain/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Instructions append

## Implementaton Notes

The test suite passes a Function class instance to the `keep` and `discard` methods as a [block argument][block-argument].
To successfully pass the tests, you'll need to call that function inside your code.
If you're unfamiliar with how to do this, please consult the [Function class documentation][function-class].

[block-argument]: https://wren.io/functions.html#block-arguments
[function-class]: https://wren.io/modules/core/fn.html
29 changes: 29 additions & 0 deletions exercises/practice/strain/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Instructions

Implement the `keep` and `discard` operation on collections.
Given a collection and a predicate on the collection's elements, `keep` returns a new collection containing those elements where the predicate is true, while `discard` returns a new collection containing those elements where the predicate is false.

For example, given the collection of numbers:

- 1, 2, 3, 4, 5

And the predicate:

- is the number even?

Then your keep operation should produce:

- 2, 4

While your discard operation should produce:

- 1, 3, 5

Note that the union of keep and discard is all the elements.

The functions may be called `keep` and `discard`, or they may need different names in order to not clash with existing functions or concepts in your language.

## Restrictions

Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library!
Solve this one yourself using other basic tools instead.
19 changes: 19 additions & 0 deletions exercises/practice/strain/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"strain.wren"
],
"test": [
"strain.spec.wren"
],
"example": [
".meta/proof.ci.wren"
]
},
"blurb": "Implement the `keep` and `discard` operation on collections.",
"source": "Conversation with James Edward Gray II",
"source_url": "http://graysoftinc.com/"
}
16 changes: 16 additions & 0 deletions exercises/practice/strain/.meta/proof.ci.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Strain {
static keep(list, predicate) {
var kept = []
for (item in list) {
if (predicate.call(item)) {
kept.add(item)
}
}
return kept
}

static discard(list, predicate) {
return keep(list) { |x| !predicate.call(x) }
}
}

52 changes: 52 additions & 0 deletions exercises/practice/strain/.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.

[26af8c32-ba6a-4eb3-aa0a-ebd8f136e003]
description = "keep on empty list returns empty list"

[f535cb4d-e99b-472a-bd52-9fa0ffccf454]
description = "keeps everything"

[950b8e8e-f628-42a8-85e2-9b30f09cde38]
description = "keeps nothing"

[92694259-6e76-470c-af87-156bdf75018a]
description = "keeps first and last"

[938f7867-bfc7-449e-a21b-7b00cbb56994]
description = "keeps neither first nor last"

[8908e351-4437-4d2b-a0f7-770811e48816]
description = "keeps strings"

[2728036b-102a-4f1e-a3ef-eac6160d876a]
description = "keeps lists"

[ef16beb9-8d84-451a-996a-14e80607fce6]
description = "discard on empty list returns empty list"

[2f42f9bc-8e06-4afe-a222-051b5d8cd12a]
description = "discards everything"

[ca990fdd-08c2-4f95-aa50-e0f5e1d6802b]
description = "discards nothing"

[71595dae-d283-48ca-a52b-45fa96819d2f]
description = "discards first and last"

[ae141f79-f86d-4567-b407-919eaca0f3dd]
description = "discards neither first nor last"

[daf25b36-a59f-4f29-bcfe-302eb4e43609]
description = "discards strings"

[a38d03f9-95ad-4459-80d1-48e937e4acaf]
description = "discards lists"
21 changes: 21 additions & 0 deletions exercises/practice/strain/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions exercises/practice/strain/package.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "wren-package" for WrenPackage, Dependency
import "os" for Process

class Package is WrenPackage {
construct new() {}
name { "exercism/strain" }
dependencies {
return [
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git")
]
}
}

Package.new().default()
112 changes: 112 additions & 0 deletions exercises/practice/strain/strain.spec.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import "wren-testie/testie" for Testie, Expect
import "./strain" for Strain

Testie.test("Strain") { |do, skip|
do.describe("keep") {
do.test("keep on empty list returns empty list") {
var actual = Strain.keep([]) { |x| true }
Expect.value(actual).toEqual([])
}

skip.test("keeps everything") {
var actual = Strain.keep([1, 3, 5]) { |x| true }
Expect.value(actual).toEqual([1, 3, 5])
}

skip.test("keeps nothing") {
var actual = Strain.keep([1, 3, 5]) { |x| false }
Expect.value(actual).toEqual([])
}

skip.test("keeps first and last") {
var actual = Strain.keep([1, 2, 3]) { |x| x % 2 == 1 }
Expect.value(actual).toEqual([1, 3])
}

skip.test("keeps neither first nor last") {
var actual = Strain.keep([1, 2, 3]) { |x| x % 2 == 0 }
Expect.value(actual).toEqual([2])
}

skip.test("keeps strings") {
var list = ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"]
var actual = Strain.keep(list) { |x| x.startsWith("z") }
var expected = ["zebra", "zombies", "zealot"]
Expect.value(actual).toEqual(expected)
}

skip.test("keeps lists") {
var list = [
[1, 2, 3],
[5, 5, 5],
[5, 1, 2],
[2, 1, 2],
[1, 5, 2],
[2, 2, 1],
[1, 2, 5]
]
var actual = Strain.keep(list) { |x| x.contains(5) }
var expected = [
[5, 5, 5],
[5, 1, 2],
[1, 5, 2],
[1, 2, 5]
]
Expect.value(actual).toEqual(expected)
}
}

do.describe("discard") {
skip.test("discard on empty list returns empty list") {
var actual = Strain.discard([]) { |x| true }
Expect.value(actual).toEqual([])
}

skip.test("discards everything") {
var actual = Strain.discard([1, 3, 5]) { |x| true }
Expect.value(actual).toEqual([])
}

skip.test("discards nothing") {
var actual = Strain.discard([1, 3, 5]) { |x| false }
Expect.value(actual).toEqual([1, 3, 5])
}

skip.test("discards first and last") {
var actual = Strain.discard([1, 2, 3]) { |x| x % 2 == 1 }
Expect.value(actual).toEqual([2])
}

skip.test("discards neither first nor last") {
var actual = Strain.discard([1, 2, 3]) { |x| x % 2 == 0 }
Expect.value(actual).toEqual([1, 3])
}

skip.test("discards strings") {
var list = ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"]
var actual = Strain.discard(list) { |x| x.startsWith("z") }
var expected = ["apple", "banana", "cherimoya"]
Expect.value(actual).toEqual(expected)
}

skip.test("discards lists") {
var list = [
[1, 2, 3],
[5, 5, 5],
[5, 1, 2],
[2, 1, 2],
[1, 5, 2],
[2, 2, 1],
[1, 2, 5]
]
var actual = Strain.discard(list) { |x| x.contains(5) }
var expected = [
[1, 2, 3],
[2, 1, 2],
[2, 2, 1]
]
Expect.value(actual).toEqual(expected)
}
}
}

9 changes: 9 additions & 0 deletions exercises/practice/strain/strain.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Strain {
static keep(list, predicate) {
Fiber.abort("Remove this statement and implement this function")
}

static discard(list, predicate) {
Fiber.abort("Remove this statement and implement this function")
}
}