From 63b8cfd835e5e8dec098c77871c66b735bfbcf28 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Thu, 21 Mar 2024 14:48:39 +0100 Subject: [PATCH] Add `proverb` exercise (#391) --- config.json | 8 +++ .../practice/proverb/.docs/instructions.md | 19 +++++++ exercises/practice/proverb/.meta/config.json | 19 +++++++ .../practice/proverb/.meta/proverb.example.pl | 16 ++++++ exercises/practice/proverb/.meta/tests.toml | 28 ++++++++++ exercises/practice/proverb/proverb.pl | 1 + exercises/practice/proverb/proverb_tests.plt | 53 +++++++++++++++++++ 7 files changed, 144 insertions(+) create mode 100644 exercises/practice/proverb/.docs/instructions.md create mode 100644 exercises/practice/proverb/.meta/config.json create mode 100644 exercises/practice/proverb/.meta/proverb.example.pl create mode 100644 exercises/practice/proverb/.meta/tests.toml create mode 100644 exercises/practice/proverb/proverb.pl create mode 100644 exercises/practice/proverb/proverb_tests.plt diff --git a/config.json b/config.json index ae119f23..3c3f4df4 100644 --- a/config.json +++ b/config.json @@ -757,6 +757,14 @@ "practices": [], "prerequisites": [], "difficulty": 1 + }, + { + "slug": "proverb", + "name": "Proverb", + "uuid": "2dec40db-2bc8-4f5e-bfd7-6b872d335af2", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ], "foregone": [ diff --git a/exercises/practice/proverb/.docs/instructions.md b/exercises/practice/proverb/.docs/instructions.md new file mode 100644 index 00000000..f6fb8593 --- /dev/null +++ b/exercises/practice/proverb/.docs/instructions.md @@ -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. diff --git a/exercises/practice/proverb/.meta/config.json b/exercises/practice/proverb/.meta/config.json new file mode 100644 index 00000000..09b07faf --- /dev/null +++ b/exercises/practice/proverb/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "proverb.pl" + ], + "test": [ + "proverb_tests.plt" + ], + "example": [ + ".meta/proverb.example.pl" + ] + }, + "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" +} diff --git a/exercises/practice/proverb/.meta/proverb.example.pl b/exercises/practice/proverb/.meta/proverb.example.pl new file mode 100644 index 00000000..45c2cf49 --- /dev/null +++ b/exercises/practice/proverb/.meta/proverb.example.pl @@ -0,0 +1,16 @@ +pairs([X,Y|Tail], [X-Y|Pairs]) :- pairs([Y|Tail], Pairs), !. +pairs(_, []) :- !. + +buildup_line(Want-Loss, Line) :- + format(string(Line), "For want of a ~w the ~w was lost.", [Want, Loss]). + +final_line(Want, Line) :- + format(string(Line), "And all for the want of a ~w.", [Want]). + +proverb([], []). +proverb(Wants, Lines) :- + [Want|_] = Wants, + pairs(Wants, Pairs), + maplist(buildup_line, Pairs, BuildupLines), + final_line(Want, FinalLine), + append(BuildupLines, [FinalLine], Lines). diff --git a/exercises/practice/proverb/.meta/tests.toml b/exercises/practice/proverb/.meta/tests.toml new file mode 100644 index 00000000..dc92a0c9 --- /dev/null +++ b/exercises/practice/proverb/.meta/tests.toml @@ -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" diff --git a/exercises/practice/proverb/proverb.pl b/exercises/practice/proverb/proverb.pl new file mode 100644 index 00000000..b79a97bc --- /dev/null +++ b/exercises/practice/proverb/proverb.pl @@ -0,0 +1 @@ +proverb(Wants, Lines). diff --git a/exercises/practice/proverb/proverb_tests.plt b/exercises/practice/proverb/proverb_tests.plt new file mode 100644 index 00000000..c114bda3 --- /dev/null +++ b/exercises/practice/proverb/proverb_tests.plt @@ -0,0 +1,53 @@ +pending :- + current_prolog_flag(argv, ['--all'|_]). +pending :- + write('\nA TEST IS PENDING!\n'), + fail. + +:- begin_tests(proverb). + + test(zero_pieces, condition(true)) :- + proverb([], Lines), + Lines == []. + + test(one_piece, condition(pending)) :- + proverb(["nail"], Lines), + Lines == ["And all for the want of a nail."]. + + test(two_pieces, condition(pending)) :- + proverb(["nail", "shoe"], Lines), + Lines == [ + "For want of a nail the shoe was lost.", + "And all for the want of a nail." + ]. + + test(three_pieces, condition(pending)) :- + proverb(["nail", "shoe", "horse"], Lines), + Lines == [ + "For want of a nail the shoe was lost.", + "For want of a shoe the horse was lost.", + "And all for the want of a nail." + ]. + + test(full_proverb, condition(pending)) :- + proverb(["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"], Lines), + Lines == [ + "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." + ]. + + test(four_pieces_modernized, condition(pending)) :- + proverb(["pin", "gun", "soldier", "battle"], Lines), + Lines == [ + "For want of a pin the gun was lost.", + "For want of a gun the soldier was lost.", + "For want of a soldier the battle was lost.", + "And all for the want of a pin." + ]. + +:- end_tests(proverb).