Ruby 与扫描字符串长度不一致的结果
Ruby non consistent results with scanned string's length
我这里可能没有完整的图片,但我得到的计算结果不一致:我正在尝试解决 运行 长度编码问题,这样如果您得到像 [=19 这样的输入字符串=] 编码将是:"3A2B3A3C2A" 所以功能是:
def encode(input)
res = ""
input.scan(/(.)*/i) do |match|
res << input[/(?<bes>#{match}+)/, "bes"].length.to_s << match[0].to_s
end
res
end
我得到的结果是:
irb(main):049:0> input = "AAABBBCCCDDD"
=> "AAABBBCCCDDD"
irb(main):050:0> encode(input)
(a) => "3A3B3C3D"
irb(main):051:0> input = "AAABBBCCCAAA"
=> "AAABBBCCCAAA"
irb(main):052:0> encode(input)
(b) => "3A3B3C3A"
irb(main):053:0> input = "AAABBBCCAAA"
=> "AAABBBCCAAA"
irb(main):054:0> encode(input)
(c) => "3A3B2C3A"
irb(main):055:0> input = "AAABBBCCAAAA"
=> "AAABBBCCAAAA"
irb(main):056:0> encode(input)
(d) => "3A3B2C3A"
irb(main):057:0> input = 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB'
=> "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"
irb(main):058:0> encode(input)
(e) => "12W1B12W1B12W1B"
如您所见,结果(a)到(c)是正确的,但是结果(d)和(e)缺少一些重复,并且生成的代码短了几个字母,你能给点提示吗请问去哪里查? (我现在正在学习使用'pry')
您只会得到最先出现的匹配符号重复次数。您需要在 gsub
中执行替换并将匹配对象传递给您可以执行必要操作的块:
def encode(input)
input.gsub(/(.)*/) { |m| m.length.to_s << m[0] }
end
结果:
"AAABBBCCCDDD" => 3A3B3C3D
"AAABBBCCCAAA" => 3A3B3C3A
"AAABBBCCAAA" => 3A3B2C3A
"AAABBBCCAAAA" => 3A3B2C4A
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" => 12W1B12W3B24W1B
正则表达式很棒,但并不是解决所有问题的金锤子。
str = "AAABBAAACCCAA"
str.chars.chunk_while { |i, j| i == j }.map { |a| "#{a.size}#{a.first}" }.join
分解它的作用:
str = "AAABBAAACCCAA"
str.chars # => ["A", "A", "A", "B", "B", "A", "A", "A", "C", "C", "C", "A", "A"]
.chunk_while { |i, j| i == j } # => #<Enumerator: #<Enumerator::Generator:0x007fc1998ac020>:each>
.to_a # => [["A", "A", "A"], ["B", "B"], ["A", "A", "A"], ["C", "C", "C"], ["A", "A"]]
.map { |a| "#{a.size}#{a.first}" } # => ["3A", "2B", "3A", "3C", "2A"]
.join # => "3A2B3A3C2A"
to_a
用于说明,但不是必需的:
str = "AAABBAAACCCAA"
str.chars
.chunk_while { |i, j| i == j }
.map { |a| "#{a.size}#{a.first}" }
.join # => "3A2B3A3C2A"
how do you get to know such methods as Array#chunk_while? I am using Ruby 2.3.1 but cannot find it in the API docs, I mean, where is the compendium list of all the methods available? certainly not here ruby-doc.org/core-2.3.1/Array.html
好吧,这与问题无关,但它是有用的信息:
请记住,Array 在命令行中包含 Enumerable module, which contains chunk_while
. Use the search functionality of http://ruby-doc.org to find where things live. Also, get familiar with using ri
at the command line, and try running gem server
以获得您已安装的所有 gem 的帮助。
如果你查看 Array 文档页面,在左侧你可以看到 Array 有一个 Object 的父 class,所以它有 Object 的方法,并且它也继承自 Enumerable,因此它也会引入 Enumerable 中实现的任何内容。
我这里可能没有完整的图片,但我得到的计算结果不一致:我正在尝试解决 运行 长度编码问题,这样如果您得到像 [=19 这样的输入字符串=] 编码将是:"3A2B3A3C2A" 所以功能是:
def encode(input)
res = ""
input.scan(/(.)*/i) do |match|
res << input[/(?<bes>#{match}+)/, "bes"].length.to_s << match[0].to_s
end
res
end
我得到的结果是:
irb(main):049:0> input = "AAABBBCCCDDD"
=> "AAABBBCCCDDD"
irb(main):050:0> encode(input)
(a) => "3A3B3C3D"
irb(main):051:0> input = "AAABBBCCCAAA"
=> "AAABBBCCCAAA"
irb(main):052:0> encode(input)
(b) => "3A3B3C3A"
irb(main):053:0> input = "AAABBBCCAAA"
=> "AAABBBCCAAA"
irb(main):054:0> encode(input)
(c) => "3A3B2C3A"
irb(main):055:0> input = "AAABBBCCAAAA"
=> "AAABBBCCAAAA"
irb(main):056:0> encode(input)
(d) => "3A3B2C3A"
irb(main):057:0> input = 'WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB'
=> "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"
irb(main):058:0> encode(input)
(e) => "12W1B12W1B12W1B"
如您所见,结果(a)到(c)是正确的,但是结果(d)和(e)缺少一些重复,并且生成的代码短了几个字母,你能给点提示吗请问去哪里查? (我现在正在学习使用'pry')
您只会得到最先出现的匹配符号重复次数。您需要在 gsub
中执行替换并将匹配对象传递给您可以执行必要操作的块:
def encode(input)
input.gsub(/(.)*/) { |m| m.length.to_s << m[0] }
end
结果:
"AAABBBCCCDDD" => 3A3B3C3D
"AAABBBCCCAAA" => 3A3B3C3A
"AAABBBCCAAA" => 3A3B2C3A
"AAABBBCCAAAA" => 3A3B2C4A
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" => 12W1B12W3B24W1B
正则表达式很棒,但并不是解决所有问题的金锤子。
str = "AAABBAAACCCAA"
str.chars.chunk_while { |i, j| i == j }.map { |a| "#{a.size}#{a.first}" }.join
分解它的作用:
str = "AAABBAAACCCAA"
str.chars # => ["A", "A", "A", "B", "B", "A", "A", "A", "C", "C", "C", "A", "A"]
.chunk_while { |i, j| i == j } # => #<Enumerator: #<Enumerator::Generator:0x007fc1998ac020>:each>
.to_a # => [["A", "A", "A"], ["B", "B"], ["A", "A", "A"], ["C", "C", "C"], ["A", "A"]]
.map { |a| "#{a.size}#{a.first}" } # => ["3A", "2B", "3A", "3C", "2A"]
.join # => "3A2B3A3C2A"
to_a
用于说明,但不是必需的:
str = "AAABBAAACCCAA"
str.chars
.chunk_while { |i, j| i == j }
.map { |a| "#{a.size}#{a.first}" }
.join # => "3A2B3A3C2A"
how do you get to know such methods as Array#chunk_while? I am using Ruby 2.3.1 but cannot find it in the API docs, I mean, where is the compendium list of all the methods available? certainly not here ruby-doc.org/core-2.3.1/Array.html
好吧,这与问题无关,但它是有用的信息:
请记住,Array 在命令行中包含 Enumerable module, which contains chunk_while
. Use the search functionality of http://ruby-doc.org to find where things live. Also, get familiar with using ri
at the command line, and try running gem server
以获得您已安装的所有 gem 的帮助。
如果你查看 Array 文档页面,在左侧你可以看到 Array 有一个 Object 的父 class,所以它有 Object 的方法,并且它也继承自 Enumerable,因此它也会引入 Enumerable 中实现的任何内容。