hp12c
18 January 2012

10のチェスクイズをRubyの配列組み換えメソッドを使わないで解いたよ

Rubyの配列組み換えメソッド群の使い方を覚えるために、前回10のチェスクイズを考えたよ。

10のチェスクイズでRubyの配列組み換えメソッドを覚えよう! - 10 Chess Quizzes to know Recombination Methods of Ruby Array

Array#permutationとかcombinationとかの組み換えメソッド群を使えば、このクイズは簡単に解けるけど、これらのメソッドの使用を禁止したら解くのは結構厄介かなと思ったんだけど、やってみたら簡単だったんで拍子抜けしちゃったよ^ ^;

一応結果だけここに貼っておくね..

def q1(list)
 ret = []
 list.each { |x| list.each { |y| ret << [x, y] } }
 ret
end
def q2(xl, yl)
 ret = []
 xl.each { |x| yl.each { |y| ret << [x, y] } }
 ret
end
def q3(board)
 board.inject(Hash.new{ |h,k| h[k] = [] }) { |h, code| h[code[0]] << code; h }.values
end
def q4(board)
 board.inject(Hash.new{ |h,k| h[k] = [] }) { |h, code| h[code[1]] << code; h }.values
end
def q5(board)
 board.inject(Hash.new{ |h,k| h[k] = [] }) { |h, cols| cols.each { |col| h[col[1]] << col }; h }.values
end
def q6(board)
 col1, col2, col3 = board.map { |col| col.to_enum }
 col1.to_a.size.times.map { [col1.next, col2.next, col3.next] }
end
def q7(board)
 ret = []
 board.each { |a| board.each { |b| ret << [a, b] unless a == b } }
 ret.uniq { |x| x.sort }
end
def q8(pieces)
 ret = []
 pieces.each { |first| pieces.each { |second| ret << [first, second] unless first == second } }
 ret
end
def q9(captures)
 bishops, remains = [], []
 captures.each do |capture|
 if capture.include?(:bishop)
 bishops << capture
 else
 remains << capture
 end
 end
 [bishops, remains]
end
def q10(pieces)
 ret = []
 pieces.each { |first| pieces.each { |second| ret << [first, second] } }
 ret.uniq { |x| x.sort }
end
require "rspec"
describe "Chess Quiz" do
 before do
 @board = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
 @excel_board = [[:a, 1], [:a, 2], [:a, 3], [:b, 1], [:b, 2], [:b, 3], [:c, 1], [:c, 2], [:c, 3]]
 @board_by_col = [[[0, 0], [0, 1], [0, 2]], [[1, 0], [1, 1], [1, 2]], [[2, 0], [2, 1], [2, 2]]]
 @board_by_row = [[[0, 0], [1, 0], [2, 0]], [[0, 1], [1, 1], [2, 1]], [[0, 2], [1, 2], [2, 2]]]
 @col1 = [[0, 0], [0, 1], [0, 2]]
 @col2 = [[1, 0], [1, 1], [1, 2]]
 @col3 = [[2, 0], [2, 1], [2, 2]]
 @piece_combinations = [[[0, 0], [0, 1]], [[0, 0], [0, 2]], [[0, 0], [1, 0]], [[0, 0], [1, 1]], [[0, 0], [1, 2]], [[0, 0], [2, 0]], [[0, 0], [2, 1]], [[0, 0], [2, 2]], [[0, 1], [0, 2]], [[0, 1], [1, 0]], [[0, 1], [1, 1]], [[0, 1], [1, 2]], [[0, 1], [2, 0]], [[0, 1], [2, 1]], [[0, 1], [2, 2]], [[0, 2], [1, 0]], [[0, 2], [1, 1]], [[0, 2], [1, 2]], [[0, 2], [2, 0]], [[0, 2], [2, 1]], [[0, 2], [2, 2]], [[1, 0], [1, 1]], [[1, 0], [1, 2]], [[1, 0], [2, 0]], [[1, 0], [2, 1]], [[1, 0], [2, 2]], [[1, 1], [1, 2]], [[1, 1], [2, 0]], [[1, 1], [2, 1]], [[1, 1], [2, 2]], [[1, 2], [2, 0]], [[1, 2], [2, 1]], [[1, 2], [2, 2]], [[2, 0], [2, 1]], [[2, 0], [2, 2]], [[2, 1], [2, 2]]]
 @pieces = [:rook , :bishop, :queen]
 @captures = [[:rook, :bishop], [:rook, :queen], [:bishop, :rook], [:bishop, :queen], [:queen, :rook], [:queen, :bishop]]
 @bishops = [[:rook, :bishop], [:bishop, :rook], [:bishop, :queen], [:queen, :bishop]]
 @remains = [[:rook, :queen], [:queen, :rook]]
 @moves = [[:rook, :rook], [:rook, :bishop], [:rook, :queen], [:bishop, :bishop], [:bishop, :queen], [:queen, :queen]]
 end
 it "q1" do
 q1([0,1,2]).should eql @board
 end
 it "q2" do
 q2([:a,:b,:c],[1,2,3]).should eql @excel_board
 end
 it "q3" do
 q3(@board).should eql @board_by_col
 end
 it "q4" do
 q4(@board).should eql @board_by_row
 end
 it "q5" do
 q5(@board_by_col).should eql @board_by_row
 end
 it "q6" do
 q6(@board_by_col).should eql @board_by_row
 end
 it "q7" do
 q7(@board).should eql @piece_combinations
 end
 it "q8" do
 q8(@pieces).should eql @captures
 end
 it "q9" do
 bishops, remains = q9(@captures)
 bishops.should eql @bishops
 remains.should eql @remains
 end
 it "q10" do
 q10(@pieces).should eql @moves
 end
end


Please enable JavaScript to view the comments powered by Disqus. blog comments powered by Disqus
ruby_pack8

100円〜で好評発売中!
M'ELBORNE BOOKS



AltStyle によって変換されたページ (->オリジナル) /