Skip to content

Commit 9d54fb7

Browse files
Add proverb exercise (#517)
* Add proverb exercise * Apply suggestions from code review --------- Co-authored-by: Ryan Hartlage <[email protected]>
1 parent 3e5c5af commit 9d54fb7

File tree

8 files changed

+160
-0
lines changed

8 files changed

+160
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,14 @@
536536
"strings"
537537
]
538538
},
539+
{
540+
"slug": "proverb",
541+
"name": "Proverb",
542+
"uuid": "fe8aca38-4d7d-4a22-bae2-8e16e60c972e",
543+
"practices": [],
544+
"prerequisites": [],
545+
"difficulty": 3
546+
},
539547
{
540548
"slug": "pythagorean-triplet",
541549
"name": "Pythagorean Triplet",

exercises/practice/proverb/.busted

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Instructions
2+
3+
For want of a horseshoe nail, a kingdom was lost, or so the saying goes.
4+
5+
Given a list of inputs, generate the relevant proverb.
6+
For example, given the list `["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]`, you will output the full text of this proverbial rhyme:
7+
8+
```text
9+
For want of a nail the shoe was lost.
10+
For want of a shoe the horse was lost.
11+
For want of a horse the rider was lost.
12+
For want of a rider the message was lost.
13+
For want of a message the battle was lost.
14+
For want of a battle the kingdom was lost.
15+
And all for the want of a nail.
16+
```
17+
18+
Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content.
19+
No line of the output text should be a static, unchanging string; all should vary according to the input given.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"proverb.lua"
8+
],
9+
"test": [
10+
"proverb_spec.lua"
11+
],
12+
"example": [
13+
".meta/example.lua"
14+
]
15+
},
16+
"blurb": "For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Output the full text of this proverbial rhyme.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/For_Want_of_a_Nail"
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local Proverb = {}
2+
3+
function Proverb.recite(strings)
4+
if #strings == 0 then
5+
return ''
6+
end
7+
8+
local lines = {}
9+
local template = "For want of a %s the %s was lost.\n"
10+
for index = 2, #strings do
11+
table.insert(lines, template:format(strings[index - 1], strings[index]))
12+
end
13+
14+
template = "And all for the want of a %s.\n"
15+
table.insert(lines, template:format(strings[1]))
16+
return table.concat(lines)
17+
end
18+
19+
return Proverb
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[e974b73e-7851-484f-8d6d-92e07fe742fc]
13+
description = "zero pieces"
14+
15+
[2fcd5f5e-8b82-4e74-b51d-df28a5e0faa4]
16+
description = "one piece"
17+
18+
[d9d0a8a1-d933-46e2-aa94-eecf679f4b0e]
19+
description = "two pieces"
20+
21+
[c95ef757-5e94-4f0d-a6cb-d2083f5e5a83]
22+
description = "three pieces"
23+
24+
[433fb91c-35a2-4d41-aeab-4de1e82b2126]
25+
description = "full proverb"
26+
27+
[c1eefa5a-e8d9-41c7-91d4-99fab6d6b9f7]
28+
description = "four pieces modernized"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local Proverb = {}
2+
3+
function Proverb.recite(strings)
4+
end
5+
6+
return Proverb
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
local Proverb = require 'proverb'
2+
3+
describe('proverb', function()
4+
it('zero pieces', function()
5+
local strings = {}
6+
local expected = table.concat({})
7+
assert.equal(expected, Proverb.recite(strings))
8+
end)
9+
10+
it('one piece', function()
11+
local strings = { 'nail' }
12+
local expected = table.concat({ "And all for the want of a nail.\n" })
13+
assert.equal(expected, Proverb.recite(strings))
14+
end)
15+
16+
it('two pieces', function()
17+
local strings = { 'nail', 'shoe' }
18+
local expected = table.concat({ "For want of a nail the shoe was lost.\n", "And all for the want of a nail.\n" })
19+
assert.equal(expected, Proverb.recite(strings))
20+
end)
21+
22+
it('three pieces', function()
23+
local strings = { 'nail', 'shoe', 'horse' }
24+
local expected = table.concat({
25+
"For want of a nail the shoe was lost.\n",
26+
"For want of a shoe the horse was lost.\n",
27+
"And all for the want of a nail.\n"
28+
})
29+
assert.equal(expected, Proverb.recite(strings))
30+
end)
31+
32+
it('full proverb', function()
33+
local strings = { 'nail', 'shoe', 'horse', 'rider', 'message', 'battle', 'kingdom' }
34+
local expected = table.concat({
35+
"For want of a nail the shoe was lost.\n",
36+
"For want of a shoe the horse was lost.\n",
37+
"For want of a horse the rider was lost.\n",
38+
"For want of a rider the message was lost.\n",
39+
"For want of a message the battle was lost.\n",
40+
"For want of a battle the kingdom was lost.\n",
41+
"And all for the want of a nail.\n"
42+
})
43+
assert.equal(expected, Proverb.recite(strings))
44+
end)
45+
46+
it('four pieces modernized', function()
47+
local strings = { 'pin', 'gun', 'soldier', 'battle' }
48+
local expected = table.concat({
49+
"For want of a pin the gun was lost.\n",
50+
"For want of a gun the soldier was lost.\n",
51+
"For want of a soldier the battle was lost.\n",
52+
"And all for the want of a pin.\n"
53+
})
54+
assert.equal(expected, Proverb.recite(strings))
55+
end)
56+
end)

0 commit comments

Comments
 (0)