Skip to content

Instantly share code, notes, and snippets.

@felhix
Created February 9, 2017 22:35
Show Gist options
  • Select an option

  • Save felhix/1d2ea4c97737af9af73ee930e125ba33 to your computer and use it in GitHub Desktop.

Select an option

Save felhix/1d2ea4c97737af9af73ee930e125ba33 to your computer and use it in GitHub Desktop.
#write your code here
def isvowel(letter)
if letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u" || letter == "y"
return true
end
return false
end
def translate(word)
phrase = word.split
longueur_phrase = 0
phrase_finale = []
while longueur_phrase < phrase.length
if isvowel(phrase[longueur_phrase][0])
new_word = phrase[longueur_phrase] + "ay"
end
if isvowel(phrase[longueur_phrase][0]) == false
n=0
array_provisoire = []
while isvowel(phrase[longueur_phrase][n]) == false
if phrase[longueur_phrase][n] == "q"
array_provisoire << phrase[longueur_phrase][n]
array_provisoire << phrase[longueur_phrase][n+1]
n = n+1
else
array_provisoire << phrase[longueur_phrase][n]
end
n = n +1
end
array_provisoire << "ay"
array_provisoire_2 = []
while n < phrase[longueur_phrase].length
array_provisoire_2 << phrase[longueur_phrase][n]
n = n + 1
end
new_word = []
new_word << array_provisoire_2
new_word << array_provisoire
new_word = new_word.join
# puts new_word
end
phrase_finale << new_word
longueur_phrase = longueur_phrase + 1
end
return phrase_finale.join(" ")
end
translate("quiet cherry apple banana")
# # Topics
#
# * modules
# * strings
#
# # 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, but that should be enough to understand the tests.)
#
# See <http://en.wikipedia.org/wiki/Pig_latin> for more details.
#
#
require "pig_latin"
describe "#translate" do
it "translates a word beginning with a vowel" do
s = translate("apple")
expect(s).to eq("appleay")
end
it "translates a word beginning with a consonant" do
s = translate("banana")
expect(s).to eq("ananabay")
end
it "translates a word beginning with two consonants" do
s = translate("cherry")
expect(s).to eq("errychay")
end
it "translates two words" do
s = translate("eat pie")
expect(s).to eq("eatay iepay")
end
it "translates a word beginning with three consonants" do
expect(translate("three")).to eq("eethray")
end
it "counts 'sch' as a single phoneme" do
s = translate("school")
expect(s).to eq("oolschay")
end
it "counts 'qu' as a single phoneme" do
s = translate("quiet")
expect(s).to eq("ietquay")
end
it "counts 'qu' as a consonant even when it's preceded by a consonant" do
s = translate("square")
expect(s).to eq("aresquay")
end
it "translates many words" do
s = translate("the quick brown fox")
expect(s).to eq("ethay ickquay ownbray oxfay")
end
# Test-driving bonus:
# * write a test asserting that capitalized words are still capitalized (but with a different initial capital letter, of course)
# * retain the punctuation from the original phrase
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment