Sha256: ded8a47a8fab134144ee2adef3457fff3246a54c3a0c3f5d215ebb96f2c15fde

Contents?: true

Size: 962 Bytes

Versions: 75

Compression:

Stored size: 962 Bytes

Contents

public class RotationalCipher {
    private int key

    public RotationalCipher(int key) {
        this.key = key
    }

    public String rotate(String cipherText) {
        StringBuilder ret = new StringBuilder()
        cipherText.each { c -> ret.append(convert(c)) }
        ret.toString()
    }

    /*
     * 'a' == 97
     * 'z' == 122
     * 'A' == 65
     * 'Z' == 90
     * For reference, see http://www.asciitable.com
     */
    private char convert(String s) {
        char c = s as Character
        int cInt = c as Integer
        int convertedInt = cInt + key
        if(c.isLetter()) {
            if(c.isLowerCase() && convertedInt > 122) {
                (96 + (convertedInt % 122)) as Character
            } else if (c.isUpperCase() && convertedInt > 90){
                (64 + (convertedInt % 90)) as Character
            } else {
                convertedInt as Character
            }
        } else {
            c
        }

    }
}

Version data entries

75 entries across 75 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.179 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.178 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.177 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.176 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.175 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.174 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.173 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.172 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.171 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.170 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.169 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.167 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.166 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.165 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.164 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.163 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.162 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.161 tracks/groovy/exercises/rotational-cipher/Example.groovy
trackler-2.2.1.160 tracks/groovy/exercises/rotational-cipher/Example.groovy