summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-09-22 15:26:42 +0200
committerDimitri Sokolyuk <demon@dim13.org>2018-09-22 15:26:42 +0200
commit1c0526fd0df159c46c9b5117704894ec49158cd0 (patch)
treea711a2c2a1540e90cb747d90627642f646469bd5
parentdea0ce8b414f72b60d3b7d0a9c8ac72296e8705d (diff)
solve reverse string
-rw-r--r--go/reverse-string/.solution.json1
-rw-r--r--go/reverse-string/README.md31
-rw-r--r--go/reverse-string/cases_test.go39
-rw-r--r--go/reverse-string/reverse_string.go10
-rw-r--r--go/reverse-string/reverse_string_test.go44
5 files changed, 125 insertions, 0 deletions
diff --git a/go/reverse-string/.solution.json b/go/reverse-string/.solution.json
new file mode 100644
index 0000000..b7137a4
--- /dev/null
+++ b/go/reverse-string/.solution.json
@@ -0,0 +1 @@
+{"track":"go","exercise":"reverse-string","id":"9cba46fc62dd43f6b492d489f98fdbc5","url":"https://exercism.io/my/solutions/9cba46fc62dd43f6b492d489f98fdbc5","handle":"dim13","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/go/reverse-string/README.md b/go/reverse-string/README.md
new file mode 100644
index 0000000..43f446b
--- /dev/null
+++ b/go/reverse-string/README.md
@@ -0,0 +1,31 @@
+# Reverse String
+
+Reverse a string
+
+For example:
+input: "cool"
+output: "looc"
+
+## 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.
diff --git a/go/reverse-string/cases_test.go b/go/reverse-string/cases_test.go
new file mode 100644
index 0000000..ee46a78
--- /dev/null
+++ b/go/reverse-string/cases_test.go
@@ -0,0 +1,39 @@
+package reverse
+
+// Source: exercism/problem-specifications
+// Commit: 2f77985 reverse-string: apply "input" policy
+// Problem Specifications Version: 1.1.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",
+ },
+}
diff --git a/go/reverse-string/reverse_string.go b/go/reverse-string/reverse_string.go
new file mode 100644
index 0000000..d01a5a8
--- /dev/null
+++ b/go/reverse-string/reverse_string.go
@@ -0,0 +1,10 @@
+package reverse
+
+// String reverse
+func String(s string) string {
+ r := []rune(s)
+ for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
+ r[i], r[j] = r[j], r[i]
+ }
+ return string(r)
+}
diff --git a/go/reverse-string/reverse_string_test.go b/go/reverse-string/reverse_string_test.go
new file mode 100644
index 0000000..70c1e77
--- /dev/null
+++ b/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 := String(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 == String(String(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 {
+ String(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",
+ },
+}