你可以追加到基于if语句条件数组中的特定元素? [英] Can you append to specific elements in an array based on if statement conditions?
本文介绍了你可以追加到基于if语句条件数组中的特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一个开发者训练营的学生,在和我的一个项目的问题。我们以Ruby code一个隐语页面。我把它通过测试,直到它需要接受多个字的一点:
高清pig_latina(字)
#万能变量
元音= ['一','E','我','O','U']
user_output =
#如果词以元音开头加上'的方式'
如果vowels.include?(字[0])
user_output =字+'的方式'
#元音到年底之前在移动一个单词的开头第一个辅音
其他
word.split()each_with_index做|。信中,指数| 如果vowels.include?(字母)
user_output =字[指数..- 1] +字[0..index-1] +'唉'
打破
结束
结束
结束
#需要以'曲'开头的单词,并将其移动到公共汽车后面,并增加了AY
如果字[0,2] =='曲'
user_output =字[2 ..- 1] +'码头'
结束
#需要包含曲字,并将其移动到公共汽车后面,并增加了AY
如果字[1,2] =='曲'
user_output =字[3 ..- 1] +字[0] +'码头'
结束
#打印结果
user_output
结束
我不知道该怎么做。这不是功课或任何东西。我试过
字= phrase.split()
words.each做|字|
如果vowels.include?(字[0])
字+'的方式'
但我认为其他
语句是搞乱了这一切。任何有识之士将大大AP preciated!谢谢!
解决方案
高清pig_latina(字)
preFIX =字[0%(重)(A E I O U).MAP {|元音| #{}一词AEIOU的.index(元音)}。分]
preFIX ='曲'如果字[0,2] =='曲'
prefix.length == 0? #{}字路:#{字[prefix.length ..- 1]}#{preFIX} AY
结束短语=狗跳过了鹌鹑
翻译= phrase.scan(/ \\ W + /){地图|。字| pig_latina(字)}。加入().capitalize把翻译#=> Ethay ogday umpedjay overway ethay ailquay
I am a developer bootcamp student and was having an issue with one of my projects. We are using Ruby to code a Pig Latin page. I got it passing tests up until the point where it needs to accept multiple words:
def pig_latina(word)
# univeral variables
vowels = ['a','e','i','o','u']
user_output = ""
# adds 'way' if the word starts with a vowel
if vowels.include?(word[0])
user_output = word + 'way'
# moves the first consonants at the beginning of a word before a vowel to the end
else
word.split("").each_with_index do |letter, index|
if vowels.include?(letter)
user_output = word[index..-1] + word[0..index-1] + 'ay'
break
end
end
end
# takes words that start with 'qu' and moves it to the back of the bus and adds 'ay'
if word[0,2] == 'qu'
user_output = word[2..-1] + 'quay'
end
# takes words that contain 'qu' and moves it to the back of the bus and adds 'ay'
if word[1,2] == 'qu'
user_output = word[3..-1] + word[0] + 'quay'
end
# prints result
user_output
end
I don't know how to do it. This isn't homework or anything. I tried
words = phrase.split(" ")
words.each do |word|
if vowels.include?(word[0])
word + 'way'
but I think that the else
statement is messing it all up. Any insight would be greatly appreciated! Thanks!!
解决方案
def pig_latina(word)
prefix = word[0, %w(a e i o u).map{|vowel| "#{word}aeiou".index(vowel)}.min]
prefix = 'qu' if word[0, 2] == 'qu'
prefix.length == 0 ? "#{word}way" : "#{word[prefix.length..-1]}#{prefix}ay"
end
phrase = "The dog jumped over the quail"
translated = phrase.scan(/\w+/).map{|word| pig_latina(word)}.join(" ").capitalize
puts translated # => "Ethay ogday umpedjay overway ethay ailquay"
这篇关于你可以追加到基于if语句条件数组中的特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文