Skip to content

Instantly share code, notes, and snippets.

@fractalatcarf
Created March 30, 2017 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fractalatcarf/e75f5c4cd76e8a107fe2903411a26ec8 to your computer and use it in GitHub Desktop.
Save fractalatcarf/e75f5c4cd76e8a107fe2903411a26ec8 to your computer and use it in GitHub Desktop.
live code de l'acronymizer
def acronymizer(sentence)
# TODO implement acornymizer method
to_ignore = %w(de des d à)
split = sentence.split(/[ ',.]/)
filtered = split.reject{|word| to_ignore.include?(word)}
result = filtered.map do |word|
if block_given?
letters = yield(word)
else
letters = 1
end
word[0,letters].upcase
end
return result.join
end
# tests
puts acronymizer("museum of modern art") #=> MOMA
puts acronymizer("compagnie républicaine de sécurité") #=> CRS
puts acronymizer("société protectrice des animaux") #=> SPA
puts acronymizer("swatch mercedes art") {|word| word == "art" ? 3 : 1 } #=> SMART
puts (acronymizer("courrier d'entreprise à distribution exceptionnelle") do |word|
word == "exceptionnelle" ? 2 : 1
end)
#=> CEDEX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment