Avatar of Ric0chet

Ric0chet's solution

to Pig Latin in the Go Track

Published at May 21 2019 · 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.

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 igpay
// 05-04-2017 -- two benchmark examples

import (
	"regexp"
	st "strings"
)

const testVersion = 1

/* Add these test cases to exercise additional Pig Latin rules:
{"aay", "a"},
{"nthay", "nth"},
{"ashtray", "trash"},
{"ashtrayay", "ashtray"},
{"atarqay", "Qatar"},
{"ylophonexay", "xylophone"},
{"ylestay", "style"},
*/

// PigLatin converts an input string to Pig Latin
// Using Switch-Case
// 7840 ns/op -- FASTER
func PigLatin(input string) string {
	slice := st.Fields(st.ToLower(input))
	acc := []string{}
	for _, word := range slice {
		q := false

	WordLoop:
		for idx, chr := range word {
			// Initial consonant-vowels
			// (no switch-case due to Y duplicate)
			if idx == 0 && st.Contains("nxy", string(word[0])) &&
				len(word) > 1 && !st.Contains("aeiouy", string(word[1])) {
				acc = append(acc, word+"ay")
				break WordLoop
			}

			switch chr {
			case 'q':
				q = true
			case 'a', 'e', 'i', 'o', 'u', 'y':
				// Initial non-vowel Y
				if chr == 'y' && idx == 0 {
					continue
				}
				// Qu exception
				if chr == 'u' && q {
					idx++
				}
				// Earliest vowel
				acc = append(acc, word[idx:]+word[:idx]+"ay")
				break WordLoop
			}
		}
	}
	return st.Join(acc, " ")
}

/*
// ----------------------------------------------------------------------------
var vowel = regexp.MustCompile(`^([aeiou]|y[^aeiou]|x[^aeiouy])[a-z]*`)
var cons = regexp.MustCompile(`^([^aeiou]?qu|[^aeiouy]+|[^aeiou]+)([a-z]*)`)

// PigLatin converts an input string to Pig Latin
// using RegEx
// 21162 ns/op -- SLOWER, but cooler
func PigLatin(input string) string {
	slice := st.Fields(st.ToLower(input))
	for idx, word := range slice {
		if vowel.MatchString(word) {
			slice[idx] = word + "ay"
		} else if pos := cons.FindStringSubmatchIndex(word); pos != nil {
			slice[idx] = word[pos[3]:] + word[:pos[3]] + "ay"
		}
	}
	return st.Join(slice, " ")
}
*/

Community comments

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

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?