Created
October 6, 2016 11:03
-
-
Save erbesharat/a0b717918729f09f1a7b5a69eafbc41c to your computer and use it in GitHub Desktop.
Caesar cipher - Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def caesar(text,key) | |
asc = text.chars.map { |c| c.ord} | |
shifted = asc.map { |c| c + key.to_i} | |
return shifted.map { |c| c.chr }.join | |
end | |
puts caesar("Erfan", 5) # Should be --> Jwkfs | |
puts caesar("Ruby", 3) # Should be --> Uxe| | |
puts caesar("Rails", 2) # Should be --> Tcknu | |
puts caesar("Golang", 8) # Should be --> Owtivo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment