Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何查看一个字符串的字符在第二个字符串中是否按顺序出现_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何查看一个字符串的字符在第二个字符串中是否按顺序出现

Ruby on rails 如何查看一个字符串的字符在第二个字符串中是否按顺序出现,ruby-on-rails,ruby,Ruby On Rails,Ruby,给定两个字符串“word”和“key”,如果“key”中的字符在“word”中以相同的顺序出现(但不一定是连续的),如何编写返回true(否则false)的方法sequence\u search(word,key) 例如,假设,key='cat'。如果“word”等于“arcata”,“c1a2t3”或“coat”,则该方法应返回true,但如果“word”等于“cta”,则返回false 以下是我试图回答的问题: def sequence_search(word, key) arr =

给定两个字符串“word”和“key”,如果“key”中的字符在“word”中以相同的顺序出现(但不一定是连续的),如何编写返回
true
(否则
false
)的方法
sequence\u search(word,key)

例如,假设,
key='cat'
。如果“word”等于
“arcata”
“c1a2t3”
“coat”
,则该方法应返回
true
,但如果“word”等于
“cta”
,则返回
false

以下是我试图回答的问题:

def sequence_search(word, key)
    arr = []
    i = 0
    while i < word.length
     word[i].include?(key)
    arr >> word[i]
      end
      i+= 1
    end
      if arr.join == key  # line raising exception
        return true
      else false
      end
    end

在指示的行中。为什么?有没有更好的方法来编写此方法?

我不知道您在寻找什么解决方案,但这对我来说是一个快速的解决方案:

def序列搜索(单词、键)
arr=键拆分(“”)
每一个都有|
如果word.index(c)=nil,则返回false
单词,切片!(0,字索引(c)+1)
如果arr.last==c,则返回true
结束
结束
序列搜索('cat','cat')#=>真
序列搜索('cdadsas','cat')#=>false
序列搜索('GDFGDDCGDDADSAST','cat')#=>true
序列搜索('GDFGDDCGDDADSAST','cat4')#=>false

我不知道您在寻找什么解决方案,但这对我来说是一个快速的解决方案:

def序列搜索(单词、键)
arr=键拆分(“”)
每一个都有|
如果word.index(c)=nil,则返回false
单词,切片!(0,字索引(c)+1)
如果arr.last==c,则返回true
结束
结束
序列搜索('cat','cat')#=>真
序列搜索('cdadsas','cat')#=>false
序列搜索('GDFGDDCGDDADSAST','cat')#=>true
序列搜索('GDFGDDCGDDADSAST','cat4')#=>false

要确定代码的问题,请先将其正确格式化

def sequence_search(word, key)
  arr = []
  i = 0
  while i < word.length
    word[i].include?(key)
    arr >> word[i]
  end
  i += 1
end

if arr.join == key  # line raising exception
  return true
else
  false
end

end

请参阅,特别注意可选的第二个参数,搜索开始时字符串的偏移量

使用正则表达式

def sequence_search(word, key)
  word.match?(/#{key.chars.join(".*")}/)
end

key=“cat”


正则表达式如下:“匹配一个“c”,后跟零个或多个字符,后跟“a”,后跟零个或多个字符,后跟“t”。

要确定代码的问题,请先正确格式化代码

def sequence_search(word, key)
  arr = []
  i = 0
  while i < word.length
    word[i].include?(key)
    arr >> word[i]
  end
  i += 1
end

if arr.join == key  # line raising exception
  return true
else
  false
end

end

请参阅,特别注意可选的第二个参数,搜索开始时字符串的偏移量

使用正则表达式

def sequence_search(word, key)
  word.match?(/#{key.chars.join(".*")}/)
end

key=“cat”


正则表达式如下:“匹配一个“c”,后跟零个或多个字符,后跟“a”,后跟零个或多个字符,后跟“t”。

删除每个不在键中的字符,并检查键是否包含在余数中:

def sequence_search(str, key)
  str.delete("^#{key}").include?(key) # ^ means "everything but"
end

删除不在键中的每个字符,并检查键是否包含在余数中:

def sequence_search(str, key)
  str.delete("^#{key}").include?(key) # ^ means "everything but"
end
有用信息:

require 'benchmark'

N = 100_000

puts 'Ruby %s' % RUBY_VERSION

def cary1(word, key)
  i = -1
  key.each_char.all? { |c| i = word.index(c,i+1) }
end

def cary2(word, key)
  word.match?(/#{key.chars.join(".*")}/)
end

def steenslag(str, key)
  str.delete("^#{key}").include?(key) # ^ means "everything but"
end

def estebes(word, key)
  arr = key.split('')
  arr.each do |c|
    return false if word.index(c) == nil
    word.slice!(0, word.index(c) + 1)
    return true if arr.last == c
  end
end

Benchmark.bmbm do |x|
  x.report('cary1')     { N.times { cary1("arcata", "cat") } }
  x.report('cary2')     { N.times { cary2("arcata", "cat") } }
  x.report('steenslag') { N.times { steenslag("arcata", "cat") } }
  x.report('estebes')   { N.times { estebes("arcata", "cat") } }
end
使用:

有用信息:

require 'benchmark'

N = 100_000

puts 'Ruby %s' % RUBY_VERSION

def cary1(word, key)
  i = -1
  key.each_char.all? { |c| i = word.index(c,i+1) }
end

def cary2(word, key)
  word.match?(/#{key.chars.join(".*")}/)
end

def steenslag(str, key)
  str.delete("^#{key}").include?(key) # ^ means "everything but"
end

def estebes(word, key)
  arr = key.split('')
  arr.each do |c|
    return false if word.index(c) == nil
    word.slice!(0, word.index(c) + 1)
    return true if arr.last == c
  end
end

Benchmark.bmbm do |x|
  x.report('cary1')     { N.times { cary1("arcata", "cat") } }
  x.report('cary2')     { N.times { cary2("arcata", "cat") } }
  x.report('steenslag') { N.times { steenslag("arcata", "cat") } }
  x.report('estebes')   { N.times { estebes("arcata", "cat") } }
end
使用:


我建议首先将我们的解决方案编写为伪代码。在本例中,您将检查字符串中的第一个字符,然后在单词的剩余部分中搜索下一个字符,这样您就不会介意我的编辑了。当然,您可以自由地进行任何进一步的更改或回滚到原始问题。我建议您首先以伪代码的形式编写我们的解决方案。在本例中,您将检查字符串中的第一个字符,然后在单词的剩余部分中搜索下一个字符,这样您就不会介意我的编辑了。当然,您可以自由地进行任何进一步的更改或回滚到原始问题。Niiiice!这是一种倒退,但是使用
delete
include
让C完成繁重的工作。niiice!这是一种倒退,但是使用
delete
include
让C来完成繁重的工作。
require 'benchmark'

N = 100_000

puts 'Ruby %s' % RUBY_VERSION

def cary1(word, key)
  i = -1
  key.each_char.all? { |c| i = word.index(c,i+1) }
end

def cary2(word, key)
  word.match?(/#{key.chars.join(".*")}/)
end

def steenslag(str, key)
  str.delete("^#{key}").include?(key) # ^ means "everything but"
end

def estebes(word, key)
  arr = key.split('')
  arr.each do |c|
    return false if word.index(c) == nil
    word.slice!(0, word.index(c) + 1)
    return true if arr.last == c
  end
end

Benchmark.bmbm do |x|
  x.report('cary1')     { N.times { cary1("arcata", "cat") } }
  x.report('cary2')     { N.times { cary2("arcata", "cat") } }
  x.report('steenslag') { N.times { steenslag("arcata", "cat") } }
  x.report('estebes')   { N.times { estebes("arcata", "cat") } }
end
# >> Ruby 2.7.1
# >> Rehearsal ---------------------------------------------
# >> cary1       0.128231   0.000218   0.128449 (  0.128572)
# >> cary2       0.461305   0.000509   0.461814 (  0.462048)
# >> steenslag   0.055794   0.000026   0.055820 (  0.055847)
# >> estebes     0.263030   0.000185   0.263215 (  0.263399)
# >> ------------------------------------ total: 0.909298sec
# >> 
# >>                 user     system      total        real
# >> cary1       0.131944   0.000141   0.132085 (  0.132227)
# >> cary2       0.453452   0.000626   0.454078 (  0.454374)
# >> steenslag   0.055342   0.000026   0.055368 (  0.055394)
# >> estebes     0.255280   0.000156   0.255436 (  0.255607)
require 'fruity'

puts 'Ruby %s' % RUBY_VERSION

def cary1(word, key)
  i = -1
  key.each_char.all? { |c| i = word.index(c,i+1) }
end

def cary2(word, key)
  word.match?(/#{key.chars.join(".*")}/)
end

def steenslag(str, key)
  str.delete("^#{key}").include?(key) # ^ means "everything but"
end

def estebes(word, key)
  arr = key.split('')
  arr.each do |c|
    return false if word.index(c) == nil
    word.slice!(0, word.index(c) + 1)
    return true if arr.last == c
  end
end

compare do
  _cary1     { cary1("arcata", "cat") } 
  _cary2     { cary2("arcata", "cat") } 
  _steenslag { steenslag("arcata", "cat") } 
  _estebes   { estebes("arcata", "cat") } 
end
# >> Ruby 2.7.1
# >> Running each test 8192 times. Test will take about 2 seconds.
# >> _steenslag is faster than _cary1 by 2x ± 0.1
# >> _cary1 is faster than _estebes by 2x ± 0.1
# >> _estebes is faster than _cary2 by 2x ± 0.1