[英]Get all possible substrings and their count
I'm trying to get all possible substrings and their count in a hash. 我正在尝试获取所有可能的子字符串及其哈希值。 Eg
例如
"abc" => { a: 1, b: 1, ab: 1, bc: 1}
For that I wrote the following code: 为此,我编写了以下代码:
def get_all(b)
(0..(b.size-1)).to_a.combination(2).inject({}) { |h, g|
s = b[g[0],g[1]]
h[s] ? ( h[s] += 1) : ( h[s] = 1 )
h
}
end
But somehow It does not work correctly, because for "abchh"
It returns: 但是由于某种原因,它无法正常工作,因为对于
"abchh"
它返回:
{"a"=>1, "ab"=>1, "abc"=>1, "abch"=>1, "bc"=>1, "bch"=>1, "bchh"=>1, "chh"=>2, "hh"=>1}
chh
is in there twice , but I can't understand why. chh
在那里两次 ,但我不明白为什么。 What do I wrong? 我怎么了
Thank you! 谢谢!
String#[]
can be called in various ways, including: String#[]
可以通过多种方式调用,包括:
str[start, length] → new_str or nil str[range] → new_str or nil
The former expects start and length , whereas the latter expects a range denoting start and end . 前者期望起点和长度 ,而后者期望指示起点和终点的范围。
So instead of two arguments g[0]
and g[1]
: 因此,代替了两个参数
g[0]
和g[1]
:
b[g[0], g[1]]
you have to pass a single argument g[0]..g[1]
: 您必须传递一个参数
g[0]..g[1]
:
b[g[0]..g[1]]
Besides, you have to use repeated_combination
in order to get the single characters as well: 此外,您还必须使用
repeated_combination
才能获得单个字符:
(0..2).to_a.combination(2).to_a
#=> [[0, 1], [0, 2], [1, 2]]
(0..2).to_a.repeated_combination(2).to_a
#=> [[0, 0], [0, 1], [0, 2], [1, 1], [1, 2], [2, 2]]
Furthermore, your code can be simplified: 此外,您的代码可以简化:
a...b
instead of a..(b-1)
a...b
代替a..(b-1)
each_with_object
over inject
so you don't have to return the hash from the block each_with_object
对于inject
,更喜欢each_with_object
,因此您不必从块中返回哈希值 Hash.new(0)
Hash.new(0)
设置默认的哈希值 (i, j)
to have i..j
instead of g[0]..g[1]
(i, j)
分解元组数组(i, j)
使其具有i..j
而不是g[0]..g[1]
Example: (the indices
variable can be inlined) 示例:(可以内联
indices
变量)
def get_all(str)
indices = (0...str.size).to_a.repeated_combination(2)
indices.each_with_object(Hash.new(0)) do |(i, j), h|
h[str[i..j]] += 1
end
end
Or, using two nested loops: 或者,使用两个嵌套循环:
def get_all(str)
(0...str.size).each_with_object(Hash.new(0)) do |i, h|
(i...str.size).each do |j|
h[str[i..j]] += 1
end
end
end
Maybe the method is already doing too much. 也许该方法已经做得太多了。 I'd probably split it into two methods: one for enumerating the substrings and another one for counting them.
我可能会将其分为两种方法:一种用于枚举子字符串,另一种用于计数子字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.