ruby 嵌套数组的解决方案为Boggle Board建模

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 嵌套数组的解决方案为Boggle Board建模相关的知识,希望对你有一定的参考价值。

# Solution for Challenge: A Nested Array to Model a Boggle Board. Started 2014-01-29T21:58:07+00:00

@boggle_board = [["b", "r", "a", "e"],
                 ["i", "o", "d", "t"],
                 ["e", "c", "l", "r"],
                 ["t", "a", "k", "e"]]


def create_word(board, *coords)
  coords.map { |coord| board[coord.first][coord.last]}.join("")
end

def get_row(row)
  @boggle_board[row]
end

def get_col(col)
  @boggle_board.map {|row|  row[col]}
end


#1) Access multiple elements of a nested array
puts create_word(@boggle_board, [2,2], [1,1], [2,1], [3,2], [3,3], [2,3])  #=> returns "locker"
puts create_word(@boggle_board, [2,1], [3,1], [3,2], [3,3])  #=> returns "cake"

# 2) Write a method that takes a row number and returns all the elements in the row.
p get_row(0) #=>  ["b", "r", "a", "e"]
p get_row(2) #=>  ["e", "c", "l", "r"]

#3) Write a method that takes a column number and returns all the elements in the column.
p get_col(0) #=> ["b", "i", "e", "t"]
p get_col(3) #=> ["e", "t", "r", "e"]

# Not much refactoring that I can see. This seemed unnecessarily confusing with the need to create the 
# global boggle board not matching the format of #create_word. It seems like maybe it's a trick to make 
# us think, but it's not really in line with how DBC presents challenges thus far, even when there are
# 'tricks'.

以上是关于ruby 嵌套数组的解决方案为Boggle Board建模的主要内容,如果未能解决你的问题,请参考以下文章

ruby 使用类从Boggle Board创建检索Boggle字

ruby Boggle Solver挑战赛

ruby 阶段0单位2周1boggle班级挑战

ruby 阶段0单位2周1boggle班级挑战

ruby 阶段0单位2周1boggle班级挑战

ruby 阶段0单位2周1boggle班级挑战