如何将n-D数组转换为ruby中的字符串?

时间:2015-06-08 08:21:28

标签: ruby-on-rails arrays ruby string random

我有一个多维数组:

[
  [: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)))"

我尝试使用扁平和连接,但我没有得到 期望的结果。我希望它带括号,以便它可以很容易 鉴定。如果我使用flattenconcat,我会获得CCCCCCCCCCCCC。一世 想要用圆括号。

3 个答案:

答案 0 :(得分:3)

一旦您注意到第一级和其他级别使用不同的规则格式化,该算法就很简单。所以为了清楚起见,我必须使用两种不同的功能。除此之外,该算法非常明显。但即使如此,我也评论它尽可能清楚。

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)

答案 1 :(得分:0)

假设要求如下:

[: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)))"

答案 2 :(得分:0)

您可以按照以下要求创建字符串:

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)))))"