Skip to content

Instantly share code, notes, and snippets.

@Oceantidote
Created January 16, 2020 18:21
Show Gist options
  • Select an option

  • Save Oceantidote/56aa4886cf0cb173437cf253725fc94e to your computer and use it in GitHub Desktop.

Select an option

Save Oceantidote/56aa4886cf0cb173437cf253725fc94e to your computer and use it in GitHub Desktop.
acronymize and encrypt
def acronymize(string)
# 1. split sentence
# words = string.split
# # 2. map over array
# letters = words.map { |word| word[0].capitalize }
# # 3. block should take first letter and capitalize
# return letters.join
# 4. Join final result
string.split.map { |word| word[0].capitalize }.join
end
require_relative "../acronymize"
describe "#acronymize" do
it "returns an empty string when passed an empty string" do
actual = acronymize("")
expected = ""
expect(actual).to eq(expected) # passes if `actual == expected`
end
end
describe "#acronymize" do
it "returns an empty string when passed an empty string" do
actual = acronymize("Nobody Wants To Write Nothing")
expected = "NWTWN"
expect(actual).to eq(expected) # passes if `actual == expected`
end
end
describe "#acronymize" do
it "returns an empty string when passed an empty string" do
actual = acronymize("check if It STILL captjhdvaw")
expected = "CIISC"
expect(actual).to eq(expected) # passes if `actual == expected`
end
end
def encrypt(sentence, permutation_level = -3)
alphabet = ("A".."Z").to_a
sentence.split("").map do |letter|
letter == " " ? " " : alphabet[(alphabet.index(letter) + permutation_level) % 26]
end.join
end
# 1. create alphabet
# 2. split sentence into letters
# 3. map over letters
# 4. find index of letter in alphabet
# 5. subtract 3 from index
# 6. Use new index to get new letter from alphabet
# 7. Somewhere along the line deal with the spaces
# 8. Join letteres back together
def encrypt(sentence, permutation_level = -3)
alphabet = ("A".."Z").to_a
sentence.split("").map do |letter|
letter == " " ? " " : alphabet[(alphabet.index(letter) + permutation_level) % 26]
end.join
end
# 1. create alphabet
# 2. split sentence into letters
# 3. map over letters
# 4. find index of letter in alphabet
# 5. subtract 3 from index
# 6. Use new index to get new letter from alphabet
# 7. Somewhere along the line deal with the spaces
# 8. Join letteres back together
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment