Created
July 11, 2019 17:26
-
-
Save marcoranieri/1c1244c132a9ce6e56dbbc8764068efd to your computer and use it in GitHub Desktop.
Livecode - Acronymize with map & Encrypt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def acronymize(sentence) | |
return "" if sentence.empty? | |
sentence.split.map { |word| word[0].upcase}.join | |
end | |
puts acronymize("be right back") | |
# puts acronymize("") == "" | |
describe "#acronymize" do | |
it "return an empty string when passed an empty string" do | |
actual = acronymize("") | |
expected = "" | |
expect(actual).to eq(expected) | |
end | |
it "return String object when passed a string" do | |
actual = acronymize("Hello World") | |
expected = "HW" | |
expect(actual.class).to eq(expected.class) | |
end | |
it "return BRB when passed Be Right Back" do | |
actual = acronymize("Be Right Back") | |
expected = "BRB" | |
expect(actual).to eq(expected) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def encrypt(sentence) | |
alphabet = ("A".."Z").to_a | |
shift_alphabet = ("X".."Z").to_a + ("A".."W").to_a | |
sentence.chars.map do |char| | |
# "#{index}. #{char}" | |
if char == " " | |
char | |
else | |
char = alphabet[alphabet.index(char) - 3] | |
char | |
end | |
end.join | |
end | |
puts encrypt("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG") == "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" | |
describe "#encrypt" do | |
it "return QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD an empty string when passed THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG" do | |
actual = encrypt("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG") | |
expected = "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD" | |
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