summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-11-11 01:13:51 +0100
committerDimitri Sokolyuk <demon@dim13.org>2016-11-11 01:13:51 +0100
commitd8f2fbf5a82505b5d33e5d8a37fabafd19244aa7 (patch)
tree4451cff925dc276501a9d2b3acd9a44c47421cab
parentebf043f8d0336af76ce2514bee66dad1ce58658d (diff)
Solve acronym
-rw-r--r--go/acronym/README.md27
-rw-r--r--go/acronym/acronym.go23
-rw-r--r--go/acronym/acronym_test.go36
3 files changed, 86 insertions, 0 deletions
diff --git a/go/acronym/README.md b/go/acronym/README.md
new file mode 100644
index 0000000..ab36c9f
--- /dev/null
+++ b/go/acronym/README.md
@@ -0,0 +1,27 @@
+# Acronym
+
+Convert a long phrase to its acronym
+
+Techies love their TLA (Three Letter Acronyms)!
+
+Help generate some jargon by writing a program that converts a long name
+like Portable Network Graphics to its acronym (PNG).
+
+
+To run the tests simply run the command `go test` in the exercise directory.
+
+If the test suite contains benchmarks, you can run these with the `-bench`
+flag:
+
+ go test -bench .
+
+For more detailed info about the Go track see the [help
+page](http://exercism.io/languages/go).
+
+## Source
+
+Julien Vanier [https://github.com/monkbroc](https://github.com/monkbroc)
+
+## Submitting Incomplete Problems
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
diff --git a/go/acronym/acronym.go b/go/acronym/acronym.go
new file mode 100644
index 0000000..2752fd1
--- /dev/null
+++ b/go/acronym/acronym.go
@@ -0,0 +1,23 @@
+package acronym
+
+import (
+ "bytes"
+ "strings"
+ "unicode"
+)
+
+const testVersion = 1
+
+func abbreviate(s string) string {
+ buf := new(bytes.Buffer)
+ for i, v := range strings.Title(s) {
+ if i > 0 && unicode.IsUpper(rune(s[i-1])) {
+ continue
+ }
+ if unicode.IsUpper(v) {
+ buf.WriteRune(v)
+ }
+
+ }
+ return buf.String()
+}
diff --git a/go/acronym/acronym_test.go b/go/acronym/acronym_test.go
new file mode 100644
index 0000000..d68fcd6
--- /dev/null
+++ b/go/acronym/acronym_test.go
@@ -0,0 +1,36 @@
+package acronym
+
+import (
+ "testing"
+)
+
+const targetTestVersion = 1
+
+type testCase struct {
+ input string
+ expected string
+}
+
+var stringTestCases = []testCase{
+ {"Portable Network Graphics", "PNG"},
+ {"HyperText Markup Language", "HTML"},
+ {"Ruby on Rails", "ROR"},
+ {"PHP: Hypertext Preprocessor", "PHP"},
+ {"First In, First Out", "FIFO"},
+ {"Complementary metal-oxide semiconductor", "CMOS"},
+}
+
+func TestTestVersion(t *testing.T) {
+ if testVersion != targetTestVersion {
+ t.Errorf("Found testVersion = %v, want %v.", testVersion, targetTestVersion)
+ }
+}
+
+func TestAcronym(t *testing.T) {
+ for _, test := range stringTestCases {
+ actual := abbreviate(test.input)
+ if actual != test.expected {
+ t.Errorf("Acronym test [%s], expected [%s], actual [%s]", test.input, test.expected, actual)
+ }
+ }
+}