Merge Go Work

This commit is contained in:
2016-08-23 15:47:19 -05:00
parent b8814259b5
commit 9e58d17c5c
25 changed files with 941 additions and 50 deletions

36
go/pig-latin/README.md Normal file
View File

@@ -0,0 +1,36 @@
# Pig Latin
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.
- **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.
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.
To run the tests simply run the command `go test` in the exercise directory.
If the test suite contains benchmarks, you can run these with the `-bench`
flag:
go test -bench .
For more detailed info about the Go track see the [help
page](http://exercism.io/languages/go).
## Source
The Pig Latin exercise at Test First Teaching by Ultrasaurus [https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/](https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/)
## Submitting Incomplete Problems
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

41
go/pig-latin/igpay.go Normal file
View File

@@ -0,0 +1,41 @@
package igpay
import "strings"
// PigLatin takes some words and converts them to pig latin
// 1) Move starting consonant sounds to the end of the word
// 2) add 'ay'
func PigLatin(in string) string {
var ret string
words := strings.Split(in, " ")
for i := range words {
ret += pigLatinWord(words[i])
if i < len(words)-1 {
ret += " "
}
}
return ret
}
// pigLatinWord actually does the heavy lifting
func pigLatinWord(in string) string {
var ret string
// First move all consonant sounds to the end
for i := 0; i < len(in); i++ {
// Edge consonant sound cases
if in[0] == 'q' && in[1] == 'u' {
in = in[2:] + string(in[0]) + string(in[1])
}
vowelPos := strings.IndexAny(in, "aeiou")
// If we have a y and another vowel, then y is a consonant
// also, x
if vowelPos == 0 || (vowelPos > 1 && (in[0] == 'y' || in[0] == 'x')) {
return in + "ay"
}
in = in[1:] + string(in[0])
}
return ret
}

View File

@@ -0,0 +1,29 @@
package igpay
import "testing"
var tests = []struct{ pl, in string }{
{"appleay", "apple"},
{"earay", "ear"},
{"igpay", "pig"},
{"oalakay", "koala"},
{"airchay", "chair"},
{"eenquay", "queen"},
{"aresquay", "square"},
{"erapythay", "therapy"},
{"ushthray", "thrush"},
{"oolschay", "school"},
{"ickquay astfay unray", "quick fast run"},
{"ellowyay", "yellow"},
{"yttriaay", "yttria"},
{"enonxay", "xenon"},
{"xrayay", "xray"},
}
func TestPigLatin(t *testing.T) {
for _, test := range tests {
if pl := PigLatin(test.in); pl != test.pl {
t.Fatalf("PigLatin(%q) = %q, want %q.", test.in, pl, test.pl)
}
}
}