Created
May 4, 2021 02:32
-
-
Save taboularasa/f6912f9827ca543ba27b1d144450f67a to your computer and use it in GitHub Desktop.
implement caesar cipher
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 transpose(message, mapping) | |
message.chars.map {|e| mapping[e]}.join | |
end | |
puts "encode or decode?" | |
selected_action = gets.chomp | |
puts "what is the key?" | |
key = gets.chomp | |
puts "what is your message?" | |
message = gets.chomp | |
plain = ('a'..'z').to_a | |
rotated = plain.rotate(key.to_i) | |
encode_mapping = plain.zip(rotated).to_h.tap {|h| h[" "] = " "} | |
actions = { | |
"encode" => encode_mapping, | |
"decode" => encode_mapping.invert | |
} | |
transposed_message = transpose(message, actions[selected_action]) | |
puts "this is your modified message: #{transposed_message}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment