Skip to content

Instantly share code, notes, and snippets.

@taboularasa
Created May 4, 2021 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taboularasa/f6912f9827ca543ba27b1d144450f67a to your computer and use it in GitHub Desktop.
Save taboularasa/f6912f9827ca543ba27b1d144450f67a to your computer and use it in GitHub Desktop.
implement caesar cipher
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