Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created January 25, 2019 02:17
Show Gist options
  • Save caioertai/3178f1879b002fd00cc3af03c8c32d4a to your computer and use it in GitHub Desktop.
Save caioertai/3178f1879b002fd00cc3af03c8c32d4a to your computer and use it in GitHub Desktop.
def encrypter(sentence)
# Create an array of the alphabet
alphabet = ("A".."Z").to_a
# Break all the sentences
sentence_letters = sentence.split("")
# Set our encription key
key = -3
# Use map to create one sentence
sentence_letters.map do |letter|
letter_index = alphabet.index(letter)
letter_index.nil? ? letter : alphabet[(letter_index + key) % alphabet.size]
# Join back
end.join
end
puts encrypter("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG")
require_relative './encrypter.rb'
describe '#encrypter' do
it 'returns an encrypted sentence' do
sentence = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
expected = "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD"
actual = encrypter(sentence)
expect(actual).to eq(expected)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment