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 #261

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
22 changes: 22 additions & 0 deletions exercises/practice/reverse-string/.approaches/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"approaches": [
{
"uuid": "b7d9f537-d0f7-4f80-b22a-c74ad88b949f",
"slug": "list-of-characters",
"title": "Reverse a List of Characters",
"blurb": "Explode the string into a list of characters and reverse the list",
"authors": [
"BNAndras"
]
},
{
"uuid": "8a1adc50-f84f-4d3d-acdb-26c1677d4c69",
"slug": "looping-in-reverse",
"title": "Looping in Reverse",
"blurb": "Loop over the string backwards and build up a new string",
"authors": [
"BNAndras"
]
}
]
}
31 changes: 31 additions & 0 deletions exercises/practice/reverse-string/.approaches/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Introduction

There are two common approaches highlighted here for this exercise.

## Approach: Reversing a list of characters

```vim
function! Reverse(text) abort
return join(reverse(split(a:text, '\zs')), '')
endfunction
```

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

## Approach: Looping in reverse

```vim
function! Reverse(text) abort
let l:reversed = ""
for i in range(len(a:text) - 1, -1, -1)
let l:reversed .= a:text[i]
endfor
return reversed
endfunction
```

For more information, check the [looping in reverse approach][approach-loop-in-reverse].

[modulo-operator]: https://en.wikipedia.org/wiki/Modulo
[approach-list-of-characters]: https://exercism.org/tracks/vimscript/exercises/reverse-string/approaches/list-of-characters
[approach-looping-in-reverse]: https://exercism.org/tracks/vimscript/exercises/reverse-string/approaches/looping-in-reverse
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Reverse a List of Characters

```vim
function! Reverse(text) abort
return join(reverse(split(a:text, '\zs')), '')
endfunction
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function! Reverse(text) abort
return join(reverse(split(a:text, '\zs')), '')
endfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Loop In Reverse

```vim
function! Reverse(text) abort
let l:reversed = ""
for i in range(len(a:text) - 1, -1, -1)
let l:reversed .= a:text[i]
endfor
return reversed
endfunction
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function! Reverse(text) abort
let l:reversed = ""
for i in range(len(a:text) - 1, -1, -1)
let l:reversed .= a:text[i]
endfor
return reversed
endfunction
Loading