Skip to content

Commit b23d011

Browse files
Add proverb exercise (#111)
1 parent cae5e6c commit b23d011

File tree

12 files changed

+3642
-0
lines changed

12 files changed

+3642
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@
234234
"prerequisites": [],
235235
"difficulty": 4
236236
},
237+
{
238+
"slug": "proverb",
239+
"name": "Proverb",
240+
"uuid": "ce15ad3b-3423-44e9-9847-f6f73c9415e5",
241+
"practices": [],
242+
"prerequisites": [],
243+
"difficulty": 4
244+
},
237245
{
238246
"slug": "queen-attack",
239247
"name": "Queen Attack",
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.s"
8+
],
9+
"test": [
10+
"proverb_test.c"
11+
],
12+
"example": [
13+
".meta/example.s"
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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.data
2+
for: .string "For want of a "
3+
the: .string " the "
4+
was: .string " was lost.\n"
5+
and: .string "And all for the want of a ";
6+
end: .string ".\n";
7+
8+
.macro APPEND str
9+
adrp x5, \str
10+
add x5, x5, :lo12:\str
11+
bl append
12+
.endm
13+
14+
.text
15+
.globl recite
16+
17+
append:
18+
ldrb w6, [x5], #1 /* load byte, with post-increment */
19+
strb w6, [x0], #1 /* store byte, post-increment */
20+
cbnz w6, append
21+
22+
sub x0, x0, #1
23+
ret
24+
25+
/* extern void recite(char *buffer, const char **strings); */
26+
recite:
27+
mov x7, lr
28+
mov x2, x1
29+
ldr x3, [x2], #8 /* load, post-increment */
30+
cbz x3, .empty
31+
32+
.next_line:
33+
ldr x4, [x2], #8 /* load, post-increment */
34+
cbz x4, .last
35+
36+
APPEND for
37+
38+
mov x5, x3
39+
bl append
40+
41+
APPEND the
42+
43+
mov x5, x4
44+
bl append
45+
46+
APPEND was
47+
48+
mov x3, x4
49+
b .next_line
50+
51+
.last:
52+
APPEND and
53+
54+
ldr x5, [x1]
55+
bl append
56+
57+
APPEND end
58+
59+
mov lr, x7
60+
61+
.empty:
62+
strb wzr, [x0]
63+
ret
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"

exercises/practice/proverb/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
AS = aarch64-linux-gnu-as
2+
CC = aarch64-linux-gnu-gcc
3+
4+
CFLAGS = -g -Wall -Wextra -pedantic -Werror
5+
LDFLAGS =
6+
7+
ALL_LDFLAGS = -pie -Wl,--fatal-warnings
8+
9+
ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
10+
ALL_LDFLAGS += $(LDFLAGS)
11+
12+
C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
13+
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
14+
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)
15+
16+
CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<
17+
18+
all: tests
19+
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<
20+
21+
tests: $(ALL_OBJS)
22+
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)
23+
24+
%.o: %.s
25+
@$(AS) -o $@ $<
26+
27+
%.o: %.c
28+
@$(CC_CMD)
29+
30+
vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
31+
@$(CC_CMD)
32+
33+
clean:
34+
@rm -f *.o vendor/*.o tests
35+
36+
.PHONY: all clean

exercises/practice/proverb/proverb.s

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.text
2+
.globl recite
3+
4+
recite:
5+
ret
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include "vendor/unity.h"
2+
3+
#include <stddef.h>
4+
5+
#define BUFFER_SIZE 400
6+
7+
extern void recite(char *buffer, const char **strings);
8+
9+
void setUp(void) {
10+
}
11+
12+
void tearDown(void) {
13+
}
14+
15+
void test_zero_pieces(void) {
16+
const char *strings[] = {
17+
NULL,
18+
};
19+
const char *expected = "";
20+
char buffer[BUFFER_SIZE];
21+
22+
recite(buffer, strings);
23+
TEST_ASSERT_EQUAL_STRING(expected, buffer);
24+
}
25+
26+
void test_one_piece(void) {
27+
TEST_IGNORE();
28+
const char *strings[] = {
29+
"nail",
30+
NULL,
31+
};
32+
const char *expected =
33+
"And all for the want of a nail.\n";
34+
char buffer[BUFFER_SIZE];
35+
36+
recite(buffer, strings);
37+
TEST_ASSERT_EQUAL_STRING(expected, buffer);
38+
}
39+
40+
void test_two_pieces(void) {
41+
TEST_IGNORE();
42+
const char *strings[] = {
43+
"nail",
44+
"shoe",
45+
NULL,
46+
};
47+
const char *expected =
48+
"For want of a nail the shoe was lost.\n"
49+
"And all for the want of a nail.\n";
50+
char buffer[BUFFER_SIZE];
51+
52+
recite(buffer, strings);
53+
TEST_ASSERT_EQUAL_STRING(expected, buffer);
54+
}
55+
56+
void test_three_pieces(void) {
57+
TEST_IGNORE();
58+
const char *strings[] = {
59+
"nail",
60+
"shoe",
61+
"horse",
62+
NULL,
63+
};
64+
const char *expected =
65+
"For want of a nail the shoe was lost.\n"
66+
"For want of a shoe the horse was lost.\n"
67+
"And all for the want of a nail.\n";
68+
char buffer[BUFFER_SIZE];
69+
70+
recite(buffer, strings);
71+
TEST_ASSERT_EQUAL_STRING(expected, buffer);
72+
}
73+
74+
void test_full_proverb(void) {
75+
TEST_IGNORE();
76+
const char *strings[] = {
77+
"nail",
78+
"shoe",
79+
"horse",
80+
"rider",
81+
"message",
82+
"battle",
83+
"kingdom",
84+
NULL,
85+
};
86+
const char *expected =
87+
"For want of a nail the shoe was lost.\n"
88+
"For want of a shoe the horse was lost.\n"
89+
"For want of a horse the rider was lost.\n"
90+
"For want of a rider the message was lost.\n"
91+
"For want of a message the battle was lost.\n"
92+
"For want of a battle the kingdom was lost.\n"
93+
"And all for the want of a nail.\n";
94+
char buffer[BUFFER_SIZE];
95+
96+
recite(buffer, strings);
97+
TEST_ASSERT_EQUAL_STRING(expected, buffer);
98+
}
99+
100+
void test_four_pieces_modernized(void) {
101+
TEST_IGNORE();
102+
const char *strings[] = {
103+
"pin",
104+
"gun",
105+
"soldier",
106+
"battle",
107+
NULL,
108+
};
109+
const char *expected =
110+
"For want of a pin the gun was lost.\n"
111+
"For want of a gun the soldier was lost.\n"
112+
"For want of a soldier the battle was lost.\n"
113+
"And all for the want of a pin.\n";
114+
char buffer[BUFFER_SIZE];
115+
116+
recite(buffer, strings);
117+
TEST_ASSERT_EQUAL_STRING(expected, buffer);
118+
}
119+
120+
int main(void) {
121+
UNITY_BEGIN();
122+
RUN_TEST(test_zero_pieces);
123+
RUN_TEST(test_one_piece);
124+
RUN_TEST(test_two_pieces);
125+
RUN_TEST(test_three_pieces);
126+
RUN_TEST(test_full_proverb);
127+
RUN_TEST(test_four_pieces_modernized);
128+
return UNITY_END();
129+
}

0 commit comments

Comments
 (0)