new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
23
Task/Caesar_cipher/Ruby/caesar_cipher.rb
Normal file
23
Task/Caesar_cipher/Ruby/caesar_cipher.rb
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
@atoz = Hash.new do |hash, key|
|
||||
str = ('A'..'Z').to_a.rotate(key).join("")
|
||||
hash[key] = (str << str.downcase)
|
||||
end
|
||||
|
||||
def encrypt(key, plaintext)
|
||||
(1..25) === key or raise ArgumentError, "key not in 1..25"
|
||||
plaintext.tr(@atoz[0], @atoz[key])
|
||||
end
|
||||
|
||||
def decrypt(key, ciphertext)
|
||||
(1..25) === key or raise ArgumentError, "key not in 1..25"
|
||||
ciphertext.tr(@atoz[key], @atoz[0])
|
||||
end
|
||||
|
||||
original = "THEYBROKEOURCIPHEREVERYONECANREADTHIS"
|
||||
en = encrypt(3, original)
|
||||
de = decrypt(3, en)
|
||||
|
||||
[original, en, de].each {|e| puts e}
|
||||
|
||||
puts 'OK' if
|
||||
(1..25).all? {|k| original == decrypt(k, encrypt(k, original))}
|
||||
Loading…
Add table
Add a link
Reference in a new issue