Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reverse-string approaches #335

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions exercises/practice/reverse-string/.approaches/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"approaches": [
{
"uuid": "5e6c8ae8-7cd0-4500-bc1b-e21f036eb708",
"slug": "reversing-list",
"title": "Reversing a List of Characters",
"blurb": "Explode the string into a list of characters and reverse it",
"authors": [
"BNAndras"
]
},
{
"uuid": "92f3c425-7696-4759-88dc-07ebe2c2f249",
"slug": "reversing-with-substrings",
"title": "Reversing with Substrings",
"blurb": "Reverse the string with recursion and substrings",
"authors": [
"BNAndras"
]
},
{
"uuid": "b7c8e68e-69f7-4e14-be0c-3191f57fb3b4",
"slug": "reversing-by-index",
"title": "Reversing by Index",
"blurb": "Reverse the string by iterating backwards",
"authors": [
"BNAndras"
]
}
]
}
40 changes: 40 additions & 0 deletions exercises/practice/reverse-string/.approaches/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Introduction

There are several idiomatic ways to solve this exercise.

## Approach: Reversing a list of characters

```scheme
(define (my-reverse s)
(list->string (foldl cons '() (string->list s))))
```

For more information, check the [reversing a list of characters approach][approach-reversing-list].

## Approach: Reversing with substrings

```scheme
(define (my-reverse s)
(if (non-empty-string? s)
(string-append (my-reverse (substring s 1))
(substring s 0 1))
""))
```

For more information, check the [reversing with substrings approach][approach-reversing-substrings].

## Approach: Reversing by index

```scheme
(define (my-reverse s)
(define end (sub1 (string-length s)))
(build-string (add1 end)
(λ (i) (string-ref s (- end i)))))
```

For more information, check the [reversing by index approach][approach-reversing-by-index].


[approach-reversing-list]: https://exercism.org/tracks/racket/exercises/reverse-string/approaches/reversing-list
[approach-reversing-substrings]: https://exercism.org/tracks/racket/exercises/reverse-string/approaches/reversing-substrings
[approach-reversing-by-index]: https://exercism.org/tracks/racket/exercises/reverse-string/approaches/reversing-by-index
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Reversing by Index

```scheme
(define (my-reverse s)
(define end (sub1 (string-length s)))
(build-string (add1 end)
(λ (i) (string-ref s (- end i)))))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(define (my-reverse s)
(define end (sub1 (string-length s)))
(build-string (add1 end)
(λ (i) (string-ref s (- end i)))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Reversing a List of Characters

```scheme
(define (my-reverse s)
(list->string (foldl cons '() (string->list s))))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(define (my-reverse s)
(list->string (foldl cons '() (string->list s))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Reversing with Substrings

```scheme
(define (my-reverse s)
(if (non-empty-string? s)
(string-append (my-reverse (substring s 1))
(substring s 0 1))
""))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(define (my-reverse s)
(if (non-empty-string? s)
(string-append (my-reverse (substring s 1))
(substring s 0 1))
""))
Loading