From a3f55f3afad70c8686f8240bb32a1543d412eeeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 24 Sep 2024 07:20:10 -0700 Subject: [PATCH] Add `strain` (#589) * Add `strain` * Reapply formatting * Swap stub and example --- config.json | 8 + .../practice/strain/.docs/instructions.md | 29 ++++ exercises/practice/strain/.meta/config.json | 19 +++ .../practice/strain/.meta/lib/example.dart | 17 ++ exercises/practice/strain/.meta/tests.toml | 52 ++++++ exercises/practice/strain/lib/strain.dart | 3 + exercises/practice/strain/pubspec.yaml | 5 + .../practice/strain/test/strain_test.dart | 149 ++++++++++++++++++ 8 files changed, 282 insertions(+) create mode 100644 exercises/practice/strain/.docs/instructions.md create mode 100644 exercises/practice/strain/.meta/config.json create mode 100644 exercises/practice/strain/.meta/lib/example.dart create mode 100644 exercises/practice/strain/.meta/tests.toml create mode 100644 exercises/practice/strain/lib/strain.dart create mode 100644 exercises/practice/strain/pubspec.yaml create mode 100644 exercises/practice/strain/test/strain_test.dart diff --git a/config.json b/config.json index b56151c7..ea8f340b 100644 --- a/config.json +++ b/config.json @@ -301,6 +301,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "strain", + "name": "Strain", + "uuid": "3c03a3f1-972e-44d0-a558-43480d862021", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "sum-of-multiples", "name": "Sum of Multiples", diff --git a/exercises/practice/strain/.docs/instructions.md b/exercises/practice/strain/.docs/instructions.md new file mode 100644 index 00000000..3469ae65 --- /dev/null +++ b/exercises/practice/strain/.docs/instructions.md @@ -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. diff --git a/exercises/practice/strain/.meta/config.json b/exercises/practice/strain/.meta/config.json new file mode 100644 index 00000000..1bae3800 --- /dev/null +++ b/exercises/practice/strain/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "lib/strain.dart" + ], + "test": [ + "test/strain_test.dart" + ], + "example": [ + ".meta/lib/example.dart" + ] + }, + "blurb": "Implement the `keep` and `discard` operation on collections.", + "source": "Conversation with James Edward Gray II", + "source_url": "http://graysoftinc.com/" +} diff --git a/exercises/practice/strain/.meta/lib/example.dart b/exercises/practice/strain/.meta/lib/example.dart new file mode 100644 index 00000000..2d00c9f1 --- /dev/null +++ b/exercises/practice/strain/.meta/lib/example.dart @@ -0,0 +1,17 @@ +class Strain { + List keep(List list, bool Function(T) predicate) { + final result = []; + + for (final item in list) { + if (predicate(item)) { + result.add(item); + } + } + + return result; + } + + List discard(List list, bool Function(T) predicate) { + return keep(list, (item) => !predicate(item)); + } +} diff --git a/exercises/practice/strain/.meta/tests.toml b/exercises/practice/strain/.meta/tests.toml new file mode 100644 index 00000000..3a617b4a --- /dev/null +++ b/exercises/practice/strain/.meta/tests.toml @@ -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" diff --git a/exercises/practice/strain/lib/strain.dart b/exercises/practice/strain/lib/strain.dart new file mode 100644 index 00000000..ddefef35 --- /dev/null +++ b/exercises/practice/strain/lib/strain.dart @@ -0,0 +1,3 @@ +class Strain { + // your code here... +} diff --git a/exercises/practice/strain/pubspec.yaml b/exercises/practice/strain/pubspec.yaml new file mode 100644 index 00000000..3674ddd4 --- /dev/null +++ b/exercises/practice/strain/pubspec.yaml @@ -0,0 +1,5 @@ +name: 'strain' +environment: + sdk: '>=3.2.0 <4.0.0' +dev_dependencies: + test: '<2.0.0' diff --git a/exercises/practice/strain/test/strain_test.dart b/exercises/practice/strain/test/strain_test.dart new file mode 100644 index 00000000..157d4c24 --- /dev/null +++ b/exercises/practice/strain/test/strain_test.dart @@ -0,0 +1,149 @@ +import 'package:strain/strain.dart'; +import 'package:test/test.dart'; + +void main() { + final strain = Strain(); + + group('Strain', () { + group('Keep', () { + test('keep on empty list returns empty list', () { + final values = []; + final fn = (x) => true; + final result = strain.keep(values, fn); + final expected = []; + expect(result, equals(expected)); + }, skip: false); + + test('keeps everything', () { + final values = [1, 3, 5]; + final fn = (x) => true; + final result = strain.keep(values, fn); + final expected = [1, 3, 5]; + expect(result, equals(expected)); + }, skip: false); + + test('keeps nothing', () { + final values = [1, 3, 5]; + final fn = (x) => false; + final result = strain.keep(values, fn); + final expected = []; + expect(result, equals(expected)); + }, skip: false); + + test('keeps first and last', () { + final values = [1, 2, 3]; + final fn = (x) => x % 2 == 1; + final result = strain.keep(values, fn); + final expected = [1, 3]; + expect(result, equals(expected)); + }, skip: false); + + test('keeps neither first nor last', () { + final values = [1, 2, 3]; + final fn = (x) => x % 2 == 0; + final result = strain.keep(values, fn); + final expected = [2]; + expect(result, equals(expected)); + }, skip: false); + + test('keeps strings', () { + final values = ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"]; + final fn = (String x) => x.startsWith('z'); + final result = strain.keep(values, fn); + final expected = ["zebra", "zombies", "zealot"]; + expect(result, equals(expected)); + }, skip: false); + + test('keeps lists', () { + final values = [ + [1, 2, 3], + [5, 5, 5], + [5, 1, 2], + [2, 1, 2], + [1, 5, 2], + [2, 2, 1], + [1, 2, 5] + ]; + final fn = (List x) => x.contains(5); + final result = strain.keep(values, fn); + final expected = [ + [5, 5, 5], + [5, 1, 2], + [1, 5, 2], + [1, 2, 5] + ]; + expect(result, equals(expected)); + }, skip: false); + }); + + group('Discard', () { + test('discard on empty list returns empty list', () { + final values = []; + final fn = (x) => true; + final result = strain.discard(values, fn); + final expected = []; + expect(result, equals(expected)); + }, skip: false); + + test('discards everything', () { + final values = [1, 3, 5]; + final fn = (x) => true; + final result = strain.discard(values, fn); + final expected = []; + expect(result, equals(expected)); + }, skip: false); + + test('discards nothing', () { + final values = [1, 3, 5]; + final fn = (x) => false; + final result = strain.discard(values, fn); + final expected = [1, 3, 5]; + expect(result, equals(expected)); + }, skip: false); + + test('discards first and last', () { + final values = [1, 2, 3]; + final fn = (x) => x % 2 == 1; + final result = strain.discard(values, fn); + final expected = [2]; + expect(result, equals(expected)); + }, skip: false); + + test('discards neither first nor last', () { + final values = [1, 2, 3]; + final fn = (x) => x % 2 == 0; + final result = strain.discard(values, fn); + final expected = [1, 3]; + expect(result, equals(expected)); + }, skip: false); + + test('discards strings', () { + final values = ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"]; + final fn = (String x) => x.startsWith('z'); + final result = strain.discard(values, fn); + final expected = ["apple", "banana", "cherimoya"]; + expect(result, equals(expected)); + }, skip: false); + + test('discards lists', () { + final values = [ + [1, 2, 3], + [5, 5, 5], + [5, 1, 2], + [2, 1, 2], + [1, 5, 2], + [2, 2, 1], + [1, 2, 5] + ]; + final fn = (List x) => x.contains(5); + final result = strain.discard(values, fn); + final expected = [ + [1, 2, 3], + [2, 1, 2], + [2, 2, 1] + ]; + expect(result, equals(expected)); + }, skip: false); + }); + }); +}