Avatar of paddydoyle

paddydoyle's solution

to Acronym in the Go Track

Published at Sep 13 2019 · 0 comments
Instructions
Test suite
Solution

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.

Source

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.

acronym_test.go

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

cases_test.go

package acronym

// Source: exercism/problem-specifications
// Commit: cacf1f1 Acronym: add underscore test case (#1436)
// Problem Specifications Version: 1.7.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",
	},
	{
		input:    "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me",
		expected: "ROTFLSHTMDCOALM",
	},
	{
		input:    "Something - I made up from thin air",
		expected: "SIMUFTA",
	},
	{
		input:    "Halley's Comet",
		expected: "HC",
	},
	{
		input:    "The Road _Not_ Taken",
		expected: "TRNT",
	},
}
// This is a "stub" file.  It's a little start on your solution.
// It's not a complete solution though; you have to write some code.

// Package acronym should have a package comment that summarizes what it's about.
// https://golang.org/doc/effective_go.html#commentary
package acronym

import (
	"strings"
)

var stopChars = []string{
	"-",
	"_",
}

// Abbreviate should have a comment documenting it.
func Abbreviate(s string) (output string) {
	for _, stopChar := range stopChars {
		s = strings.Replace(s, stopChar, " ", -1)
	}

	words := strings.Fields(s)

	buf := new(strings.Builder)

	for _, word := range words {
		buf.WriteByte(word[0])
	}

	return strings.ToUpper(buf.String())
}

Community comments

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

paddydoyle's Reflection

Started with regexes, which will work better in general. But for these test cases it was a lot faster to just use string operations.