ruby中如何将n维数组变成字符串?
How to make n-D array into a string in ruby?
我有一个多维数组:
[
[:C],
[:C, [:C]],
[:C, [[:C]]],
[:C, [:C, [:C]]],
[:C, [:C, [:C, :C]]],
[:C, [:C, [:C, [:C]]]],
[:C, [:C, [:C, [:C, :C]]]],
]
我需要将它翻译成这样的字符串:
"C C(C) C((C)) C(C(C)) C(C(CC)) C(C(CC)) C(C(C(C)))"
为清楚起见,这里是内部数组,每个数组都显示了其翻译后的字符串:
[:C] => "C"
[:C, [:C]] => "C(C)"
[:C, [[:C]]] => "C((C))"
[:C, [:C, [:C]]] => "C(C(C))"
[:C, [:C, [:C, :C]]] => "C(C(CC))"
[:C, [:C, [:C, [:C]]]] => "C(C(CC))"
[:C, [:C, [:C, [:C, :C]]]] => "C(C(C(C)))"
我试过使用展平和连接但我没有得到
期望的结果。我想要带括号的它,这样它就可以很容易地
确定。如果我使用 flatten
或 concat
,我会得到 CCCCCCCCCCCCC
。我
想要带括号。
假设需求是这样的:
[:C] => "C"
[:C, [:C]] => "C(C)"
[:C, [[:C]]] => "C(C)"
[:C, [:C, [:C]]] => "C(C(C))"
[:C, [:C, [:C, :C]]] => "C(C(CC))"
[:C, [:C, [:C, [:C]]]] => "C(C(C(C)))"
[:C, [:C, [:C, [:C, :C]]]] => "C(C(C(CC)))"
这不是你的问题所说的,但在 D-Side 的评论中,我认为这就是你想要的:基本上,用花括号替换除了外部括号之外的所有括号,并删除外部括号和不需要的格式化。
def to_formatted_string(array)
array.inspect.gsub(/^\[|\]$|[\:\,\s]/,"").gsub("[","(").gsub("]",")")
end
测试 = [
[:C],
[:C, [:C]],
[:C, [[:C]]],
[:C, [:C, [:C]]],
[:C, [:C, [:C, :C]]],
[:C, [:C, [:C, [:C]]]],
[:C, [:C, [:C, [:C, :C]]]]
]
tests.each{|arr| puts "#{arr.inspect} => #{to_formatted_string(arr).inspect}"};false
给予
[:C] => "C"
[:C, [:C]] => "C(C)"
[:C, [[:C]]] => "C((C))"
[:C, [:C, [:C]]] => "C(C(C))"
[:C, [:C, [:C, :C]]] => "C(C(CC))"
[:C, [:C, [:C, [:C]]]] => "C(C(C(C)))"
[:C, [:C, [:C, [:C, :C]]]] => "C(C(C(CC)))"
一旦您注意到第一层和其他层使用不同的规则进行格式化,该算法就很简单了。所以为了清楚起见,我不得不使用两个不同的函数。除此之外,该算法是不言自明的。但即便如此,我还是尽可能清楚地评论了它。
data = [:C], [:C, [:C]], [:C, [[:C]]], [:C, [:C, [:C]]],
[:C, [:C, [:C, :C]]], [:C, [:C, [:C, [:C]]]],
[:C, [:C, [:C, [:C, :C]]]]
def format_step(e) # Expects an array, since all the elements of data are arrays.
e.map do |x| # So for each element, get the following
if x.is_a?(Array)
"(#{ format_step(x) })" # ...then call the same function on it
else
x.to_s # convert to string and return
end
end.join # this way map returns an array of strings here, join them
end
def reformat(data) # This rule is only for the first level and is a bit different
data.map do |element| # For each element of the root array
format_step(element) # do this
end.join(' ') # ..and join the results with spaces
end
puts reformat(data)
您可以根据需要创建字符串,如下所示:
array = [:C], [:C, [:C]], [:C, [[:C]]], [:C, [:C, [:C]]], [:C, [:C, [:C, :C]]], [:C, [:C, [:C, [:C]]]], [:C, [:C, [:C, [:C, :C]]]]
array.inspect.gsub(/(\[|\]|:C)/, '[' => '(', ']' => ')',':C' => 'c')
控制台输出:
2.1.2-p95 :029 > array = [:C], [:C, [:C]], [:C, [[:C]]], [:C, [:C, [:C]]], [:C, [:C, [:C, :C]]], [:C, [:C, [:C, [:C]]]], [:C, [:C, [:C, [:C, :C]]]]
=> [[:C], [:C, [:C]], [:C, [[:C]]], [:C, [:C, [:C]]], [:C, [:C, [:C, :C]]], [:C, [:C, [:C, [:C]]]], [:C, [:C, [:C, [:C, :C]]]]]
2.1.2-p95 :030 > array.inspect.gsub(/(\[|\]|:C)/, '[' => '(', ']' => ')',':C' => 'c')
=> "((c), (c, (c)), (c, ((c))), (c, (c, (c))), (c, (c, (c, c))), (c, (c, (c, (c)))), (c, (c, (c, (c, c)))))"
我有一个多维数组:
[
[:C],
[:C, [:C]],
[:C, [[:C]]],
[:C, [:C, [:C]]],
[:C, [:C, [:C, :C]]],
[:C, [:C, [:C, [:C]]]],
[:C, [:C, [:C, [:C, :C]]]],
]
我需要将它翻译成这样的字符串:
"C C(C) C((C)) C(C(C)) C(C(CC)) C(C(CC)) C(C(C(C)))"
为清楚起见,这里是内部数组,每个数组都显示了其翻译后的字符串:
[:C] => "C"
[:C, [:C]] => "C(C)"
[:C, [[:C]]] => "C((C))"
[:C, [:C, [:C]]] => "C(C(C))"
[:C, [:C, [:C, :C]]] => "C(C(CC))"
[:C, [:C, [:C, [:C]]]] => "C(C(CC))"
[:C, [:C, [:C, [:C, :C]]]] => "C(C(C(C)))"
我试过使用展平和连接但我没有得到
期望的结果。我想要带括号的它,这样它就可以很容易地
确定。如果我使用 flatten
或 concat
,我会得到 CCCCCCCCCCCCC
。我
想要带括号。
假设需求是这样的:
[:C] => "C"
[:C, [:C]] => "C(C)"
[:C, [[:C]]] => "C(C)"
[:C, [:C, [:C]]] => "C(C(C))"
[:C, [:C, [:C, :C]]] => "C(C(CC))"
[:C, [:C, [:C, [:C]]]] => "C(C(C(C)))"
[:C, [:C, [:C, [:C, :C]]]] => "C(C(C(CC)))"
这不是你的问题所说的,但在 D-Side 的评论中,我认为这就是你想要的:基本上,用花括号替换除了外部括号之外的所有括号,并删除外部括号和不需要的格式化。
def to_formatted_string(array)
array.inspect.gsub(/^\[|\]$|[\:\,\s]/,"").gsub("[","(").gsub("]",")")
end
测试 = [ [:C], [:C, [:C]], [:C, [[:C]]], [:C, [:C, [:C]]], [:C, [:C, [:C, :C]]], [:C, [:C, [:C, [:C]]]], [:C, [:C, [:C, [:C, :C]]]] ]
tests.each{|arr| puts "#{arr.inspect} => #{to_formatted_string(arr).inspect}"};false
给予
[:C] => "C"
[:C, [:C]] => "C(C)"
[:C, [[:C]]] => "C((C))"
[:C, [:C, [:C]]] => "C(C(C))"
[:C, [:C, [:C, :C]]] => "C(C(CC))"
[:C, [:C, [:C, [:C]]]] => "C(C(C(C)))"
[:C, [:C, [:C, [:C, :C]]]] => "C(C(C(CC)))"
一旦您注意到第一层和其他层使用不同的规则进行格式化,该算法就很简单了。所以为了清楚起见,我不得不使用两个不同的函数。除此之外,该算法是不言自明的。但即便如此,我还是尽可能清楚地评论了它。
data = [:C], [:C, [:C]], [:C, [[:C]]], [:C, [:C, [:C]]],
[:C, [:C, [:C, :C]]], [:C, [:C, [:C, [:C]]]],
[:C, [:C, [:C, [:C, :C]]]]
def format_step(e) # Expects an array, since all the elements of data are arrays.
e.map do |x| # So for each element, get the following
if x.is_a?(Array)
"(#{ format_step(x) })" # ...then call the same function on it
else
x.to_s # convert to string and return
end
end.join # this way map returns an array of strings here, join them
end
def reformat(data) # This rule is only for the first level and is a bit different
data.map do |element| # For each element of the root array
format_step(element) # do this
end.join(' ') # ..and join the results with spaces
end
puts reformat(data)
您可以根据需要创建字符串,如下所示:
array = [:C], [:C, [:C]], [:C, [[:C]]], [:C, [:C, [:C]]], [:C, [:C, [:C, :C]]], [:C, [:C, [:C, [:C]]]], [:C, [:C, [:C, [:C, :C]]]]
array.inspect.gsub(/(\[|\]|:C)/, '[' => '(', ']' => ')',':C' => 'c')
控制台输出:
2.1.2-p95 :029 > array = [:C], [:C, [:C]], [:C, [[:C]]], [:C, [:C, [:C]]], [:C, [:C, [:C, :C]]], [:C, [:C, [:C, [:C]]]], [:C, [:C, [:C, [:C, :C]]]]
=> [[:C], [:C, [:C]], [:C, [[:C]]], [:C, [:C, [:C]]], [:C, [:C, [:C, :C]]], [:C, [:C, [:C, [:C]]]], [:C, [:C, [:C, [:C, :C]]]]]
2.1.2-p95 :030 > array.inspect.gsub(/(\[|\]|:C)/, '[' => '(', ']' => ')',':C' => 'c')
=> "((c), (c, (c)), (c, ((c))), (c, (c, (c))), (c, (c, (c, c))), (c, (c, (c, (c)))), (c, (c, (c, (c, c)))))"