PigLatin Kata
PigLatin Kata
Create a PigLatin class that is initialized with a string
- detail: The string is a list of words seperated by spaces: 'hello world'
- detail: The string is accessed by a method named phrase
- detail: The string can be reset at any time without re-initializing
- example: PigLatin.new('hello world')
completed (Y|n):
Translate Method
Create a translate method that translates the phrase from english to pig-latin.
- detail: The method will return a string.
- detail: The empty string will return nil.
- example: "" translates to nil
completed (Y|n):
Translate words that start with vowels.
- detail: Append "ay" to the word if it ends in a consonant.
- example: "ask" translates to "askay"
- detail: Append "yay" to the word if it ends with a vowel.
- example: "apple" translates to "appleyay"
- detail: Append "nay" to the word if it ends with "y".
- example: "any" translates to "anynay"
completed (Y|n):
Translate a word that starts with a single consonant.
- detail: Removing the consonant from the front of the word.
- detail: Add the consonant to the end of the word.
- detail: Append 'ay' to the resulting word.
- example: "hello" translates to "ellohay"
- example: "world" translates to "orldway"
completed (Y|n):
Translate words that start with multiple consonants.
- detail: Remove all leading consonants from the front of the word.
- detail: Add the consonants to the end of the word.
- detail: Append 'ay' to the resulting word.
- example: "known" translates to "ownknay"
- example: "special" translates to "ecialspay"
completed (Y|n):
Support any number of words in the phrase.
- example: "hello world" translates to "ellohay orldway"
- detail: Each component of a hyphenated word is translated seperately.
- example: "well-being" translates to "ellway-eingbay"
completed (Y|n):
Support capital letters.
- detail: If a word is captalized, the translated word should be capitalized.
- example: "Bill" translates to "Illbay"
- example: "Andrew" translates to "Andreway"
completed (Y|n):
Retain punctuation.
- detail: Punctuation marks should be retained from the source to the translated string
- example: "fantastic!" tranlates to "anfasticfay!"
- example: "Three things: one, two, three." translates to "Eethray ingsthay: oneyay, otway, eethray."
completed (Y|n):
Congratulations!
- Create a PigLatin class that is initialized with a string 00:03:51
- Create a translate method that translates the phrase from english to p 00:02:09
- Translate words that start with vowels. 00:09:24
- Translate a word that starts with a single consonant. 00:05:13
- Translate words that start with multiple consonants. 00:01:26
- Support any number of words in the phrase. 02:48:13
- Support capital letters. 00:10:46
- Retain punctuation. 00:40:26
---------------------------------------------------------------------- --------
Total Time taking PigLatin kata: 04:01:27
Specs:
1 require 'spec_helper'
2 require 'piglatin'
3
4 describe PigLatin do
5 subject(:piglatin) { PigLatin.new phrase }
6 let(:phrase) { "hello world" }
7
8 describe "new" do
9 specify { expect { piglatin }.to_not raise_error }
10 end
11
12 describe ".translate" do
13 let(:phrase) { "" }
14 its(:translate) { should eq("") }
15
16 context "for words that start with vowels" do
17 context "and end with consonants" do
18 let(:phrase) { "ask" }
19 subject(:translate) { piglatin.translate }
20 it 'appends ay' do
21 should eq("askay")
22 end
23 end
24 context "and end with vowels" do
25 let(:phrase) { "apple" }
26 its(:translate) do should eq("appleyay") end
27 end
28 context "and ends with y" do
29 let(:phrase) { "any" }
30 its(:translate) { should eq(phrase + "nay") }
31 end
32 end
33
34 context "words that start with consonants" do
35 let(:phrase) { "hello" }
36 its(:translate) { should eq("ellohay") }
37 end
38 context "words that start with multiple consonants" do
39 let(:phrase) { "known" }
40 its(:translate) { should eq("ownknay") }
41 end
42 context "multiple words" do
43 let(:phrase) { "hello world" }
44 its(:translate) { should eq("ellohay orldway") }
45 end
46 context 'hyphenated words' do
47 let(:phrase) { "well-being" }
48 its(:translate) { should eq("ellway-eingbay") }
49 end
50 context 'capatalized words' do
51 let(:phrase) { "Bill" }
52 its(:translate) { should eq("Illbay") }
53 end
54 context 'capatalized words' do
55 let(:phrase) { "Andrew" }
56 its(:translate) { should eq("Andreway") }
57 end
58 context 'punctuation marks' do
59 let(:phrase) { "fantastic!" }
60 its(:translate) { should eq("antasticfay!") }
61 end
62 context 'complex phrase' do
63 let(:phrase) { 'Three things: one, two, three.' }
64 its(:translate) { should eq('Eethray ingsthay: oneyay, otway, eethray.') }
65 end
66 end
67 end
PigLatin class:
1 class PigLatin
2 attr_accessor :phrase
3 alias :initialize :phrase=
4
5 def translate
6 return "" if @phrase.empty?
7 @phrase.gsub!(/\w+/) { |word| translate_word(word) }
8 end
9
10 private
11
12 def translate_word(word)
13 # Consonants
14 word.concat(word.slice!(/^[^aeiou]*/i || ""))
15 # Vowels
16 word.gsub!(/y$/, "yn") or word.gsub!(/([aeiou])$/, '\1y')
17 # Capitalized Words
18 word.capitalize! if word.downcase!
19 # Ending
20 word += 'ay'
21 end
22 end