loading...

re: Daily Challenge #42 - Caesar Cipher VIEW POST

FULL DISCUSSION
 

ruby <3

def decrypt_caesar_cipher(s, shift)
  decrypter = (?a..?z).cycle.lazy.then { @1.zip(@1.drop(26 - shift)).take(26).to_h }
  decrypter.default_proc = -> { @1.fetch(@2.downcase, nil)&.upcase || @2 }
  s.each_char.inject('') { @1 + decrypter[@2] }
end

def brute_force_caesar_cipher(s)
  0...26 |> to_h { [@1, decrypt_caesar_cipher(s, @1)] }
end

p decrypt_caesar_cipher('dwwdfn iurp wkh zrrgv dw gdzq', 3)
pp brute_force_caesar_cipher('dwwdfn iurp wkh zrrgv dw gdzq')
code of conduct - report abuse