猪拉丁方法翻译 [英] Pig-Latin method translation
问题描述
尝试用ruby编写Method,该方法将翻译成Pig-latin的字符串,即规则: 规则1:如果单词以元音开头,则在单词末尾添加"ay"声音.
Trying to write Method in ruby that will translate a string in pig-latin , the rule : Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
规则2:如果单词以辅音开头,请将其移至单词的末尾,然后在单词末尾添加"ay"声音,并且当单词以2个辅音开头时,请同时移动在单词的末尾添加"ay"
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 and also when the word begins with 2 consonants , move both to the end of the word and add an "ay"
作为一个新手,我的概率是第二个规则,当单词仅以一个辅音开始时,它起作用,但是对于不止一个,我很难使其起作用,有人可以看一下代码并让我知道我可以编写不同的代码,可能是我的错误,可能是代码需要重构.谢谢,到目前为止,我想出了以下代码:
As a newbie , my prob is the second rule , when the word begin with only one consonant it work , but for more than one , I have trouble to make it work ,Can somebody look at the code and let me know how i can code that differently and probably what is my mistake , probably the code need refactoring. Thanks , so far i come up with this code :
def translate (str)
str1="aeiou"
str2=(/\A[aeiou]/)
vowel = str1.scan(/\w/)
alpha =('a'..'z').to_a
con = (alpha - vowel).join
word = str.scan(/\w/)
if #first rule
str =~ str2
str + "ay"
elsif # second rule
str != str2
s = str.slice!(/^./)
str + s + "ay"
elsif
word[0.1]=~(/\A[con]/)
s = str.slice!(/^../)
str + s + "ay"
else
word[0..2]=~(/\A[con]/)
s = str.slice!(/^.../)
str + s + "ay"
end
end
translate("apple")应该=="appleay"
translate("apple") should == "appleay"
translate("cherry")应该=="errychay"
translate("cherry") should == "errychay"
translate("three")应该=="eethray"
translate("three") should == "eethray"
推荐答案
好的,这是一个史诗般的猪拉丁语翻译器,我确定可以使用一点重构,但是可以通过测试
okay this is an epic pig latin translator that I'm sure could use a bit of refactoring, but passes the tests
def translate(sent)
sent = sent.downcase
vowels = ['a', 'e', 'i', 'o', 'u']
words = sent.split(' ')
result = []
words.each_with_index do |word, i|
translation = ''
qu = false
if vowels.include? word[0]
translation = word + 'ay'
result.push(translation)
else
word = word.split('')
count = 0
word.each_with_index do |char, index|
if vowels.include? char
# handle words that start with 'qu'
if char == 'u' and translation[-1] == 'q'
qu = true
translation = words[i][count + 1..words[i].length] + translation + 'uay'
result.push(translation)
next
end
break
else
# handle words with 'qu' in middle
if char == 'q' and word[i+1] == 'u'
qu = true
translation = words[i][count + 2..words[i].length] + 'quay'
result.push(translation)
next
else
translation += char
end
count += 1
end
end
# translation of consonant words without qu
if not qu
translation = words[i][count..words[i].length] + translation + 'ay'
result.push(translation)
end
end
end
result.join(' ')
end
所以这将给出以下内容:
So this will give the following:
puts translate('apple') # "appleay"
puts translate("quiet") # "ietquay"
puts translate("square") # "aresquay"
puts translate("the quick brown fox") # "ethay ickquay ownbray oxfay"
这篇关于猪拉丁方法翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!