A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
24
Task/Caesar-cipher/Groovy/caesar-cipher.groovy
Normal file
24
Task/Caesar-cipher/Groovy/caesar-cipher.groovy
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
def caeserEncode(int cipherKey, String text) {
|
||||
def builder = new StringBuilder()
|
||||
text.each { character ->
|
||||
int ch = character[0] as char
|
||||
switch(ch) {
|
||||
case 'a'..'z': ch = ((ch - 97 + cipherKey) % 26 + 97); break
|
||||
case 'A'..'Z': ch = ((ch - 65 + cipherKey) % 26 + 65); break
|
||||
}
|
||||
builder.append(ch as char)
|
||||
}
|
||||
builder.toString()
|
||||
}
|
||||
def caeserDecode(int cipherKey, String text) { caeserEncode(26 - cipherKey, text) }
|
||||
|
||||
def plainText = "The Quick Brown Fox jumped over the lazy dog"
|
||||
def cipherKey = 12
|
||||
def cipherText = caeserEncode(cipherKey, plainText)
|
||||
def decodedText = caeserDecode(cipherKey, cipherText)
|
||||
|
||||
println "plainText: $plainText"
|
||||
println "cypherText($cipherKey): $cipherText"
|
||||
println "decodedText($cipherKey): $decodedText"
|
||||
|
||||
assert plainText == decodedText
|
||||
Loading…
Add table
Add a link
Reference in a new issue