Go - Acronym
This commit is contained in:
parent
1abb8f7e73
commit
8ff43f2212
27
go/acronym/README.md
Normal file
27
go/acronym/README.md
Normal file
@ -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.
|
||||
|
46
go/acronym/acronym.go
Normal file
46
go/acronym/acronym.go
Normal file
@ -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
|
||||
}
|
36
go/acronym/acronym_test.go
Normal file
36
go/acronym/acronym_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
binary-search-tree
|
28
go/pangram/README.md
Normal file
28
go/pangram/README.md
Normal file
@ -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.
|
||||
|
43
go/pangram/pangram_test.go
Normal file
43
go/pangram/pangram_test.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user