Skip to content

Commit 2c0ccc1

Browse files
committed
Initial commit for reverse-string approaches
1 parent de4fb45 commit 2c0ccc1

File tree

8 files changed

+105
-0
lines changed

8 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"approaches": [
3+
{
4+
"uuid": "5e6c8ae8-7cd0-4500-bc1b-e21f036eb708",
5+
"slug": "reversing-list",
6+
"title": "Reversing a List of Characters",
7+
"blurb": "Explode the string into a list of characters and reverse it",
8+
"authors": [
9+
"BNAndras"
10+
]
11+
},
12+
{
13+
"uuid": "92f3c425-7696-4759-88dc-07ebe2c2f249",
14+
"slug": "reversing-with-substrings",
15+
"title": "Reversing with Substrings",
16+
"blurb": "Reverse the string with recursion and substrings",
17+
"authors": [
18+
"BNAndras"
19+
]
20+
},
21+
{
22+
"uuid": "b7c8e68e-69f7-4e14-be0c-3191f57fb3b4",
23+
"slug": "reversing-by-index",
24+
"title": "Reversing by Index",
25+
"blurb": "Reverse the string by iterating backwards",
26+
"authors": [
27+
"BNAndras"
28+
]
29+
}
30+
]
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Introduction
2+
3+
There are several idiomatic ways to solve this exercise.
4+
5+
## Approach: Reversing a list of characters
6+
7+
```scheme
8+
(define (my-reverse s)
9+
(list->string (foldl cons '() (string->list s))))
10+
```
11+
12+
For more information, check the [reversing a list of characters approach][approach-reversing-list].
13+
14+
## Approach: Reversing with substrings
15+
16+
```scheme
17+
(define (my-reverse s)
18+
(if (non-empty-string? s)
19+
(string-append (my-reverse (substring s 1))
20+
(substring s 0 1))
21+
""))
22+
```
23+
24+
For more information, check the [reversing with substrings approach][approach-reversing-substrings].
25+
26+
## Approach: Reversing by index
27+
28+
```scheme
29+
(define (my-reverse s)
30+
(define end (sub1 (string-length s)))
31+
(build-string (add1 end)
32+
(λ (i) (string-ref s (- end i)))))
33+
```
34+
35+
For more information, check the [reversing by index approach][approach-reversing-by-index].
36+
37+
38+
[approach-reversing-list]: https://exercism.org/tracks/racket/exercises/reverse-string/approaches/reversing-list
39+
[approach-reversing-substrings]: https://exercism.org/tracks/racket/exercises/reverse-string/approaches/reversing-substrings
40+
[approach-reversing-by-index]: https://exercism.org/tracks/racket/exercises/reverse-string/approaches/reversing-by-index
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Reversing by Index
2+
3+
```scheme
4+
(define (my-reverse s)
5+
(define end (sub1 (string-length s)))
6+
(build-string (add1 end)
7+
(λ (i) (string-ref s (- end i)))))
8+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(define (my-reverse s)
2+
(define end (sub1 (string-length s)))
3+
(build-string (add1 end)
4+
(λ (i) (string-ref s (- end i)))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Reversing a List of Characters
2+
3+
```scheme
4+
(define (my-reverse s)
5+
(list->string (foldl cons '() (string->list s))))
6+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(define (my-reverse s)
2+
(list->string (foldl cons '() (string->list s))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Reversing with Substrings
2+
3+
```scheme
4+
(define (my-reverse s)
5+
(if (non-empty-string? s)
6+
(string-append (my-reverse (substring s 1))
7+
(substring s 0 1))
8+
""))
9+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(define (my-reverse s)
2+
(if (non-empty-string? s)
3+
(string-append (my-reverse (substring s 1))
4+
(substring s 0 1))
5+
""))

0 commit comments

Comments
 (0)