Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby:访问数组';数组中的数组_Ruby_Arrays_Recursion - Fatal编程技术网

Ruby:访问数组';数组中的数组

Ruby:访问数组';数组中的数组,ruby,arrays,recursion,Ruby,Arrays,Recursion,这是一个需要通过以下代码解决的锦标赛: tourney = [ [ [ ["Armando", "P"], ["Dave", "S"] ], [ ["Richard", "R"], ["Michael", "S"] ] ], [ [ ["Allen", "S"], ["Omer", "P"] ], [ ["David E.", "R"], ["Richard X.", "P"] ] ], ] 现在的基本情况当然是 def self.w

这是一个需要通过以下代码解决的锦标赛:

tourney = [
  [
    [ ["Armando", "P"], ["Dave", "S"] ],      
    [ ["Richard", "R"], ["Michael", "S"] ]
  ],
  [
    [ ["Allen", "S"], ["Omer", "P"] ],
    [ ["David E.", "R"], ["Richard X.", "P"] ]
  ],
]
现在的基本情况当然是

def self.winner(player1, player2)
  p1c = player1.last.downcase
  p2c = player2.last.downcase
  unless RPS.include?(p1c) && RPS.include?(p2c)
    raise NoSuchStrategyError, "Strategy must be one of R,P,S"
  end
  if p1c!=p2c 
    case p1c
      when "r" 
        p2c=="s" ? player1 : player2
      when "p" 
        p2c=="r" ? player1 : player2
      when "s" 
        p2c=="p" ? player1 : player2
    end
   else 
     player1
  end  
end
但是在else中,我如何才能得到第一个数组集,即包含“Armando”和“Dave”的数组,检查谁获胜,然后继续,它需要在任意大小的数组上使用。 是否有一种方法可以通过这些元素过滤胜利;即。, 在递归的第一个实例上,它应该返回:

def self.tournament_winner(tournament)
  if tournament.size == 2
    self.winner(tournament[0], tournament[1])
  else
    #WORK HERE
  end
end

如果我的措辞不好,我很抱歉。我试图解决这个问题已经有一段时间了,但时间已经很晚了,我缺乏判断力。

基本函数是不正确的。首先,让我们假设
锦标赛
数组的形式良好,总是有成对的玩家或数组。如果
锦标赛[0]
是一个简单的玩家定义,那么玩家1就是
锦标赛[0]
。但是,如果
锦标赛[0]
是一组玩家,那么玩家1就是
锦标赛[0]
的赢家-这是您的递归。对玩家2重复同样的逻辑,并返回玩家1对玩家2的胜者

现在,问题变成了“我如何确定
锦标赛[0]
是否是一个简单数组?”让我们用交互式shell的简单方法来实现这一点(我更喜欢
pry
而不是默认的
irb
),使用第二个示例:

tourney = [
  [
    ["Dave", "S"],      
    [ ["Richard", "R"], ["Michael", "S"] ]
  ],
  [
    [ ["Allen", "S"], ["Omer", "P"] ],
    [ ["David E.", "R"], ["Richard X.", "P"] ]
  ],
]
因此,我们可以测试
.class
方法来检查它是否是一个数组,从而得出以下解决方案:

[3] pry(main)> tourney.class
=> Array
[4] pry(main)> tourney[0].class
=> Array
[5] pry(main)> tourney[0][0].class
=> Array
[6] pry(main)> tourney[0][0][0].class
=> String
请注意,我需要使用
锦标赛[0][0]
来阻止我们递归一步太深。事实上,我不喜欢这样,所以让我们尝试另一种方式,使用上面建议的方法。如果展平的数组与未展平的数组大小相同,则我们已到达树的底部:

def self.tournament_winner(tournament)
  if tournament[0][0].class == Array
    player1 = self.tournament_winner(tournament[0])
  else
    player1 = tournament[0]
  end
  if tournament[1][0].class == Array
    player2 = tournament_winner(tournament[1])
  else
    player2 = tournament[1]
  end

  self.winner(player1, player2)
end
我使用
flatten(1)
作为次要优化。现在:

def self.tournament_winner(tournament)
  if tournament[0].flatten(1).size != tournament[0].size
    player1 = self.tournament_winner(tournament[0])
  else
    player1 = tournament[0]
  end
  if tournament[1].flatten(1).size != tournament[1].size
    player2 = tournament_winner(tournament[1])
  else
    player2 = tournament[1]
  end

  self.winner(player1, player2)
end

上述函数的进一步Ruby化可能是

[7] pry(main)> puts "And the winner is #{tournament_winner(tourney)[0]}!"
=> And the winner is Richard!
或使用烘干机:

def self.tournament_winner(tournament)
  player1 = if tournament[0].flatten(1).size != tournament[0].size
              self.tournament_winner(tournament[0])
            else
              tournament[0]
            end
  player2 = if tournament[1].flatten(1).size != tournament[1].size
              tournament_winner(tournament[1])
            else
              tournament[1]
            end

  self.winner(player1, player2)
end

看起来像EdX或Coursera运动。我的建议是在某种程度上把它展平。EdX是的,我在这方面已经有一段时间了。我学习了一门基于递归和允许的语言的课程。首先,rest帮助很大,但我不知道如何做到这一点。这是一个使用递归的规范谢谢你帮助我理解这里的递归功能我真的很挣扎你花了多长时间才想出这个算法?
def self.get_competitor(branch)
  if branch.flatten(1).size != branch.size
    self.tournament_winner(branch)
  else
    branch
  end
end

def self.tournament_winner(tournament)
  player1 = self.get_competitor(tournament[0])
  player2 = self.get_competitor(tournament[1])

  self.winner(player1, player2)
end