Skip to content

Commit f085d44

Browse files
Add proverb exercise (#239)
1 parent fd17e9e commit f085d44

File tree

7 files changed

+142
-0
lines changed

7 files changed

+142
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@
327327
"prerequisites": [],
328328
"difficulty": 3
329329
},
330+
{
331+
"slug": "proverb",
332+
"name": "Proverb",
333+
"uuid": "d521aafa-57a9-4a2f-a32a-8e3efaf6f896",
334+
"practices": [],
335+
"prerequisites": [],
336+
"difficulty": 3
337+
},
330338
{
331339
"slug": "pythagorean-triplet",
332340
"name": "Pythagorean Triplet",
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"proverb.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
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+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module main
2+
3+
import strings
4+
5+
fn recite(inputs []string) string {
6+
if inputs.len == 0 {
7+
return ''
8+
}
9+
10+
mut builder := strings.new_builder(4000)
11+
for i in 1 .. inputs.len {
12+
builder.write_string('For want of a ')
13+
builder.write_string(inputs[i - 1])
14+
builder.write_string(' the ')
15+
builder.write_string(inputs[i])
16+
builder.write_string(' was lost.\n')
17+
}
18+
builder.write_string('And all for the want of a ')
19+
builder.write_string(inputs[0])
20+
builder.write_string('.')
21+
return builder.str()
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[e974b73e-7851-484f-8d6d-92e07fe742fc]
6+
description = "zero pieces"
7+
8+
[2fcd5f5e-8b82-4e74-b51d-df28a5e0faa4]
9+
description = "one piece"
10+
11+
[d9d0a8a1-d933-46e2-aa94-eecf679f4b0e]
12+
description = "two pieces"
13+
14+
[c95ef757-5e94-4f0d-a6cb-d2083f5e5a83]
15+
description = "three pieces"
16+
17+
[433fb91c-35a2-4d41-aeab-4de1e82b2126]
18+
description = "full proverb"
19+
20+
[c1eefa5a-e8d9-41c7-91d4-99fab6d6b9f7]
21+
description = "four pieces modernized"

exercises/practice/proverb/proverb.v

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module main
2+
3+
fn recite(inputs []string) string {
4+
}

exercises/practice/proverb/run_test.v

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module main
2+
3+
fn test_zero_pieces() {
4+
strings := []string{}
5+
expected := ''
6+
assert recite(strings) == expected
7+
}
8+
9+
fn test_one_piece() {
10+
strings := ['nail']
11+
expected := ('And all for the want of a nail.')
12+
assert recite(strings) == expected
13+
}
14+
15+
fn test_two_pieces() {
16+
strings := ['nail', 'shoe']
17+
expected := ('For want of a nail the shoe was lost.
18+
And all for the want of a nail.')
19+
assert recite(strings) == expected
20+
}
21+
22+
fn test_three_pieces() {
23+
strings := ['nail', 'shoe', 'horse']
24+
expected := ('For want of a nail the shoe was lost.
25+
For want of a shoe the horse was lost.
26+
And all for the want of a nail.')
27+
assert recite(strings) == expected
28+
}
29+
30+
fn test_full_proverb() {
31+
strings := ['nail', 'shoe', 'horse', 'rider', 'message', 'battle', 'kingdom']
32+
expected := ('For want of a nail the shoe was lost.
33+
For want of a shoe the horse was lost.
34+
For want of a horse the rider was lost.
35+
For want of a rider the message was lost.
36+
For want of a message the battle was lost.
37+
For want of a battle the kingdom was lost.
38+
And all for the want of a nail.')
39+
assert recite(strings) == expected
40+
}
41+
42+
fn test_four_pieces_modernized() {
43+
strings := ['pin', 'gun', 'soldier', 'battle']
44+
expected := ('For want of a pin the gun was lost.
45+
For want of a gun the soldier was lost.
46+
For want of a soldier the battle was lost.
47+
And all for the want of a pin.')
48+
assert recite(strings) == expected
49+
}

0 commit comments

Comments
 (0)