Avatar of csage

csage's solution

to Pig Latin in the Go Track

Published at Jul 06 2020 · 0 comments
Instructions
Test suite
Solution

Implement a program that translates from English to Pig Latin.

Pig Latin is a made-up children's language that's intended to be confusing. It obeys a few simple rules (below), but when it's spoken quickly it's really difficult for non-children (and non-native speakers) to understand.

  • Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word. Please note that "xr" and "yt" at the beginning of a word make vowel sounds (e.g. "xray" -> "xrayay", "yttria" -> "yttriaay").
  • Rule 2: If a word begins with a consonant sound, move it to the end of the word and then add an "ay" sound to the end of the word. Consonant sounds can be made up of multiple consonants, a.k.a. a consonant cluster (e.g. "chair" -> "airchay").
  • Rule 3: If a word starts with a consonant sound followed by "qu", move it to the end of the word, and then add an "ay" sound to the end of the word (e.g. "square" -> "aresquay").
  • Rule 4: If a word contains a "y" after a consonant cluster or as the second letter in a two letter word it makes a vowel sound (e.g. "rhythm" -> "ythmrhay", "my" -> "ymay").

There are a few more rules for edge cases, and there are regional variants too.

See http://en.wikipedia.org/wiki/Pig_latin for more details.

Coding the solution

Look for a stub file having the name pig_latin.go and place your solution code in that file.

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

The Pig Latin exercise at Test First Teaching by Ultrasaurus https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/

Submitting Incomplete Solutions

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

cases_test.go

package piglatin

// Source: exercism/problem-specifications
// Commit: d77de78 pig-latin: Apply new "input" policy
// Problem Specifications Version: 1.2.0

var testCases = []struct {
	description string
	input       string
	expected    string
}{
	{
		description: "word beginning with a",
		input:       "apple",
		expected:    "appleay",
	},
	{
		description: "word beginning with e",
		input:       "ear",
		expected:    "earay",
	},
	{
		description: "word beginning with i",
		input:       "igloo",
		expected:    "iglooay",
	},
	{
		description: "word beginning with o",
		input:       "object",
		expected:    "objectay",
	},
	{
		description: "word beginning with u",
		input:       "under",
		expected:    "underay",
	},
	{
		description: "word beginning with a vowel and followed by a qu",
		input:       "equal",
		expected:    "equalay",
	},
	{
		description: "word beginning with p",
		input:       "pig",
		expected:    "igpay",
	},
	{
		description: "word beginning with k",
		input:       "koala",
		expected:    "oalakay",
	},
	{
		description: "word beginning with x",
		input:       "xenon",
		expected:    "enonxay",
	},
	{
		description: "word beginning with q without a following u",
		input:       "qat",
		expected:    "atqay",
	},
	{
		description: "word beginning with ch",
		input:       "chair",
		expected:    "airchay",
	},
	{
		description: "word beginning with qu",
		input:       "queen",
		expected:    "eenquay",
	},
	{
		description: "word beginning with qu and a preceding consonant",
		input:       "square",
		expected:    "aresquay",
	},
	{
		description: "word beginning with th",
		input:       "therapy",
		expected:    "erapythay",
	},
	{
		description: "word beginning with thr",
		input:       "thrush",
		expected:    "ushthray",
	},
	{
		description: "word beginning with sch",
		input:       "school",
		expected:    "oolschay",
	},
	{
		description: "word beginning with yt",
		input:       "yttria",
		expected:    "yttriaay",
	},
	{
		description: "word beginning with xr",
		input:       "xray",
		expected:    "xrayay",
	},
	{
		description: "y is treated like a consonant at the beginning of a word",
		input:       "yellow",
		expected:    "ellowyay",
	},
	{
		description: "y is treated like a vowel at the end of a consonant cluster",
		input:       "rhythm",
		expected:    "ythmrhay",
	},
	{
		description: "y as second letter in two letter word",
		input:       "my",
		expected:    "ymay",
	},
	{
		description: "a whole phrase",
		input:       "quick fast run",
		expected:    "ickquay astfay unray",
	},
}

pig_latin_test.go

package piglatin

import (
	"fmt"
	"testing"
)

func TestPigLatin(t *testing.T) {
	for _, test := range testCases {
		if pl := Sentence(test.input); pl != test.expected {
			t.Fatalf("FAIL: Sentence(%q) = %q, want %q.", test.input, pl, test.expected)
		}
		fmt.Printf("PASS: %s\n", test.description)
	}
}
package piglatin

import (
	"regexp"
	"strings"
)

func Sentence(input string) (s string) {
	r := regexp.MustCompile(`^((?:[aeiou]|xr|yt).*|[^aeiou]*qu|[^aeiouy]+|[^aeiou]+)(.*)`)

	words := []string{}
	for _, w := range strings.Fields(strings.ToLower(input)) {
		words = append(words, r.ReplaceAllString(w, "$2$1")+"ay")
	}

	return strings.Join(words, " ")
}

Community comments

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

csage's Reflection

Managed to simply my solution thanks to the inspiration provided by joostshao's. Please note that (same as his) this only satisfies the tests provided as opposed to all cases possible in pig latin in general.

However, I felt that the objective here was primarily to get (re-)acquainted with the possibilities of regular expressions, and the exercise certainly did that for me.