Skip to content

Commit

Permalink
Add reverse-string exercise (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Oct 25, 2024
1 parent e6d1842 commit 6a88603
Show file tree
Hide file tree
Showing 13 changed files with 3,530 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "reverse-string",
"name": "Reverse String",
"uuid": "90e4cbd3-c4db-4e87-8002-750a67a015a4",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "two-fer",
"name": "Two-Fer",
Expand Down
9 changes: 9 additions & 0 deletions exercises/practice/reverse-string/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Instructions

Your task is to reverse a given string.

Some examples:

- Turn `"stressed"` into `"desserts"`.
- Turn `"strops"` into `"sports"`.
- Turn `"racecar"` into `"racecar"`.
5 changes: 5 additions & 0 deletions exercises/practice/reverse-string/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Introduction

Reversing strings (reading them from right to left, rather than from left to right) is a surprisingly common task in programming.

For example, in bioinformatics, reversing the sequence of DNA or RNA strings is often important for various analyses, such as finding complementary strands or identifying palindromic sequences that have biological significance.
19 changes: 19 additions & 0 deletions exercises/practice/reverse-string/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"reverse_string.s"
],
"test": [
"reverse_string_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Reverse a given string.",
"source": "Introductory challenge to reverse an input string",
"source_url": "https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb"
}
30 changes: 30 additions & 0 deletions exercises/practice/reverse-string/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.text
.globl reverse

/* extern void reverse(char *str); */
reverse:
mov x1, x0

.scan:
ldrb w2, [x1], #1 /* load byte, post-increment */
cbnz w2, .scan

sub x1, x1, #1
cmp x1, x0
beq .done /* zero length string */

.reverse:
sub x1, x1, #1
cmp x1, x0
beq .done /* middle byte of odd length string */

ldrb w2, [x1]
ldrb w3, [x0]
strb w3, [x1]
strb w2, [x0], #1 /* store byte, post-increment */
cmp x1, x0
bne .reverse
/* middle bytes of even length string */

.done:
ret
40 changes: 40 additions & 0 deletions exercises/practice/reverse-string/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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.

[c3b7d806-dced-49ee-8543-933fd1719b1c]
description = "an empty string"

[01ebf55b-bebb-414e-9dec-06f7bb0bee3c]
description = "a word"

[0f7c07e4-efd1-4aaa-a07a-90b49ce0b746]
description = "a capitalized word"

[71854b9c-f200-4469-9f5c-1e8e5eff5614]
description = "a sentence with punctuation"

[1f8ed2f3-56f3-459b-8f3e-6d8d654a1f6c]
description = "a palindrome"

[b9e7dec1-c6df-40bd-9fa3-cd7ded010c4c]
description = "an even-sized word"

[1bed0f8a-13b0-4bd3-9d59-3d0593326fa2]
description = "wide characters"
include = false

[93d7e1b8-f60f-4f3c-9559-4056e10d2ead]
description = "grapheme cluster with pre-combined form"
include = false

[1028b2c1-6763-4459-8540-2da47ca512d9]
description = "grapheme clusters"
include = false
36 changes: 36 additions & 0 deletions exercises/practice/reverse-string/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/reverse-string/reverse_string.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl reverse

reverse:
ret
61 changes: 61 additions & 0 deletions exercises/practice/reverse-string/reverse_string_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "vendor/unity.h"

extern void reverse(char *str);

void setUp(void) {
}

void tearDown(void) {
}

void test_an_empty_string(void) {
char str[] = "";
reverse(str);
TEST_ASSERT_EQUAL_STRING("", str);
}

void test_a_word(void) {
TEST_IGNORE();
char str[] = "robot";
reverse(str);
TEST_ASSERT_EQUAL_STRING("tobor", str);
}

void test_a_capitalized_word(void) {
TEST_IGNORE();
char str[] = "Ramen";
reverse(str);
TEST_ASSERT_EQUAL_STRING("nemaR", str);
}

void test_a_sentence_with_punctuation(void) {
TEST_IGNORE();
char str[] = "I'm hungry!";
reverse(str);
TEST_ASSERT_EQUAL_STRING("!yrgnuh m'I", str);
}

void test_a_palindrome(void) {
TEST_IGNORE();
char str[] = "racecar";
reverse(str);
TEST_ASSERT_EQUAL_STRING("racecar", str);
}

void test_an_evensized_word(void) {
TEST_IGNORE();
char str[] = "drawer";
reverse(str);
TEST_ASSERT_EQUAL_STRING("reward", str);
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_an_empty_string);
RUN_TEST(test_a_word);
RUN_TEST(test_a_capitalized_word);
RUN_TEST(test_a_sentence_with_punctuation);
RUN_TEST(test_a_palindrome);
RUN_TEST(test_an_evensized_word);
return UNITY_END();
}
Loading

0 comments on commit 6a88603

Please sign in to comment.