尝试在 Ruby 中创建一个方法,以使用递归返回数组的所有可能组合(不是内置的组合/排列等方法)

发布于 2025-01-10 22:10:14 字数 1455 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2025-01-17 22:10:14

您可以按如下方式进行操作。

def recurse(first, *rest)
  return [[first], rest] if rest.empty?
  recurse(*rest).flat_map { |a| [a, [first] + a] }
end
​arr = [1,2,3,4,5]
a = recurse(*arr)
  #=> [[5], [1, 5], [2, 5], [1, 2, 5], [3, 5], [1, 3, 5], [2, 3, 5],
  #    [1, 2, 3, 5], [4, 5], [1, 4, 5], [2, 4, 5], [1, 2, 4, 5], [3, 4, 5],
  #    [1, 3, 4, 5], [2, 3, 4, 5], [1, 2, 3, 4, 5], [], [1], [2], [1, 2],
  #    [3], [1, 3], [2, 3], [1, 2, 3], [4], [1, 4], [2, 4], [1, 2, 4],
  #    [3, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]]
a.sort
  #=> [[],
  #    [1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5],
  #    [1, 2, 3, 5], [1, 2, 4], [1, 2, 4, 5], [1, 2, 5], [1, 3],
  #    [1, 3, 4], [1, 3, 4, 5], [1, 3, 5], [1, 4], [1, 4, 5], [1, 5],
  #    [2], [2, 3], [2, 3, 4], [2, 3, 4, 5], [2, 3, 5], [2, 4], [2, 4, 5], [2, 5],
  #    [3], [3, 4], [3, 4, 5], [3, 5],
  #    [4], [4, 5],
  #    [5]]

请参阅Enumerable#flat_map



理解递归的最佳方法是在代码中添加一些 put 语句,并在视觉上分隔方法的每个实例。这可以在这里完成,如下所示。

INDENT = 6
def indent; $col += INDENT; end
def undent; $col -= INDENT; end
def pu(s); puts "#{" "*$col}#{s}"; end 
def puhline; pu('-'*(65-$col)); end
def recurse(first, *rest)
  indent
  puhline    
  pu "recurse called with arguments first = #{first}, rest = #{rest}"
  if rest.empty?
    c =  [[first], rest]
    pu "returning #{c}"
    puhline    
    undent
    return c
  end  
  pu "calling recurse(#{rest})"
  a = recurse(*rest)
  pu "#{a} is returned"
  b = a.flat_map { |a| [a, [first] + a] }
  pu "returning a.flat_map { |a| [a, [#{first}] + a] } = #{b}"
  puhline    
  undent
  b
end
$col = -INDENT
recurse(1,2,3,4)

显示以下内容。请注意,该方法的每个实例执行的计算在下面垂直对齐。

-----------------------------------------------------------------
recurse called with arguments first = 1, rest = [2, 3, 4]
calling recurse([2, 3, 4])
      -----------------------------------------------------------
      recurse called with arguments first = 2, rest = [3, 4]
      calling recurse([3, 4])
            -----------------------------------------------------
            recurse called with arguments first = 3, rest = [4]
            calling recurse([4])
                  -----------------------------------------------
                  recurse called with arguments first = 4, rest = []
                  returning [[4], []]
                  -----------------------------------------------
            a = [[4], []] is returned
            returning a.flat_map { |a| [a, [3] + a] } = [[4], [3, 4], [], [3]]
            -----------------------------------------------------
      a = [[4], [3, 4], [], [3]] is returned
      returning a.flat_map { |a| [a, [2] + a] } = [[4], [2, 4], [3, 4], [2, 3, 4], [], [2], [3], [2, 3]]
      -----------------------------------------------------------
a = [[4], [2, 4], [3, 4], [2, 3, 4], [], [2], [3], [2, 3]] is returned
returning a.flat_map { |a| [a, [1] + a] } = [[4], [1, 4], [2, 4], [1, 2, 4], [3, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4], [], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
-----------------------------------------------------------------

You can do that as follows.

def recurse(first, *rest)
  return [[first], rest] if rest.empty?
  recurse(*rest).flat_map { |a| [a, [first] + a] }
end
​arr = [1,2,3,4,5]
a = recurse(*arr)
  #=> [[5], [1, 5], [2, 5], [1, 2, 5], [3, 5], [1, 3, 5], [2, 3, 5],
  #    [1, 2, 3, 5], [4, 5], [1, 4, 5], [2, 4, 5], [1, 2, 4, 5], [3, 4, 5],
  #    [1, 3, 4, 5], [2, 3, 4, 5], [1, 2, 3, 4, 5], [], [1], [2], [1, 2],
  #    [3], [1, 3], [2, 3], [1, 2, 3], [4], [1, 4], [2, 4], [1, 2, 4],
  #    [3, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]]
a.sort
  #=> [[],
  #    [1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5],
  #    [1, 2, 3, 5], [1, 2, 4], [1, 2, 4, 5], [1, 2, 5], [1, 3],
  #    [1, 3, 4], [1, 3, 4, 5], [1, 3, 5], [1, 4], [1, 4, 5], [1, 5],
  #    [2], [2, 3], [2, 3, 4], [2, 3, 4, 5], [2, 3, 5], [2, 4], [2, 4, 5], [2, 5],
  #    [3], [3, 4], [3, 4, 5], [3, 5],
  #    [4], [4, 5],
  #    [5]]

See Enumerable#flat_map.



The best way to understand recursion is to add some puts statements to the code and to visually separate each instance of the method. That could be done here as follows.

INDENT = 6
def indent; $col += INDENT; end
def undent; $col -= INDENT; end
def pu(s); puts "#{" "*$col}#{s}"; end 
def puhline; pu('-'*(65-$col)); end
def recurse(first, *rest)
  indent
  puhline    
  pu "recurse called with arguments first = #{first}, rest = #{rest}"
  if rest.empty?
    c =  [[first], rest]
    pu "returning #{c}"
    puhline    
    undent
    return c
  end  
  pu "calling recurse(#{rest})"
  a = recurse(*rest)
  pu "#{a} is returned"
  b = a.flat_map { |a| [a, [first] + a] }
  pu "returning a.flat_map { |a| [a, [#{first}] + a] } = #{b}"
  puhline    
  undent
  b
end
$col = -INDENT
recurse(1,2,3,4)

The following is displayed. Note that calculations performed by each instance of the method are vertically aligned below.

-----------------------------------------------------------------
recurse called with arguments first = 1, rest = [2, 3, 4]
calling recurse([2, 3, 4])
      -----------------------------------------------------------
      recurse called with arguments first = 2, rest = [3, 4]
      calling recurse([3, 4])
            -----------------------------------------------------
            recurse called with arguments first = 3, rest = [4]
            calling recurse([4])
                  -----------------------------------------------
                  recurse called with arguments first = 4, rest = []
                  returning [[4], []]
                  -----------------------------------------------
            a = [[4], []] is returned
            returning a.flat_map { |a| [a, [3] + a] } = [[4], [3, 4], [], [3]]
            -----------------------------------------------------
      a = [[4], [3, 4], [], [3]] is returned
      returning a.flat_map { |a| [a, [2] + a] } = [[4], [2, 4], [3, 4], [2, 3, 4], [], [2], [3], [2, 3]]
      -----------------------------------------------------------
a = [[4], [2, 4], [3, 4], [2, 3, 4], [], [2], [3], [2, 3]] is returned
returning a.flat_map { |a| [a, [1] + a] } = [[4], [1, 4], [2, 4], [1, 2, 4], [3, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4], [], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
-----------------------------------------------------------------
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文