Avatar of 7h3w4rd0c70r

7h3w4rd0c70r's solution

to Acronym in the Go Track

Published at Jul 13 2018 · 1 comment
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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).

Running the tests

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.

Further information

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

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


package acronym

import (

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 {


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 (

func Abbreviate(longName string) string {
	var abbr string

	var words []string
	spaceSep := strings.Split(longName, " ")
	for _, spaceSepVal := range spaceSep {
		dashSep := strings.Split(spaceSepVal, "-")
		for _, dashSepVal := range dashSep {
			words = append(words, dashSepVal)

	for _, word := range words {
		if len(word) > 0 {
			abbr += string(strings.ToUpper(word)[0])

	return abbr

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of RobertBeilich

the strings package already includes a dedicated function for splitting at whitespaces: strings.Fields

by using it you will also get rid of empty parts (no need for your if len(word) > 0 anymore)

if splitting at whitespaces is not sufficient, there is strings.FieldsFunc, where you can pass a function that checks for your criteria (you have no need for your double nested for loop anymore)

source: strings.Fields and FieldsFunc

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?