【问题标题】:How to make n-D array into a string in ruby? [closed]如何将 n-D 数组转换为 ruby​​ 中的字符串? [关闭]
【发布时间】:2015-06-08 08:21:27
【问题描述】:

我有一个多维数组:

[
  [: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 和 concatenate,但我没有得到 期望的结果。我想要带括号的,这样可以很容易 确定。如果我使用flattenconcat,我会得到CCCCCCCCCCCCC。一世 想要用括号括起来。

【问题讨论】:

  • 你能解释一下你的需求逻辑吗?为什么有些 C 在输出中应该有括号并不明显。一些更简单的例子会有所帮助。
  • chemical=Array.new(l,[:C]) 该化学物质是 N-d 阵列。它附有一些分支。您在第二个 C 中看到数组中有一个数组。所以我想用括号来区分它。 @MaxWilliams
  • 那里有三层嵌套,所以说“数组中有一个数组”并没有多大帮助。您能否为您的输入问题添加一些简单的示例,这些示例不会产生括号内的输出,然后是它的更改版本?假设没有人对你的化学作业或任何内容一无所知。
  • 如果没有带有数组的数组,那么我可以简单地展平或连接。例如:[[:C, [":Br"]], [:C, [":Br"]], [:C, [":Br"]]],输出应该是 CBrCBrCBr。但主要问题是带有数组的数组。 @MaxWilliams
  • 嗯,整个就是一个数组,里面有3个数组,然后他们里面都有另外一个数组。所以,这里有三层嵌套数组。所以数组中有很多数组,但你说它们不应该有括号。请在您的问题中非常清楚地指定您的要求规则,因为只是说“数组中的数组”没有帮助。

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


【解决方案1】:

一旦您注意到第一级和其他级别的格式不同,算法就很简单了。所以为了清楚起见,我不得不使用两个不同的功能。除此之外,该算法非常不言自明。但即便如此,我还是尽可能清楚地评论它。

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)

【讨论】:

  • 非常感谢第二个功能确实有效。我对映射了解不多。仍然在学习。 :D
  • @PriyaGupta 很好,他们一起工作。两条规则,两种功能。
【解决方案2】:

假设要求是这样的:

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

【讨论】:

  • 您将这些视为字符串。这不起作用。
【解决方案3】:

您可以根据需要创建字符串,如下所示:

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

【讨论】:

    猜你喜欢
    • 2018-06-20
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2013-09-17
    相关资源
    最近更新 更多