Convert a 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 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.
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.
Julien Vanier https://github.com/monkbroc
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
package acronym
import (
"testing"
)
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)
}
}
}
func BenchmarkAcronym(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range stringTestCases {
Abbreviate(test.input)
}
}
}
package acronym
// Source: exercism/problem-specifications
// Commit: 5ae1dba Acronym canonical-data: Remove redundant test case
// Problem Specifications Version: 1.3.0
type acronymTest struct {
input string
expected string
}
var stringTestCases = []acronymTest{
{
input: "Portable Network Graphics",
expected: "PNG",
},
{
input: "Ruby on Rails",
expected: "ROR",
},
{
input: "First In, First Out",
expected: "FIFO",
},
{
input: "GNU Image Manipulation Program",
expected: "GIMP",
},
{
input: "Complementary metal-oxide semiconductor",
expected: "CMOS",
},
}
package acronym
import (
"strings"
"unicode"
)
func Abbreviate(s string) string {
result := ""
words := strings.FieldsFunc(s, Split)
for _, w := range words {
result = result + strings.ToUpper(string(w[0]))
}
return result
}
func Split(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsNumber(r)
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,108 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments