Skip to content

Instantly share code, notes, and snippets.

@barangerbenjamin
Created April 12, 2018 17:37
Show Gist options
  • Save barangerbenjamin/d1b8cabf5d1e726b1cc59cc2298ed47a to your computer and use it in GitHub Desktop.
Save barangerbenjamin/d1b8cabf5d1e726b1cc59cc2298ed47a to your computer and use it in GitHub Desktop.
# def acronymize(sentence)
# string = ""
# sentence.split(" ").each do |word|
# string += word[0].upcase
# end
# return string
# end
# redo using map
# [] {} .
def acronymize(sentence)
sentence.split(" ").map { |word| word[0].upcase }.join
end
require_relative "../acronomize"
describe "#acronymize" do
it "should return an empty string when passed an empty string" do
actual = acronymize("")
expected = ""
expect(actual).to eq(expected) # passes if `actual == expected`
end
it "should return the acronym on upcase sentences" do
actual = acronymize("FREQUENTLY ASKED QUESTIONS")
expected = "FAQ"
expect(actual).to eq(expected)
end
it "should return the acronym on downcase sentences" do
actual = acronymize("hello world")
expected = "HW"
expect(actual).to eq(expected)
end
# write test "should return the acronym on upcase sentences"
# for acronym("FREQUENTLY ASKED QUESTIONS") == "FAQ"
# write test "should return the acronym on downcase sentences
# write test for acronym("hello world") == "HW"
end
# modify encrypt so it can encrypt and decrypt using the same method
# and apply the permutation that we want, don't have to be 3
# || [] {} . ; :
def encrypt(text, num = -3)
alphabet = ("A".."Z").to_a
letters = text.upcase.split("")
string = ""
letters.each do |letter|
index = alphabet.index(letter)
if index.nil?
string += letter
else
string += alphabet[(index + num) % 26]
end
end
return string
end
def decrypt(text)
# what code here
encrypt(text, 3)
end
encrypted_text = "FK ZOVMQLDOXMEV, X ZXBPXO ZFMEBO, XIPL HKLTK XP ZXBPXO'P ZFMEBO, QEB PEFCQ ZFMEBO, ZXBPXO'P ZLAB LO ZXBPXO PEFCQ, FP LKB LC QEB PFJMIBPQ XKA JLPQ TFABIV HKLTK BKZOVMQFLK QBZEKFNRBP. FQ FP X QVMB LC PRYPQFQRQFLK ZFMEBO FK TEFZE BXZE IBQQBO FK QEB MIXFKQBUQ FP OBMIXZBA YV X IBQQBO PLJB CFUBA KRJYBO LC MLPFQFLKP ALTK QEB XIMEXYBQ. CLO BUXJMIB, TFQE X IBCQ PEFCQ LC 3, A TLRIA YB OBMIXZBA YV X, B TLRIA YBZLJB Y, XKA PL LK. QEB JBQELA FP KXJBA XCQBO GRIFRP ZXBPXO, TEL RPBA FQ FK EFP MOFSXQB ZLOOBPMLKABKZB."
puts decrypt(encrypted_text)
def encrypt(text)
alphabet = ("A".."Z").to_a
letters = text.upcase.split("")
string = ""
letters.each do |letter|
index = alphabet.index(letter)
if index.nil?
string += letter
else
string += alphabet[index - 3]
end
end
return string
end
# create method name encrypt that takes a parameter(string) called text
# create array containing alphabet
# split text in letters
# iterate on letters
# get index of letter
# check if index exists, if not return normal letter, if exist , apply rotation
# create string from array
puts encrypt("MethOd") == "JBQELA"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment