From 8ff43f221215a5b58798f949629980a3acb46db7 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sat, 26 Nov 2016 21:58:00 -0600 Subject: [PATCH] Go - Acronym --- go/acronym/README.md | 27 ++++++++++++++++++++++ go/acronym/acronym.go | 46 ++++++++++++++++++++++++++++++++++++++ go/acronym/acronym_test.go | 36 +++++++++++++++++++++++++++++ go/current | 1 - go/pangram/README.md | 28 +++++++++++++++++++++++ go/pangram/pangram_test.go | 43 +++++++++++++++++++++++++++++++++++ 6 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 go/acronym/README.md create mode 100644 go/acronym/acronym.go create mode 100644 go/acronym/acronym_test.go delete mode 120000 go/current create mode 100644 go/pangram/README.md create mode 100644 go/pangram/pangram_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..f0ed41b --- /dev/null +++ b/go/acronym/acronym.go @@ -0,0 +1,46 @@ +package acronym + +import "strings" + +const testVersion = 1 + +func abbreviate(inp string) string { + var ret string + for _, wrd := range strings.Fields(inp) { + var newWrd []rune + for _, b := range wrd { + if (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') { + newWrd = append(newWrd, b) + } else { + if len(newWrd) > 0 { + ret = ret + testWord(string(newWrd)) + newWrd = []rune{} + } + } + } + ret = ret + testWord(string(newWrd)) + } + return strings.ToUpper(ret) +} + +// testWord figures out the abbreviation letters for +// the given word +func testWord(inp string) string { + if len(inp) == 0 { + return "" + } + if inp == strings.ToUpper(inp) { + // the whole word is capitalized, return + // the first letter + return string(inp[0]) + } + var ret string + // Find uppercase characters to return + for i, s := range inp { + st := string(s) + if i == 0 || st == strings.ToUpper(st) { + ret = ret + st + } + } + return ret +} 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) + } + } +} diff --git a/go/current b/go/current deleted file mode 120000 index ab81e1c..0000000 --- a/go/current +++ /dev/null @@ -1 +0,0 @@ -binary-search-tree \ No newline at end of file diff --git a/go/pangram/README.md b/go/pangram/README.md new file mode 100644 index 0000000..626cd3f --- /dev/null +++ b/go/pangram/README.md @@ -0,0 +1,28 @@ +# Pangram + +Determine if a sentence is a pangram. + +Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, +"every letter") is a sentence using every letter of the alphabet at least once. +The best known English pangram is "The quick brown fox jumps over the lazy dog." + +The alphabet used is ASCII, and case insensitive, from 'a' to 'z' +inclusively. + +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 + +Wikipedia [https://en.wikipedia.org/wiki/Pangram](https://en.wikipedia.org/wiki/Pangram) + +## 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/pangram/pangram_test.go b/go/pangram/pangram_test.go new file mode 100644 index 0000000..0ef2fb4 --- /dev/null +++ b/go/pangram/pangram_test.go @@ -0,0 +1,43 @@ +package pangram + +import ( + "testing" +) + +const targetTestVersion = 1 + +type testCase struct { + input string + expected bool + failureReason string +} + +var testCases = []testCase{ + {"", false, "sentence empty"}, + {"The quick brown fox jumps over the lazy dog", true, ""}, + {"a quick movement of the enemy will jeopardize five gunboats", false, "missing character 'x'"}, + {"the quick brown fish jumps over the lazy dog", false, "another missing character 'x'"}, + {"the 1 quick brown fox jumps over the 2 lazy dogs", true, ""}, + {"7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog", false, "missing letters replaced by numbers"}, + {"\"Five quacking Zephyrs jolt my wax bed.\"", true, ""}, + {"Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.", true, ""}, + {"Широкая электрификация южных губерний даст мощный толчок подъёму сельского хозяйства.", false, "Panagram in alphabet other than ASCII"}, +} + +func TestTestVersion(t *testing.T) { + if testVersion != targetTestVersion { + t.Errorf("Found testVersion = %v, want %v.", testVersion, targetTestVersion) + } +} + +func TestPangram(t *testing.T) { + for _, test := range testCases { + actual := IsPangram(test.input) + if actual != test.expected { + t.Errorf("Pangram test [%s], expected [%t], actual [%t]", test.input, test.expected, actual) + if !test.expected { + t.Logf("[%s] should not be a pangram because : %s\n", test.input, test.failureReason) + } + } + } +}