Created
January 23, 2025 21:06
-
-
Save kissu/e747b6582f78ce565399ada30be42870 to your computer and use it in GitHub Desktop.
Yield day with plenty of iterators + RSpec
This file contains hidden or 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 acronym(sentence) | |
return sentence.split.map { |word| word[0] }.join.upcase | |
end |
This file contains hidden or 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
require_relative '../acronym.rb' | |
describe "#acronym" do | |
it "returns an empty string when passed an empty string" do | |
actual = acronym("") | |
expected = "" | |
expect(actual).to eq(expected) # passes if `actual == expected` | |
end | |
it "returns the acronym on downcased sentences" do | |
actual = acronym("working from home") | |
expected = "WFH" | |
expect(actual).to eq(expected) | |
end | |
it "returns the acronym on upcased sentences" do | |
actual = acronym("AWAY FROM KEYBOARD") | |
expected = "AFK" | |
expect(actual).to eq(expected) | |
end | |
end |
This file contains hidden or 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(text) | |
# we need array of 26 letters from the alphabet (capitalized) | |
# split the unencrpyted text in letters | |
# iterate each letter (check its a letter) | |
# take letter and shift it by 3 letters backwards | |
# join the array | |
alphabet = ("A".."Z").to_a | |
text.upcase.split("").map do |letter| | |
index = alphabet.index(letter) | |
index.nil? ? " " : alphabet[index - 3] | |
end.join | |
end |
This file contains hidden or 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
require_relative '../encrypt.rb' | |
describe "#encrypt" do | |
it "return an empty string when passed an emptry string" do | |
actual = encrypt("") | |
expected = "" | |
expect(actual).to eq(expected) | |
end | |
it "returns the 3-letter backward-shifted text" 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
Don't forget to put the spec files in a
spec
directory so that it looks like this