10000 reverse alphabetical chars from a string · developgo/algorithms_with_Go@48e8104 · GitHub
[go: up one dir, main page]

Skip to content

Commit 48e8104

Browse files
committed
reverse alphabetical chars from a string
1 parent 0b8231d commit 48e8104

File tree

5 files changed

+118
-1
lines changed

5 files changed

+118
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ WIP, the description of the below `unsolved yet` problems can be found in the or
107107

108108
## [String / Array](https://github.com/danrusei/algorithms_with_Go/tree/main/strings)
109109

110-
- [] Reverse an array without affecting special characters
110+
- [x] [Reverse an array without affecting special characters](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/reverse_alpha)
111111
- [] All Possible Palindromic Partitions
112112
- [] Count triplets with sum smaller than a given value
113113
- [] Convert array into Zig-Zag fashion
@@ -117,3 +117,4 @@ WIP, the description of the below `unsolved yet` problems can be found in the or
117117
- [] Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
118118
- [] Smallest subarray with sum greater than a given value
119119
- [] Stock Buy Sell to Maximize Profit
120+

strings/reverse_alpha/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Reverse a string without affecting special characters
2+
3+
Given a string, that contains special character together with alphabets (‘a’ to ‘z’ and ‘A’ to ‘Z’), reverse the string in a way that special characters are not affected.
4+
5+
## Example
6+
7+
> Input: str = "a,b$c"
8+
> Output: str = "c,b$a"
9+
> Note that $ and , are not moved anywhere.
10+
> Only subsequence "abc" is reversed
11+
>
12+
> Input: str = "Ab,c,de!$"
13+
> Output: str = "ed,c,bA!$"
14+
15+
## Algorithm
16+
17+
* Let input string be `str` and length of string be `n`
18+
* l = 0, r = n-1
19+
* for `l` is smaller than `r`, do following
20+
* If `str[l]` is not an alphabetic character, do `l++`
21+
* else if `str[r]` is not an alphabetic character, do `r--`
22+
* else swap `str[l]` and `str[r]`
23+
24+
## Result
25+
26+
```bash
27+
$ go test -v
28+
=== RUN TestReverse
29+
=== RUN TestReverse/simple
30+
=== RUN TestReverse/with_punctuation
31+
=== RUN TestReverse/with_capital
32+
=== RUN TestReverse/with_trailing_space
33+
--- PASS: TestReverse (0.00s)
34+
--- PASS: TestReverse/simple (0.00s)
35+
--- PASS: TestReverse/with_punctuation (0.00s)
36+
--- PASS: TestReverse/with_capital (0.00s)
37+
--- PASS: TestReverse/with_trailing_space (0.00s)
38+
PASS
39+
ok _/home/rdan/projects_public/algorithms_with_Go/strings/reverse_alpha 0.001s
40+
```

strings/reverse_alpha/reverse.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package reverse
2+
3+
import (
4+
"fmt"
5+
"unicode"
6+
)
7+
8+
func reverse(input string) string {
9+
toSlice := make([]rune, len(input))
10+
for i, c := range input {
11+
toSlice[i] = c
12+
}
13+
reverseSlice(toSlice)
14+
15+
return fmt.Sprint(string(toSlice))
16+
}
17+
18+
func reverseSlice(input []rune) {
19+
l := 0
20+
r := len(input) - 1
21+
22+
for l < r {
23+
if !unicode.IsLetter(input[l]) {
24+
l++
25+
} else if !unicode.IsLetter(input[r]) {
26+
r--
27+
} else {
28+
input[l], input[r] = input[r], input[l]
29+
l++
30+
r--
31+
}
32+
}
33+
}

strings/reverse_alpha/reverse_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package reverse
2+
3+
import "testing"
4+
5+
func TestReverse(t *testing.T) {
6+
for _, tc := range testcases {
7+
t.Run(tc.name, func(t *testing.T) {
8+
result := reverse(tc.input)
9+
if result != tc.output {
10+
t.Errorf("Expected: %s, got: %s", tc.output, result)
11+
}
12+
13+
})
14+
}
15+
}

strings/reverse_alpha/test_cases.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package reverse
2+
3+
var testcases = []struct {
4+
name string
5+
input string
6+
output string
7+
}{
8+
{
9+
name: "simple",
10+
input: "abc",
11+
output: "cba",
12+
},
13+
{
14+
name: "with_punctuation",
15+
input: "a,b$c",
16+
output: "c,b$a",
17+
},
18+
{
19+
name: "with_capital",
20+
input: "Ab,c,de!$",
21+
output: "ed,c,bA!$",
22+
},
23+
{
24+
name: "with_trailing_space",
25+
input: "a,b$c ",
26+
output: "c,b$a ",
27+
},
28+
}

0 commit comments

Comments
 (0)
0