topcoderの道1をといてみる

topcoderの道1 | プログラミングに自信があるやつこい!! を説いてみた。

Groovyは本当にプログラム組むのも実行するのも楽。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class CCipher {
    final def ALPHA_LIST = 'A'..'Z'
    String decode(String ciphertest, int shift){
        ciphertest.collect{
            ALPHA_LIST[(char)it - (char)'A' - shift]
        }.join()
    }
}

new CCipher().with {
    assert decode("VQREQFGT", 2) == "TOPCODER"
    assert decode("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 10) == "QRSTUVWXYZABCDEFGHIJKLMNOP"
    assert decode("TOPCODER", 0) == "TOPCODER"
}