Avatar of Echelon133

Echelon133's solution

to Acronym in the Scala Track

Published at Sep 18 2019 · 0 comments
Instructions
Test suite
Solution

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).

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.

Source

Julien Vanier https://github.com/monkbroc

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

AcronymTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.6.0 */
class AcronymTest extends FunSuite with Matchers {

  test("basic") {
    Acronym.abbreviate("Portable Network Graphics") should be ("PNG")
  }

  test("lowercase words") {
    pending
    Acronym.abbreviate("Ruby on Rails") should be ("ROR")
  }

  test("punctuation") {
    pending
    Acronym.abbreviate("First In, First Out") should be ("FIFO")
  }

  test("all caps word") {
    pending
    Acronym.abbreviate("GNU Image Manipulation Program") should be ("GIMP")
  }

  test("punctuation without whitespace") {
    pending
    Acronym.abbreviate("Complementary metal-oxide semiconductor") should be ("CMOS")
  }

  test("very long abbreviation") {
    pending
    Acronym.abbreviate("Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me") should be ("ROTFLSHTMDCOALM")
  }

  test("consecutive delimiters") {
    pending
    Acronym.abbreviate("Something - I made up from thin air") should be ("SIMUFTA")
  }

  test("apostrophes") {
    pending
    Acronym.abbreviate("Halley's Comet") should be ("HC")
  }
}
object Acronym {
  
  def abbreviate(phrase: String): String = {
    val words = phrase.split(Array(' ', '-'))
    val filterWords = words.filter(word => word.size > 0) 
    val firstLetters = filterWords.map(word => word.toUpperCase.charAt(0))
    firstLetters.mkString("")
  }
  
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?