Skip to content

Commit

Permalink
Add proverb
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Sep 25, 2024
1 parent a3f55f3 commit 4937a57
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@
"math"
]
},
{
"slug": "proverb",
"name": "Proverb",
"uuid": "c0c9c922-e1c7-4971-ac7f-efac2a25a63a",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "grains",
"name": "Grains",
Expand Down
19 changes: 19 additions & 0 deletions exercises/practice/proverb/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Instructions

For want of a horseshoe nail, a kingdom was lost, or so the saying goes.

Given a list of inputs, generate the relevant proverb.
For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme:

```text
For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a nail.
```

Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content.
No line of the output text should be a static, unchanging string; all should vary according to the input given.
19 changes: 19 additions & 0 deletions exercises/practice/proverb/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"lib/proverb.dart"
],
"test": [
"test/proverb_test.dart"
],
"example": [
".meta/lib/example.dart"
]
},
"blurb": "For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Output the full text of this proverbial rhyme.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/For_Want_of_a_Nail"
}
15 changes: 15 additions & 0 deletions exercises/practice/proverb/.meta/lib/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Proverb {
String recite(List<String> pieces) {
if (pieces.isEmpty) {
return '';
}

final List<String> results = [];
for (int i = 0; i < pieces.length - 1; i++) {
results.add('For want of a ${pieces[i]} the ${pieces[i + 1]} was lost.');
}
results.add('And all for the want of a ${pieces.first}.');

return results.join('\n');
}
}
28 changes: 28 additions & 0 deletions exercises/practice/proverb/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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.

[e974b73e-7851-484f-8d6d-92e07fe742fc]
description = "zero pieces"

[2fcd5f5e-8b82-4e74-b51d-df28a5e0faa4]
description = "one piece"

[d9d0a8a1-d933-46e2-aa94-eecf679f4b0e]
description = "two pieces"

[c95ef757-5e94-4f0d-a6cb-d2083f5e5a83]
description = "three pieces"

[433fb91c-35a2-4d41-aeab-4de1e82b2126]
description = "full proverb"

[c1eefa5a-e8d9-41c7-91d4-99fab6d6b9f7]
description = "four pieces modernized"
3 changes: 3 additions & 0 deletions exercises/practice/proverb/lib/proverb.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Proverb {
// Put your code here
}
5 changes: 5 additions & 0 deletions exercises/practice/proverb/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'proverb'
environment:
sdk: '>=3.2.0 <4.0.0'
dev_dependencies:
test: '<2.0.0'
53 changes: 53 additions & 0 deletions exercises/practice/proverb/test/proverb_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:proverb/proverb.dart';
import 'package:test/test.dart';

void main() {
final proverb = Proverb();

group('Proverb', () {
test('zero pieces', () {
List<String> pieces = [];
final result = proverb.recite(pieces);
final expected = "";
expect(result, equals(expected));
}, skip: false);

test('one piece', () {
final pieces = ["nail"];
final result = proverb.recite(pieces);
final expected = "And all for the want of a nail.";
expect(result, equals(expected));
}, skip: true);

test('two pieces', () {
final pieces = ["nail", "shoe"];
final result = proverb.recite(pieces);
final expected = "For want of a nail the shoe was lost.\nAnd all for the want of a nail.";
expect(result, equals(expected));
}, skip: true);

test('three pieces', () {
final pieces = ["nail", "shoe", "horse"];
final result = proverb.recite(pieces);
final expected =
"For want of a nail the shoe was lost.\nFor want of a shoe the horse was lost.\nAnd all for the want of a nail.";
expect(result, equals(expected));
}, skip: true);

test('full proverb', () {
final pieces = ["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"];
final result = proverb.recite(pieces);
final expected =
"For want of a nail the shoe was lost.\nFor want of a shoe the horse was lost.\nFor want of a horse the rider was lost.\nFor want of a rider the message was lost.\nFor want of a message the battle was lost.\nFor want of a battle the kingdom was lost.\nAnd all for the want of a nail.";
expect(result, equals(expected));
}, skip: true);

test('four pieces modernized', () {
final pieces = ["pin", "gun", "soldier", "battle"];
final result = proverb.recite(pieces);
final expected =
"For want of a pin the gun was lost.\nFor want of a gun the soldier was lost.\nFor want of a soldier the battle was lost.\nAnd all for the want of a pin.";
expect(result, equals(expected));
}, skip: true);
});
}

0 comments on commit 4937a57

Please sign in to comment.