我刚开始使用 Ruby。我需要构建一个方法,该方法将两个字母作为参数并返回包含两个相同字母和它们之间包含的字母的两个数组的数组。第一个数组应该只包含元音,而第二个数组只包含辅音。例如:
def alphamek('a', 'd')
应该返回:
[['a'], ['b', 'c', 'd']]
我试过这个:
def alphamek(letter1, letter2)
first_array = (letter1..letter2).scan[aeiou].to_a
second_array = (letter1..letter2).scan[^aeiou].to_a
multi_array = [[first_array], [second_array]]
end
但是好像不行。有什么想法吗?
最佳答案
如果您从正则表达式的角度进行处理并利用像 partition
这样的工具,其实并没有那么难:
VOWEL = /[aeiou]/i
def alphamek(a, b)
(a..b).partition { |l| VOWEL.match(l) }
end
关于arrays - 以元音/辅音为元素的数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48816830/