我有:
ary = [
'ahorseride', 'amparkeetjump', 'acatlive', 'adogwish', 'bmparkeetjump',
'bcatlive', 'bdogwish', 'bhorseride', 'brabuffallo', 'chorseride7679',
'ceelionking5454', 'crabuffallokjkj4', 'dgiraffeoiu9-0', 'chorseride767',
'ccatlive', 'dcatlive', 'ddogwish', 'emparkeetjump', 'emouse', 'eeelionking',
'erabuffallo', 'ffhorseride7679', 'fgeelionking5454', 'fhcrabuffallokjkj4a',
'fkcgiraffeoiu9087*s',
]
big_animal = ['horse', 'lion', 'buffallo', 'giraffe']
对于big_animal
中的每个元素,我想找到ary
的哪些元素将其包含为子字符串,并按特定顺序显示它们。我想实现这个结果:
horse in the chorseride7679
lion in the ceelionking5454
buffallo in the crabuffallokjkj4
giraffe in the dgiraffeoiu9-0
和/或
horse in the ffhorseride7679
lion in the fgeelionking5454
buffallo in the fhcrabuffallokjkj4a
giraffe in the fkcgiraffeoiu9087*s
我该怎么做?我的尝试是:
horse = big_animal[0]
ary.each do |e|
puts "#{horse} in the house of #{e}" if e.include?(horse)
end
的结果是:
horse in the house of ahorseride
horse in the house of bhorseride
horse in the house of chorseride7679
horse in the house of chorseride767
horse in the house of ffhorseride7679
答案 0 :(得分:1)
您希望找到ary
的四个连续元素,分别包含作为big_animal
元素的字符串。如果确实如此,ary
的所有这些连续元素都可以如下获得。
ary.each_cons(big_animal.size).select do |words|
big_animal.each_index.all? { |i| words[i].include?(big_animal[i]) }
end
#=> [["chorseride7679", "ceelionking5454", "crabuffallokjkj4", "dgiraffeoiu9-0"],
# ["ffhorseride7679", "fgeelionking5454", "fhcrabuffallokjkj4a",
# "fkcgiraffeoiu9087*s"]]
答案 1 :(得分:0)
猜测你正在寻找具有匹配名称的第一只动物,以数字结尾。
Clone
答案 2 :(得分:0)
首先,我必须承认这是我第一次使用Ruby,这就是为什么我的代码可能有点混乱。
在第二个注释中,我已经看到了nPn得到你想要的正则表达式输出的答案。但是,这并不能完全解决您的问题,因为他的解决方案只会检索包含big_animal和以数字结尾的值。这个解决方案并不关心房屋的正确顺序。
据我所知,您需要包含 big_animal 的所有房屋,但前提是它们的顺序相同。这就是我提出这段代码的原因:
ary = ['ahorseride', 'amparkeetjump', 'acatlive', 'adogwish', 'bmparkeetjump', 'bcatlive', 'bdogwish', 'bhorseride', 'brabuffallo', 'chorseride7679', 'ceelionking5454', 'crabuffallokjkj4', 'dgiraffeoiu9-0', 'chorseride767', 'ccatlive', 'dcatlive', 'ddogwish', 'emparkeetjump', 'emouse', 'eeelionking', 'erabuffallo', 'ffhorseride7679', 'fgeelionking5454', 'fhcrabuffallokjkj4', 'fkcgiraffeoiu9087', ]
big_animal = ['horse', 'lion', 'buffallo', 'giraffe']
count = 0
houses = Array.new(big_animal.length)
while count < ary.length do
animals = 0
if ary[count].include?(big_animal[animals])
while animals < big_animal.length do
if ary[count+animals].include?(big_animal[animals])
houses[animals] = ary[count+animals]
if animals == big_animal.length-1
puts houses
end
else
houses = Array.new(big_animal.length)
end
animals = animals + 1
end
end
count = count + 1
end
上面的代码给出了以下输出:
chorseride7679
ceelionking5454
crabuffallokjkj4
dgiraffeoiu9-0
ffhorseride7679
fgeelionking5454
fhcrabuffallokjkj4
fkcgiraffeoiu9087
您还可以try it online here并更改该网站上的输入数组以测试不同的方案。我很高兴知道这是你正在寻找的。 p>
答案 3 :(得分:0)
通过肆无忌惮地重写String方法。最好不要使用这种方法 - ))
class String
def in_house? other
(other.is_a? String) && (other.include? self)
end
def in_house_message_for other
puts "#{self} in house of #{other}" if self.in_house? other
return
end
end
然后
ary.each do |house|
big_animal.each do |animal|
animal.in_house_message_for(house)
end
end
或以big_animal
数组
big_animal.each do |animal|
ary.each do |house|
animal.in_house_message_for(house)
end
end
第二个将打印
horse in house of ahorseride
horse in house of bhorseride
horse in house of chorseride7679
horse in house of chorseride767
horse in house of ffhorseride7679
lion in house of ceelionking5454
lion in house of eeelionking
lion in house of fgeelionking5454
buffallo in house of brabuffallo
buffallo in house of crabuffallokjkj4
buffallo in house of erabuffallo
buffallo in house of fhcrabuffallokjkj4
giraffe in house of dgiraffeoiu9-0
giraffe in house of fkcgiraffeoiu9087