Skip to content

Instantly share code, notes, and snippets.

@omegahm
Last active January 10, 2020 07:03
Show Gist options
  • Save omegahm/21077b220e7bad077909ca698c80c20e to your computer and use it in GitHub Desktop.
Save omegahm/21077b220e7bad077909ca698c80c20e to your computer and use it in GitHub Desktop.
require 'rspec'
RSpec.describe do
%w(spongebobify minisponge).each do |method|
context "##{method}" do
it 'fox' do
expect(send(method, 'fox')).to eq('fOx')
expect(send(method, 'FOX')).to eq('fOx')
end
it 'i' do
expect(send(method, 'i')).to eq('I')
expect(send(method, 'I')).to eq('I')
end
it 'i love pie' do
expect(send(method, 'i love pie')).to eq('I LoVe piE')
end
it 'brown' do
expect(send(method, 'brown')).to eq('bRoWn')
expect(send(method, 'BROWN')).to eq('bRoWn')
end
it 'lazy' do
expect(send(method, 'lazy')).to eq('LaZy')
end
it 'billiards' do
expect(send(method, 'billiards')).to eq('biLLiArDs')
end
it 'quick' do
expect(send(method, 'quick')).to eq('qUiCk')
end
it 'spongebobifies' do
expect(send(method, 'the quick brown fox jumps over the lazy dog')).to eq('tHe qUiCk bRoWn fOx jUmPs oVeR tHe LaZy dOg')
end
end
end
end
def spongebobify(str)
str.downcase.split(' ').map do |word|
next 'I' if 'i' == word
lowercase = true
word.chars.map do |char|
case char
when 'l'
'L'
when 'i'
lowercase = false
'i'
else
lowercase = !lowercase
lowercase ? char.upcase : char
end
end.join
end.join(' ')
end
def minisponge(s)
# 103 chars
s.downcase.split.map{|w|l=!p&&w==?i??I:w.chars.map{|c|c[?l]??L:c[?i]?l=!p&&c:(l=!l)?c:c.upcase}*''}*' '
end
@omegahm
Copy link
Author

omegahm commented Jan 9, 2020

97 char version

s.downcase.split.map{|w|l=!p&&w==?i??I:w.chars.map{|c|c[?l]??L:c[?i]?c:(l=!l)?c:c.upcase}*''}*' '

Doesn't work for "quick" as it transforms it to "qUicK" instead of "qUiCk".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment