カード配り問題
時間は気にせず挑戦してみた。
require "test/unit" class TestCards < Test::Unit::TestCase def setup @cards = Cards.new end def test_deal assert_equal(["111", "222", "333"], @cards.deal(3, "123123123")) assert_equal(["12","23","31","12"], @cards.deal(4, "123123123")) assert_equal(["000", "111", "222", "333", "444", "555"], @cards.deal(6, "012345012345012345")) assert_equal(["123", "123", "123", "123"], @cards.deal(4, "111122223333")) assert_equal(["012345012345012345"], @cards.deal(1, "012345012345012345")) assert_equal(["", "", "", "", "", ""], @cards.deal(6, "01234")) assert_equal(["", ""], @cards.deal(2, "")) end end class Cards def deal(numPlayers, deck) @hands = Array.new(numPlayers) {|i| ""} deck = deck[0...deck.size - (deck.size % numPlayers )] deck.each_char.with_index do |card, i| @hands[i % numPlayers] << card end @hands end end