some solutions for projects on odin project

This commit is contained in:
2023-05-24 15:34:43 -05:00
commit 793e2c2c5f
10 changed files with 273 additions and 0 deletions

22
caesar_cipher.rb Normal file
View File

@@ -0,0 +1,22 @@
### CAESAR CIPHER
Alphabet = 'abcdefghijklmnopqrstuvwxyz'
def caesar_cipher(input, shift)
unless shift.is_a? Numeric and input.is_a? String
raise TypeError("Receive invalid input")
end
input.downcase!
array_from_input = Array.new(input.length)
result = ''
for i in 0..(input.length - 1)
start = Alphabet.index(input[i])
after_shift = start ? Alphabet[(start + shift % 26)] : -1
result += after_shift == -1 ? input[i] : Alphabet[after_shift]
end
result
end
print caesar_cipher("Doing stuff with Ruby", 5)