Avatar of ruihuahan213

ruihuahan213's solution

to Pig Latin in the Go Track

Published at May 19 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 piglatin converts a word into a pig latin word
package piglatin

import (
	"regexp"
	"strings"
)

// Sentence function converts the word
func Sentence(s string) string {
	var words = strings.Split(s, " ")
	var result = ""

	for _, w := range words {
		result += singleWord(w) + " "
	}
	return strings.TrimRight(result, " ")
}

func singleWord(s string) string {
	//var vowelPattern = regexp.MustCompile(`^[aeiou]`)
	b, _ := regexp.MatchString(`^[aeiou]`, s)
	if b {
		return s + "ay"
	}
	b, _ = regexp.MatchString(`(^(yt))|(^(xr))`, s)
	if b {
		return s + "ay"
	}

	b, _ = regexp.MatchString(`(^([^aeiou]qu))|(^(thr))|(^(sch))`, s)
	if b {
		return s[3:] + s[0:3] + "ay"
	}

	b, _ = regexp.MatchString(`(^(ch))|(^(qu))|(^(th))|(^(rh))`, s)
	if b {
		return s[2:] + s[0:2] + "ay"
	}

	b, _ = regexp.MatchString(`^[^aeiou]`, s)
	if b {
		return s[1:] + s[0:1] + "ay"
	}

	return ""
}

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?