10分間でコーディングにチャレンジ

http://ameblo.jp/programming/entry-10001721422.html

やってみた。ネタバレになるので「続きを読む」で。
仕様がハッキリしていたので、テストファーストで書いた。テストも含めて6分半。

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CardsTest {
    
    private Cards cards;

    /**
     * テストを初期化する。
     * 
     * @throws Exception 例外が発生した場合
     */
    @Before
    public void setUp() throws Exception {
	cards = new Cards();
    }
    
    /**
     * テストの情報を破棄する。
     * 
     * @throws Exception 例外が発生した場合
     */
    @After
    public void tearDown() throws Exception {
	cards = null;
    }

    /**
     * 例0-1
     * 
     * @throws Exception 例外が発生した場合
     */
    @Test
    public void test1() throws Exception {
	String[] result = cards.deal(3, "123123123");
	assertThat(result.length, is(3));
	assertThat(result[0], is("111"));
	assertThat(result[1], is("222"));
	assertThat(result[2], is("333"));
    }

    /**
     * 例0-2
     * 
     * @throws Exception 例外が発生した場合
     */
    @Test
    public void test2() throws Exception {
	String[] result = cards.deal(4, "123123123");
	assertThat(result.length, is(4));
	assertThat(result[0], is("12"));
	assertThat(result[1], is("23"));
	assertThat(result[2], is("31"));
	assertThat(result[3], is("12"));
    }
    
    /**
     * 例1
     * 
     * @throws Exception 例外が発生した場合
     */
    @Test
    public void test3() throws Exception {
	String[] result = cards.deal(6, "012345012345012345");
	assertThat(result.length, is(6));
	assertThat(result[0], is("000"));
	assertThat(result[1], is("111"));
	assertThat(result[2], is("222"));
	assertThat(result[3], is("333"));
	assertThat(result[4], is("444"));
	assertThat(result[5], is("555"));
    }
    
    /**
     * 例2
     * 
     * @throws Exception 例外が発生した場合
     */
    @Test
    public void test4() throws Exception {
	String[] result = cards.deal(4, "111122223333");
	assertThat(result.length, is(4));
	assertThat(result[0], is("123"));
	assertThat(result[1], is("123"));
	assertThat(result[2], is("123"));
	assertThat(result[3], is("123"));
    }
    
    /**
     * 例3
     * 
     * @throws Exception 例外が発生した場合
     */
    @Test
    public void test5() throws Exception {
	String[] result = cards.deal(1, "012345012345012345");
	assertThat(result.length, is(1));
	assertThat(result[0], is("012345012345012345"));
    }
    
    /**
     * 例4
     * 
     * @throws Exception 例外が発生した場合
     */
    @Test
    public void test6() throws Exception {
	String[] result = cards.deal(6, "01234");
	assertThat(result.length, is(6));
	assertThat(result[0], is(""));
	assertThat(result[1], is(""));
	assertThat(result[2], is(""));
	assertThat(result[3], is(""));
	assertThat(result[4], is(""));
	assertThat(result[5], is(""));
    }
    
    /**
     * 例5
     * 
     * @throws Exception 例外が発生した場合
     */
    @Test
    public void test7() throws Exception {
	String[] result = cards.deal(2, "");
	assertThat(result.length, is(2));
	assertThat(result[0], is(""));
	assertThat(result[1], is(""));
    }
}
public class Cards {
    
    public String[] deal(int numPlayers, String deck) {
	String[] result = new String[numPlayers];
	for (int i = 0; i < numPlayers; i++) {
	    result[i] = "";
	}
	
	int total = deck.length();
	int use = total - (total % numPlayers);
	
	for (int i = 0; i < use; i++) {
	    result[i % numPlayers] += deck.charAt(i);
	}
	return result;
    }
}

あんまエレガントじゃねぇなぁー…。