uehaj's blog

Grな日々 - GroovyとかGrailsとかElmとかRustとかHaskellとかReactとかFregeとかJavaとか -

10分でコーディング

10分でコーディングについてのゲンゾウさんのところを見ていざ。

class Cards {

    String[] deal(int numParameters, String initialCards) {
        initialCards = initialCards.substring(0, initialCards.length() - initialCards.length() % numParameters)

        String[] result = new String[numParameters];
        result.eachWithIndex { it, idx ->
            result[idx] = ""
        }
        initialCards.eachWithIndex { it, idx ->
            result[idx % numParameters] += it
        }
        result
    }

    static void main(args) {
        def c = new Cards()

        assert c.deal(6, "012345012345012345") == ["000", "111", "222", "333", "444", "555" ]
        assert c.deal(4, "111122223333") == ["123", "123", "123", "123" ]
        assert c.deal(1, "012345012345012345") == ["012345012345012345" ]
        assert c.deal(6, "01234") == ["","","","","",""]
        assert c.deal(2, "") == ["", ""]
    }
}

ちきしお。15分かかっちまったい。端数のときがなければ5分なのだが。