Skip to content

Commit

Permalink
Add acronym exercise (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Nov 10, 2024
1 parent b82516f commit 722d6f7
Show file tree
Hide file tree
Showing 12 changed files with 3,568 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "acronym",
"name": "Acronym",
"uuid": "3c556860-d30f-43f3-9304-1d69b4aa707f",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "atbash-cipher",
"name": "Atbash Cipher",
Expand Down
17 changes: 17 additions & 0 deletions exercises/practice/acronym/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Instructions

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).

Punctuation is handled as follows: hyphens are word separators (like whitespace); all other punctuation can be removed from the input.

For example:

| Input | Output |
| ------------------------- | ------ |
| As Soon As Possible | ASAP |
| Liquid-crystal display | LCD |
| Thank George It's Friday! | TGIF |
19 changes: 19 additions & 0 deletions exercises/practice/acronym/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"acronym.s"
],
"test": [
"acronym_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Convert a long phrase to its acronym.",
"source": "Julien Vanier",
"source_url": "https://github.com/monkbroc"
}
32 changes: 32 additions & 0 deletions exercises/practice/acronym/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.text
.globl abbreviate

/* extern void abbreviate(char *buffer, const char *phrase); */
abbreviate:
ldrb w2, [x1], #1 /* load byte, post-increment */
cbz w2, .return

and w3, w2, #-33 /* force upper case */
sub w4, w3, #'A'
cmp w4, #26
bhs abbreviate /* jump if word has not yet started */

strb w3, [x0], #1 /* store byte, post-increment */

.read:
ldrb w2, [x1], #1 /* load byte, post-increment */
cbz w2, .return

cmp w2, #'\''
beq .read /* if an apostrophe, keep reading current word */

and w3, w2, #-33 /* force upper case */
sub w4, w3, #'A'
cmp w4, #26
blo .read /* if a letter, keep reading current word */

b abbreviate /* search for the start of the next word */

.return:
strb wzr, [x0] /* store null terminator */
ret
37 changes: 37 additions & 0 deletions exercises/practice/acronym/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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.

[1e22cceb-c5e4-4562-9afe-aef07ad1eaf4]
description = "basic"

[79ae3889-a5c0-4b01-baf0-232d31180c08]
description = "lowercase words"

[ec7000a7-3931-4a17-890e-33ca2073a548]
description = "punctuation"

[32dd261c-0c92-469a-9c5c-b192e94a63b0]
description = "all caps word"

[ae2ac9fa-a606-4d05-8244-3bcc4659c1d4]
description = "punctuation without whitespace"

[0e4b1e7c-1a6d-48fb-81a7-bf65eb9e69f9]
description = "very long abbreviation"

[6a078f49-c68d-4b7b-89af-33a1a98c28cc]
description = "consecutive delimiters"

[5118b4b1-4572-434c-8d57-5b762e57973e]
description = "apostrophes"

[adc12eab-ec2d-414f-b48c-66a4fc06cdef]
description = "underscore emphasis"
36 changes: 36 additions & 0 deletions exercises/practice/acronym/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/acronym/acronym.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl abbreviate

abbreviate:
ret
96 changes: 96 additions & 0 deletions exercises/practice/acronym/acronym_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "vendor/unity.h"

#define BUFFER_SIZE 40

extern void abbreviate(char *buffer, const char *phrase);

void setUp(void) {
}

void tearDown(void) {
}

void test_basic(void) {
char buffer[BUFFER_SIZE];

abbreviate(buffer, "Portable Network Graphics");
TEST_ASSERT_EQUAL_STRING("PNG", buffer);
}

void test_lowercase_words(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

abbreviate(buffer, "Ruby on Rails");
TEST_ASSERT_EQUAL_STRING("ROR", buffer);
}

void test_punctuation(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

abbreviate(buffer, "First In, First Out");
TEST_ASSERT_EQUAL_STRING("FIFO", buffer);
}

void test_all_caps_word(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

abbreviate(buffer, "GNU Image Manipulation Program");
TEST_ASSERT_EQUAL_STRING("GIMP", buffer);
}

void test_punctuation_without_whitespace(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

abbreviate(buffer, "Complementary metal-oxide semiconductor");
TEST_ASSERT_EQUAL_STRING("CMOS", buffer);
}

void test_very_long_abbreviation(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

abbreviate(buffer, "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me");
TEST_ASSERT_EQUAL_STRING("ROTFLSHTMDCOALM", buffer);
}

void test_consecutive_delimiters(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

abbreviate(buffer, "Something - I made up from thin air");
TEST_ASSERT_EQUAL_STRING("SIMUFTA", buffer);
}

void test_apostrophes(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

abbreviate(buffer, "Halley's Comet");
TEST_ASSERT_EQUAL_STRING("HC", buffer);
}

void test_underscore_emphasis(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

abbreviate(buffer, "The Road _Not_ Taken");
TEST_ASSERT_EQUAL_STRING("TRNT", buffer);
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_basic);
RUN_TEST(test_lowercase_words);
RUN_TEST(test_punctuation);
RUN_TEST(test_all_caps_word);
RUN_TEST(test_punctuation_without_whitespace);
RUN_TEST(test_very_long_abbreviation);
RUN_TEST(test_consecutive_delimiters);
RUN_TEST(test_apostrophes);
RUN_TEST(test_underscore_emphasis);
return UNITY_END();
}
Loading

0 comments on commit 722d6f7

Please sign in to comment.