Skip to content

Commit

Permalink
Add run-length-encoding exercise (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Dec 6, 2024
1 parent cad4bff commit de1c181
Show file tree
Hide file tree
Showing 19 changed files with 3,737 additions and 8 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "run-length-encoding",
"name": "Run-Length Encoding",
"uuid": "e2cb9eb9-c1fd-44a9-a7fa-ac8199d1680d",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "sieve",
"name": "Sieve",
Expand Down
1 change: 0 additions & 1 deletion exercises/practice/affine-cipher/affine_cipher_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#define BUFFER_SIZE 80

extern void encode(char *buffer, const char *phrase, unsigned a, unsigned b);

extern void decode(char *buffer, const char *phrase, unsigned a, unsigned b);

void setUp(void) {
Expand Down
1 change: 0 additions & 1 deletion exercises/practice/atbash-cipher/atbash_cipher_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#define BUFFER_SIZE 80

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

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

void setUp(void) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
#include <stdint.h>

extern uint64_t square_of_sum(uint64_t number);

extern uint64_t sum_of_squares(uint64_t number);

extern uint64_t difference_of_squares(uint64_t number);

void setUp(void) {
Expand Down
9 changes: 9 additions & 0 deletions exercises/practice/pig-latin/pig_latin_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ void test_word_beginning_with_q_without_a_following_u(void) {
TEST_ASSERT_EQUAL_STRING("atqay", buffer);
}

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

translate(buffer, "liquid");
TEST_ASSERT_EQUAL_STRING("iquidlay", buffer);
}

void test_word_beginning_with_ch(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];
Expand Down Expand Up @@ -197,6 +205,7 @@ int main(void) {
RUN_TEST(test_word_beginning_with_k);
RUN_TEST(test_word_beginning_with_x);
RUN_TEST(test_word_beginning_with_q_without_a_following_u);
RUN_TEST(test_word_beginning_with_consonant_and_vowel_containing_qu);
RUN_TEST(test_word_beginning_with_ch);
RUN_TEST(test_word_beginning_with_qu);
RUN_TEST(test_word_beginning_with_qu_and_a_preceding_consonant);
Expand Down
20 changes: 20 additions & 0 deletions exercises/practice/run-length-encoding/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Instructions

Implement run-length encoding and decoding.

Run-length encoding (RLE) is a simple form of data compression, where runs (consecutive data elements) are replaced by just one data value and count.

For example we can represent the original 53 characters with only 13.

```text
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB"
```

RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression.

```text
"AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE"
```

For simplicity, you can assume that the unencoded string will only contain the letters A through Z (either lower or upper case) and whitespace.
This way data to be encoded will never contain any numbers and numbers inside data to be decoded always represent the count for the following character.
19 changes: 19 additions & 0 deletions exercises/practice/run-length-encoding/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"run_length_encoding.s"
],
"test": [
"run_length_encoding_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Implement run-length encoding and decoding.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Run-length_encoding"
}
83 changes: 83 additions & 0 deletions exercises/practice/run-length-encoding/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
.text
.globl encode
.globl decode

/* extern void encode(char *buffer, const char *string); */
encode:
mov w10, #10

.encode_next:
mov x2, x1 /* address at start of run */
ldrb w3, [x1], #1 /* read byte, post-increment */
cbz w3, terminate

.encode_read:
ldrb w4, [x1], #1 /* read byte, post-increment */
cmp w4, w3
beq .encode_read

sub x1, x1, #1 /* address of first byte after run */
mov x4, x0
sub x2, x1, x2 /* length of run */
cmp x2, #1
beq .encode_write

.encode_digit:
udiv w5, w2, w10 /* quotient */
msub w6, w5, w10, w2 /* remainder */
mov w2, w5
add w6, w6, #'0'
strb w6, [x0], #1
cbnz w2, .encode_digit

mov x5, x0

.encode_reverse:
sub x5, x5, #1
cmp x5, x4
beq .encode_write /* middle byte of odd length string */

ldrb w6, [x5]
ldrb w7, [x4]
strb w6, [x4], #1 /* store byte, post-increment */
strb w7, [x5]
cmp x5, x4
bne .encode_reverse
/* middle bytes of even length string */

.encode_write:
strb w3, [x0], #1
b .encode_next

terminate:
strb wzr, [x0]
ret

/* extern void decode(char *buffer, const char *string); */
decode:
mov w10, #10

.decode_next:
mov w2, wzr /* count */

.decode_read:
ldrb w3, [x1], #1 /* read byte, post-increment */
cbz w3, terminate

sub w4, w3, #'0'
cmp w4, w10
blo .decode_digit

cmp w2, wzr
cinc w2, w2, eq

.decode_write:
strb w3, [x0], #1 /* write byte, post-increment */
sub w2, w2, #1
cbnz w2, .decode_write

b .decode_next

.decode_digit:
madd w2, w2, w10, w4
b .decode_read
49 changes: 49 additions & 0 deletions exercises/practice/run-length-encoding/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.

[ad53b61b-6ffc-422f-81a6-61f7df92a231]
description = "run-length encode a string -> empty string"

[52012823-b7e6-4277-893c-5b96d42f82de]
description = "run-length encode a string -> single characters only are encoded without count"

[b7868492-7e3a-415f-8da3-d88f51f80409]
description = "run-length encode a string -> string with no single characters"

[859b822b-6e9f-44d6-9c46-6091ee6ae358]
description = "run-length encode a string -> single characters mixed with repeated characters"

[1b34de62-e152-47be-bc88-469746df63b3]
description = "run-length encode a string -> multiple whitespace mixed in string"

[abf176e2-3fbd-40ad-bb2f-2dd6d4df721a]
description = "run-length encode a string -> lowercase characters"

[7ec5c390-f03c-4acf-ac29-5f65861cdeb5]
description = "run-length decode a string -> empty string"

[ad23f455-1ac2-4b0e-87d0-b85b10696098]
description = "run-length decode a string -> single characters only"

[21e37583-5a20-4a0e-826c-3dee2c375f54]
description = "run-length decode a string -> string with no single characters"

[1389ad09-c3a8-4813-9324-99363fba429c]
description = "run-length decode a string -> single characters with repeated characters"

[3f8e3c51-6aca-4670-b86c-a213bf4706b0]
description = "run-length decode a string -> multiple whitespace mixed in string"

[29f721de-9aad-435f-ba37-7662df4fb551]
description = "run-length decode a string -> lowercase string"

[2a762efd-8695-4e04-b0d6-9736899fbc16]
description = "encode and then decode -> encode followed by decode gives original string"
36 changes: 36 additions & 0 deletions exercises/practice/run-length-encoding/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
9 changes: 9 additions & 0 deletions exercises/practice/run-length-encoding/run_length_encoding.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.text
.globl encode
.globl decode

encode:
ret

decode:
ret
Loading

0 comments on commit de1c181

Please sign in to comment.