Convert a phrase to its acronym.
Techies love their TLA (Three Letter Acronyms)!
Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).
Test suite
require 'minitest/autorun'
require_relative 'acronym'
# Common test data version: 1.7.0 cacf1f1
class AcronymTest < Minitest::Test
def test_basic
assert_equal "PNG", Acronym.abbreviate('Portable Network Graphics')
end
def test_lowercase_words
assert_equal "ROR", Acronym.abbreviate('Ruby on Rails')
end
def test_punctuation
assert_equal "FIFO", Acronym.abbreviate('First In, First Out')
end
def test_all_caps_word
assert_equal "GIMP", Acronym.abbreviate('GNU Image Manipulation Program')
end
def test_punctuation_without_whitespace
assert_equal "CMOS", Acronym.abbreviate('Complementary metal-oxide semiconductor')
end
def test_very_long_abbreviation
assert_equal "ROTFLSHTMDCOALM", Acronym.abbreviate('Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me')
end
def test_consecutive_delimiters
assert_equal "SIMUFTA", Acronym.abbreviate('Something - I made up from thin air')
end
end
My answer was:
class Acronym
def self.abbreviate(phrase)
phrase.split("-").join(" ").split.map(&:chr).join.upcase
end
end
I saw other important answers like those:
- I don't like the scan content because it is hard to understand but it's a short answer.
- I didn't know the
tr
method, it replace the first param with the secound param 🤘
class Acronym
def self.abbreviate(string)
string.scan(/(\b\w)/).join.upcase
end
end
class Acronym
def self.abbreviate(name)
name.tr('-', ' ')
.split
.map { |x| x.slice(0) }.join.upcase
end
end
This was fun!
Top comments (0)