Last active
February 14, 2017 00:08
-
-
Save mtthwgry/7b3f01d39df7926c7cb0b3c89ecbcf60 to your computer and use it in GitHub Desktop.
pig latin solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ObjectOrientedPigLatinifier | |
def self.translate(string) | |
string.split(" ").map{ |word| new(word).translate }.join(" ") | |
end | |
def initialize(raw_word) | |
@raw_word = raw_word | |
@pig_latin_word = "" | |
@punctuation = "" | |
end | |
def translate | |
process_punctuation | |
find_beginning_and_end | |
pig_latinify | |
capitalize | |
return pig_latin_word | |
end | |
private | |
attr_reader :raw_word | |
attr_accessor :pig_latin_word, :punctuation, :beginning, :ending | |
def process_punctuation | |
match = raw_word.match /[!?.,]+\z/ | |
return unless match | |
self.punctuation = match[0] | |
raw_word.delete!(punctuation) | |
end | |
def find_beginning_and_end | |
if begins_with_a_vowel? | |
self.beginning = raw_word | |
self.ending = "ay" | |
elsif begins_with_two_letter_combo? | |
self.beginning = raw_word[2..-1] | |
self.ending = "#{raw_word[0..1]}ay" | |
else | |
self.beginning = raw_word[1..-1] | |
self.ending = "#{raw_word[0]}ay" | |
end | |
end | |
def begins_with_a_vowel? | |
raw_word.match(/\A[aeiou]/i) | |
end | |
def begins_with_two_letter_combo? | |
raw_word.match(/\Abr/i) || | |
raw_word.match(/\Aqu/i) || | |
raw_word.match(/\Ath/i) || | |
raw_word.match(/\Aph/i) | |
end | |
def pig_latinify | |
self.pig_latin_word = "#{beginning}#{ending}#{punctuation}" | |
end | |
def capitalize | |
if raw_word[0] == raw_word[0].upcase | |
self.pig_latin_word = pig_latin_word.downcase.capitalize | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
User stories | |
As a user I can enter a phrase "hello" and see it translated to Pig Latin "ellohay" | |
As a user I can enter a phrase "hello world" and see it translated to Pig Latin "ellohay orldway" | |
As a user I can enter a phrase "Hello world" and see it translated to Pig Latin "Ellohay orldway" | |
As a user I can enter a phrase "Hello, world!!" and see it translated to "Ellohay, orldway!!" | |
As a user I can enter a phrase "eat apples" and see it translated to Pig Latin "eatay applesay" | |
As a user I can enter a phrase "quick brown fox" and see it translated to Pig Latin "ickquay ownbray oxfay" | |
=end | |
require 'rspec' | |
require_relative './object_oriented_pig_latinifier.rb' | |
describe ObjectOrientedPigLatinifier do | |
describe '.translate' do | |
it 'should handle simple translations' do | |
expect(ObjectOrientedPigLatinifier.translate('hello')).to eq 'ellohay' | |
end | |
it 'should handle complex translations' do | |
expect(ObjectOrientedPigLatinifier.translate('Philz coffee is the best')).to eq 'Ilzphay offeecay isay ethay estbay' | |
expect(ObjectOrientedPigLatinifier.translate('eat apples')).to eq 'eatay applesay' | |
expect(ObjectOrientedPigLatinifier.translate('quick brown fox')).to eq 'ickquay ownbray oxfay' | |
end | |
it 'should handle simple translations for multiple words' do | |
expect(ObjectOrientedPigLatinifier.translate('hello world')).to eq 'ellohay orldway' | |
end | |
it 'should remember the capitalized words' do | |
expect(ObjectOrientedPigLatinifier.translate('Hello')).to eq 'Ellohay' | |
expect(ObjectOrientedPigLatinifier.translate('Hello world')).to eq 'Ellohay orldway' | |
expect(ObjectOrientedPigLatinifier.translate('Hello World')).to eq 'Ellohay Orldway' | |
end | |
it 'should remember the punctuation locations' do | |
expect(ObjectOrientedPigLatinifier.translate("I can't say this")).to eq "Iay an'tcay aysay isthay" | |
expect(ObjectOrientedPigLatinifier.translate('Hello, world!!')).to eq 'Ellohay, orldway!!' | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PigLatinifier | |
def self.translate(string) | |
words = string.split(" ") | |
words.map{ |word| translate_word(word) }.join(" ") | |
end | |
private | |
def self.translate_word(word) | |
punctuation = if punctuated?(word) | |
word.match(/[!,.]+\z/).to_a[0] | |
end | |
word.delete!(punctuation) if punctuation | |
pig_latin_word = "#{word[1..-1]}#{word[0]}ay#{punctuation}" | |
if capitalized?(word) | |
pig_latin_word = pig_latin_word.downcase.capitalize | |
end | |
pig_latin_word | |
end | |
def self.capitalized?(word) | |
word[0] == word[0].upcase | |
end | |
def self.punctuated?(word) | |
word.match(/[!,.]+\z/) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require_relative './pig_latinifier.rb' | |
describe PigLatinifier do | |
it 'should handle simple translations' do | |
expect(PigLatinifier.translate('hello')).to eq 'ellohay' | |
end | |
it 'should handle simple translations for multiple words' do | |
expect(PigLatinifier.translate('hello world')).to eq 'ellohay orldway' | |
end | |
context 'when containing capitalized words' do | |
it 'should remember the capitalized words' do | |
expect(PigLatinifier.translate('Hello')).to eq 'Ellohay' | |
expect(PigLatinifier.translate('Hello world')).to eq 'Ellohay orldway' | |
expect(PigLatinifier.translate('Hello World')).to eq 'Ellohay Orldway' | |
end | |
end | |
context 'when containing punctuation' do | |
it 'should remember the punctuation locations' do | |
expect(PigLatinifier.translate('Hello, world!!')).to eq 'Ellohay, orldway!!' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment