-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_cipher.rb
More file actions
executable file
·39 lines (31 loc) · 1.45 KB
/
caesar_cipher.rb
File metadata and controls
executable file
·39 lines (31 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env ruby
# frozen_string_literal: true
# In cryptography is one of the simplest and most widely known encryption techniques.
# It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet.
# For example, with a left shift of 3, D would be replaced by A, E would become B, and so on.
# Implement a Caesar cipher that takes in a string and the shift factor and then outputs the modified string using a right shift:
# > caesar_cipher("What a string!", 5)
# => "Bmfy f xywnsl!"
# Quick Tips:
# You will need to remember how to convert a string into a number.
# Don’t forget to wrap from z to a.
# Don’t forget to keep the same case.
# The Wikipedia quote discusses a Caesar cipher using a left shift.
def cipher(str, delta)
# establish the appropriate translation for any alphabetical characters
ref = ('a'..'z').each_with_object({}) do |c, h|
h[c] = ('a'.ord + (c[0].ord - 'a'.ord + delta) % 26).chr
h[c.upcase] = h[c].upcase
end
# encrypt the string
str.chars.map { |c| ref.fetch(c, c) }.join
end
def verify(id, str, delta, expected)
puts "Test #{id}: #{cipher(str, delta) == expected ? 'PASS' : 'FAIL'} cipher('#{str}', #{delta}) == '#{expected}'"
end
t = 0
verify(t += 1, 'abc', 1, 'bcd');
verify(t += 1, 'abc1', 1, 'bcd1');
verify(t += 1, 'wxYz', 3, 'zaBc');
verify(t += 1, 'wx Yz!', 3, 'za Bc!');
verify(t += 1, 'What a string!', 5, 'Bmfy f xywnsl!');