From d8f2fbf5a82505b5d33e5d8a37fabafd19244aa7 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 11 Nov 2016 01:13:51 +0100 Subject: Solve acronym --- go/acronym/README.md | 27 +++++++++++++++++++++++++++ go/acronym/acronym.go | 23 +++++++++++++++++++++++ go/acronym/acronym_test.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 go/acronym/README.md create mode 100644 go/acronym/acronym.go create mode 100644 go/acronym/acronym_test.go 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) + } + } +} -- cgit v1.2.3