続・topcoderの道1をといてみる

2019-02-21Groovy

topcoderの道1をといてみる | ゲンゾウ用ポストイット の続き。

Groovyで文字処理を書くときの心得 - Grな日々(uehajの日記) で以下のようなエントリが。

先のエントリと、ほかにいくつかGroovyでパズルっぽいコードを書いてみたんだけど、あれだね。Groovyで文字の絡む処理はC言語的にやったら負けだね。

たしかに。効率もだけど、それ以上により簡潔に書きたい。

ちょっと直してみる。

class CCipher {
    final def ALPHA_LIST = 'A'..'Z'

    String decode(String ciphertest, int shift) {
        ciphertest.collect {
            ALPHA_LIST[ALPHA_LIST.indexOf(it) - shift]
        }.join()
    }
}

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

一行しか変わってないけどwもっとうまい書き方ないかな。

2019-02-21Groovy