Skip to content

Commit

Permalink
Solve reverse string
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp committed Jun 1, 2020
1 parent 8841ae7 commit 633a3f5
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions go/reverse-string/.exercism/metadata.json
@@ -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}
36 changes: 36 additions & 0 deletions go/reverse-string/README.md
@@ -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.
44 changes: 44 additions & 0 deletions go/reverse-string/cases_test.go
@@ -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",
},
}
3 changes: 3 additions & 0 deletions go/reverse-string/go.mod
@@ -0,0 +1,3 @@
module reverse

go 1.13
16 changes: 16 additions & 0 deletions go/reverse-string/reverse_string.go
@@ -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, "")
}
44 changes: 44 additions & 0 deletions go/reverse-string/reverse_string_test.go
@@ -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",
},
}

0 comments on commit 633a3f5

Please sign in to comment.