Skip to content

Commit

Permalink
Add proverb exercise (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Nov 9, 2024
1 parent cae5e6c commit b23d011
Show file tree
Hide file tree
Showing 12 changed files with 3,642 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "proverb",
"name": "Proverb",
"uuid": "ce15ad3b-3423-44e9-9847-f6f73c9415e5",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "queen-attack",
"name": "Queen Attack",
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": [
"keiravillekode"
],
"files": {
"solution": [
"proverb.s"
],
"test": [
"proverb_test.c"
],
"example": [
".meta/example.s"
]
},
"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"
}
63 changes: 63 additions & 0 deletions exercises/practice/proverb/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
.data
for: .string "For want of a "
the: .string " the "
was: .string " was lost.\n"
and: .string "And all for the want of a ";
end: .string ".\n";

.macro APPEND str
adrp x5, \str
add x5, x5, :lo12:\str
bl append
.endm

.text
.globl recite

append:
ldrb w6, [x5], #1 /* load byte, with post-increment */
strb w6, [x0], #1 /* store byte, post-increment */
cbnz w6, append

sub x0, x0, #1
ret

/* extern void recite(char *buffer, const char **strings); */
recite:
mov x7, lr
mov x2, x1
ldr x3, [x2], #8 /* load, post-increment */
cbz x3, .empty

.next_line:
ldr x4, [x2], #8 /* load, post-increment */
cbz x4, .last

APPEND for

mov x5, x3
bl append

APPEND the

mov x5, x4
bl append

APPEND was

mov x3, x4
b .next_line

.last:
APPEND and

ldr x5, [x1]
bl append

APPEND end

mov lr, x7

.empty:
strb wzr, [x0]
ret
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"
36 changes: 36 additions & 0 deletions exercises/practice/proverb/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
5 changes: 5 additions & 0 deletions exercises/practice/proverb/proverb.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl recite

recite:
ret
129 changes: 129 additions & 0 deletions exercises/practice/proverb/proverb_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#include "vendor/unity.h"

#include <stddef.h>

#define BUFFER_SIZE 400

extern void recite(char *buffer, const char **strings);

void setUp(void) {
}

void tearDown(void) {
}

void test_zero_pieces(void) {
const char *strings[] = {
NULL,
};
const char *expected = "";
char buffer[BUFFER_SIZE];

recite(buffer, strings);
TEST_ASSERT_EQUAL_STRING(expected, buffer);
}

void test_one_piece(void) {
TEST_IGNORE();
const char *strings[] = {
"nail",
NULL,
};
const char *expected =
"And all for the want of a nail.\n";
char buffer[BUFFER_SIZE];

recite(buffer, strings);
TEST_ASSERT_EQUAL_STRING(expected, buffer);
}

void test_two_pieces(void) {
TEST_IGNORE();
const char *strings[] = {
"nail",
"shoe",
NULL,
};
const char *expected =
"For want of a nail the shoe was lost.\n"
"And all for the want of a nail.\n";
char buffer[BUFFER_SIZE];

recite(buffer, strings);
TEST_ASSERT_EQUAL_STRING(expected, buffer);
}

void test_three_pieces(void) {
TEST_IGNORE();
const char *strings[] = {
"nail",
"shoe",
"horse",
NULL,
};
const char *expected =
"For want of a nail the shoe was lost.\n"
"For want of a shoe the horse was lost.\n"
"And all for the want of a nail.\n";
char buffer[BUFFER_SIZE];

recite(buffer, strings);
TEST_ASSERT_EQUAL_STRING(expected, buffer);
}

void test_full_proverb(void) {
TEST_IGNORE();
const char *strings[] = {
"nail",
"shoe",
"horse",
"rider",
"message",
"battle",
"kingdom",
NULL,
};
const char *expected =
"For want of a nail the shoe was lost.\n"
"For want of a shoe the horse was lost.\n"
"For want of a horse the rider was lost.\n"
"For want of a rider the message was lost.\n"
"For want of a message the battle was lost.\n"
"For want of a battle the kingdom was lost.\n"
"And all for the want of a nail.\n";
char buffer[BUFFER_SIZE];

recite(buffer, strings);
TEST_ASSERT_EQUAL_STRING(expected, buffer);
}

void test_four_pieces_modernized(void) {
TEST_IGNORE();
const char *strings[] = {
"pin",
"gun",
"soldier",
"battle",
NULL,
};
const char *expected =
"For want of a pin the gun was lost.\n"
"For want of a gun the soldier was lost.\n"
"For want of a soldier the battle was lost.\n"
"And all for the want of a pin.\n";
char buffer[BUFFER_SIZE];

recite(buffer, strings);
TEST_ASSERT_EQUAL_STRING(expected, buffer);
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_zero_pieces);
RUN_TEST(test_one_piece);
RUN_TEST(test_two_pieces);
RUN_TEST(test_three_pieces);
RUN_TEST(test_full_proverb);
RUN_TEST(test_four_pieces_modernized);
return UNITY_END();
}
Loading

0 comments on commit b23d011

Please sign in to comment.