Created
January 9, 2020 02:45
-
-
Save brianmcgue/c65c95098ecf4d763e61ede64e08fd6a to your computer and use it in GitHub Desktop.
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 spongebob_case(string) | |
string.split('.').map do |sentence| | |
capital = false | |
sentence.strip.split.map do |word| | |
if word.downcase == 'i' | |
'I' | |
else | |
capital = false | |
word.each_char.map do |char| | |
if char.downcase == 'l' | |
capital = false | |
'L' | |
elsif char.downcase == 'i' | |
capital = true | |
'i' | |
else | |
capital = !capital | |
capital ? char.downcase : char.upcase | |
end | |
end.join('') | |
end | |
end.join(' ') | |
end.join('. ') | |
end | |
def test_input(input, expected) | |
actual = spongebob_case(input) | |
unless actual == expected | |
puts "expected: #{expected.inspect}" | |
puts " actual: #{actual.inspect}" | |
end | |
end | |
test_input('the quick brown fox jumps over the lazy dog', 'tHe qUiCk bRoWn fOx jUmPs oVeR tHe LaZy dOg') | |
test_input('i love pie', 'I LoVe piE') | |
test_input('billiards', 'biLLiArDs') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment