Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"track":"go","exercise":"reverse-string","id":"f27c25aede524216a6d09c355d619b5d","url":"https://exercism.io/my/solutions/f27c25aede524216a6d09c355d619b5d","handle":"rootulp","is_requester":true,"auto_approve":false} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Reverse String | ||
|
||
Reverse a string | ||
|
||
For example: | ||
input: "cool" | ||
output: "looc" | ||
|
||
## Coding the solution | ||
|
||
Look for a stub file having the name reverse_string.go | ||
and place your solution code in that file. | ||
|
||
## Running the tests | ||
|
||
To run the tests run the command `go test` from within the exercise directory. | ||
|
||
If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem` | ||
flags: | ||
|
||
go test -v --bench . --benchmem | ||
|
||
Keep in mind that each reviewer will run benchmarks on a different machine, with | ||
different specs, so the results from these benchmark tests may vary. | ||
|
||
## Further information | ||
|
||
For more detailed information about the Go track, including how to get help if | ||
you're having trouble, please visit the exercism.io [Go language page](http://exercism.io/languages/go/resources). | ||
|
||
## Source | ||
|
||
Introductory challenge to reverse an input string [https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb](https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb) | ||
|
||
## Submitting Incomplete Solutions | ||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package reverse | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: 6c95c2e reverse-string: Add a test with an even-sized word (#1519) | ||
// Problem Specifications Version: 1.2.0 | ||
|
||
type reverseTestCase struct { | ||
description string | ||
input string | ||
expected string | ||
} | ||
|
||
var testCases = []reverseTestCase{ | ||
{ | ||
description: "an empty string", | ||
input: "", | ||
expected: "", | ||
}, | ||
{ | ||
description: "a word", | ||
input: "robot", | ||
expected: "tobor", | ||
}, | ||
{ | ||
description: "a capitalized word", | ||
input: "Ramen", | ||
expected: "nemaR", | ||
}, | ||
{ | ||
description: "a sentence with punctuation", | ||
input: "I'm hungry!", | ||
expected: "!yrgnuh m'I", | ||
}, | ||
{ | ||
description: "a palindrome", | ||
input: "racecar", | ||
expected: "racecar", | ||
}, | ||
{ | ||
description: "an even-sized word", | ||
input: "drawer", | ||
expected: "reward", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module reverse | ||
|
||
go 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package reverse | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// Reverse returns a string with characters in reversed order. | ||
func Reverse(s string) string { | ||
chars := strings.Split(s, "") | ||
result := make([]string, len(s)) | ||
|
||
for i := len(chars) - 1; i >= 0; i-- { | ||
result = append(result, chars[i]) | ||
} | ||
return strings.Join(result, "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package reverse | ||
|
||
import ( | ||
"testing" | ||
"testing/quick" | ||
) | ||
|
||
func TestReverse(t *testing.T) { | ||
for _, testCase := range append(testCases, multiByteCases...) { | ||
if res := Reverse(testCase.input); res != testCase.expected { | ||
t.Fatalf("FAIL: %s(%s)\nExpected: %q\nActual: %q", | ||
testCase.description, testCase.input, testCase.expected, res) | ||
} | ||
t.Logf("PASS: %s", testCase.description) | ||
} | ||
} | ||
|
||
func TestReverseOfReverse(t *testing.T) { | ||
assertion := func(s string) bool { | ||
return s == Reverse(Reverse(s)) | ||
} | ||
if err := quick.Check(assertion, nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func BenchmarkReverse(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, test := range testCases { | ||
Reverse(test.input) | ||
} | ||
} | ||
} | ||
|
||
// mutiByteCases adds UTF-8 multi-byte case, | ||
// since the canonical-data.json (generator data source for cases_test.go) | ||
// doesn't have any such cases. | ||
var multiByteCases = []reverseTestCase{ | ||
{ | ||
description: "a multi-byte test case", | ||
input: "Hello, 世界", | ||
expected: "界世 ,olleH", | ||
}, | ||
} |